‘Fatal error: Allowed memory size of 31457280 bytes exhausted (tried to allocate 9828 bytes)’.
This is the error i get but I am only trying to upload a 1mb image. I have increased the memory limit in php.ini and the execution time. I am trying this on a local MAMP server, on a Mac using firefox. This going to be for an online image gallery. Any ideas? Below is the code:
ini_set('memory_limit','30M'); if(isset($_POST['submit'])){ if (isset ($_FILES['new_image'])){ $imagename = $_FILES['new_image']['name']; $source = $_FILES['new_image']['tmp_name']; $target = 'images/'.$imagename; move_uploaded_file($source, $target); $imagepath = $imagename; //below here for the removed code $save = 'thumbs/uploads/' . $imagepath; //This is the new file you saving $file = 'images/' . $imagepath; //This is the original file $imagesize = getimagesize($file); list($width, $height) = $imagesize; unset($imagesize); if($width>$height) { $modwidth = 150; $diff = $width / $modwidth; $modheight = $height / $diff; }else{ $modheight = 150; $diff = $height / $modheight; $modwidth = $width / $diff; } $tn = imagecreatetruecolor($modwidth, $modheight); $image = imagecreatefromjpeg($file); $imagecopy = imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height); imagedestroy($image); imagedestroy($im); imagedestroy($imagecopy); imagedestroy($source); $imagejpg = imagejpeg($tn, $save, 100); imagedestroy($tn); imagedestroy($imagejpg);
EDIT
This has now been sorted out hopefully. One of my colleagues had a solution all along but neglected to tell me!
You’re likely loading the image to do some manipulation of it. That causes the image data to be decompressed, which requires a lot of memory for big images (I think it’s about 4 bytes per pixel).
You can choose to either not process the image, or do your processing outside of PHP – for example by invoking ImageMagick or some other program. It depends a bit on what you’re trying to accomplish.