Mutley Posted December 10, 2008 Share Posted December 10, 2008 Fatal error: Out of memory (allocated 48496640) (tried to allocate 8192 bytes) I tried setting: ini_set(’memory_limit’, ‘10000M’); At the top of the script but this hasn't resolved it. It's when I upload and resize images about 3mb in filesize. Any ideas? Regards, Nick. Quote Link to comment Share on other sites More sharing options...
waynew Posted December 10, 2008 Share Posted December 10, 2008 I thought that PHP only allowed uploads of 2MB or less? Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted December 10, 2008 Share Posted December 10, 2008 I thought that PHP only allowed uploads of 2MB or less? I can't remember what the default size it, but it can be changed. Mutley: Does your host actually allow you to increase the memory limit? Quote Link to comment Share on other sites More sharing options...
Mutley Posted December 10, 2008 Author Share Posted December 10, 2008 Not sure. Does that mean I'm unable to fix this if they don't? The script works fine with small images, just large images it produces this memory error. Quote Link to comment Share on other sites More sharing options...
trq Posted December 10, 2008 Share Posted December 10, 2008 Not sure where these qutes come from but there not correct. ini_set(’memory_limit’, ‘10000M’); should be.... ini_set('memory_limit', '10000M'); You may also be best to set it to 0. edit: Hmm.... the quotes even broke the boards syntax highlighting. Quote Link to comment Share on other sites More sharing options...
Mutley Posted December 10, 2008 Author Share Posted December 10, 2008 Weird, the quotes were all messed up, I can set the memory_limit now but not sure what to set it to so it resolves the problem. Quote Link to comment Share on other sites More sharing options...
Mutley Posted December 10, 2008 Author Share Posted December 10, 2008 My host (Hostgator) has said only a 64mb limit can be set, which sadly still doesn't work: Fatal error: Out of memory (allocated 48496640) (tried to allocate 8192 bytes). I don't understand why a small image, that's only about 1mb, is fine with out any limit changes, yet a 3mb image can't upload at all even with a really high limit set. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted December 10, 2008 Share Posted December 10, 2008 Does the file do anything else apart from upload an image? Quote Link to comment Share on other sites More sharing options...
Mutley Posted December 10, 2008 Author Share Posted December 10, 2008 It resizes an image by ratio and moves/copies it. Here is where the memory errors kick in, around the colour functions at bottom: if($copy){ //print 'Image uploaded successfully.<br />'; $simg = imagecreatefromjpeg("$idir" . $url); $currwidth = imagesx($simg); $currheight = imagesy($simg); if($currheight > $currwidth){ $zoom = $twidth / $currheight; $newheight = $theight; $newwidth = $currwidth * $zoom; }else{ $zoom = $twidth / $currwidth; $newwidth = $twidth; $newheight = $currheight * $zoom; } $dimg = imagecreate($newwidth, $newheight); imagetruecolortopalette($simg, false, 256); for ($i = 0; $i < $palsize; $i++) { $colors = ImageColorsForIndex($simg, $i); ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']); imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight); imagejpeg($dimg, "$tdir" . $rand.'-'.$url); } Quote Link to comment Share on other sites More sharing options...
cwarn23 Posted December 10, 2008 Share Posted December 10, 2008 Try adding the below line of code at the top of the file: ini_set('upload_max_filesize','3000M'); That will set the upload input instead of the actual memory. Quote Link to comment Share on other sites More sharing options...
Mutley Posted December 10, 2008 Author Share Posted December 10, 2008 Same error unfortunately. Quote Link to comment Share on other sites More sharing options...
Mutley Posted December 10, 2008 Author Share Posted December 10, 2008 It appears to be this line: imagetruecolortopalette($simg, false, 256); Causing it. The file itself uploads fine but the script fails to resize it on large images. Quote Link to comment Share on other sites More sharing options...
Mutley Posted December 10, 2008 Author Share Posted December 10, 2008 Any ideas on this issue? ??? Would creating the image a different way to avoid the truecolorpalette function avoid this? Quote Link to comment Share on other sites More sharing options...
Mutley Posted January 7, 2009 Author Share Posted January 7, 2009 Still having this problem, here is the full resize script. It does the following: 1) It resizes the uploaded to a Thumbnail size 2) It resizes the uploaded image to a more reasonable size, if it is greater than 800x600 3) It deleted the temp file 4) It inserts the image location into a database <?php if(isset($_POST['SubmitImage'])){ $title = $_POST['title']; $category = $_POST['category']; $imagefile = $_FILES['imagefile']['name']; $content = $_POST['pagesContent']; if($title == NULL || $imagefile == NULL){ ?> <script language="javascript"> alert("Please fill in all the fields."); window.location = "admin.php?a=gallery&inner=create" </script> <? }else{ // Image Upload Form Below $idir = "images/gallery/"; // Path To Images Directory $tdir = "images/gallery/thumbs/"; // Path To Thumbnails Directory $twidth = "150"; // Maximum Width For Thumbnail Images $theight = "150"; // Maximum Height For Thumbnail Images $lwidth = "800"; // Maximum Width For Thumbnail Images $lheight = "600"; // Maximum Height For Thumbnail Images $type = $_FILES['imagefile']['type']; if($type == "image/jpg" || $type == "image/jpeg" || $type == "image/pjpeg"){ $file_ext = strrchr($_FILES['imagefile']['name'], '.'); // Get The File Extention In The Format Of , For Instance, .jpg, .png or .php $rand = rand(1,99999); $copy = copy($_FILES['imagefile']['tmp_name'], "$idir" . $_FILES['imagefile']['name']); // Move Image From Temporary Location To Permanent Location $url = $_FILES['imagefile']['name']; // Set $url To Equal The Filename For Later Use if($copy){ // If The Script Was Able To Copy The Image To It's Permanent Location //print 'Image uploaded successfully.<br />'; // Was Able To Successfully Upload Image $simg = imagecreatefromjpeg("$idir" . $url); // Make A New Temporary Image To Create The Thumbanil From $currwidth = imagesx($simg); // Current Image Width $currheight = imagesy($simg); // Current Image Height if($currheight > $currwidth){ // If Height Is Greater Than Width $zoom = $twidth / $currheight; // Length Ratio For Width $newheight = $theight; // Height Is Equal To Max Height $newwidth = $currwidth * $zoom; // Creates The New Width }else{ // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height) $zoom = $twidth / $currwidth; // Length Ratio For Height $newwidth = $twidth; // Width Is Equal To Max Width $newheight = $currheight * $zoom; // Creates The New Height } $dimg = imagecreate($newwidth, $newheight); // Make New Image For Thumbnail imagetruecolortopalette($simg, false, 256); // Create New Color Pallete $palsize = ImageColorsTotal($simg); for ($i = 0; $i < $palsize; $i++) { // Counting Colors In The Image $colors = ImageColorsForIndex($simg, $i); // Number Of Colors Used ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']); // Tell The Server What Colors This Image Will Use imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight); // Copy Resized Image To The New Image (So We Can Save It) imagejpeg($dimg, "$tdir" . $rand.'-'.$url); // Saving The Image } // If The Script Was Able To Copy The Image To It's Permanent Location //print 'Image uploaded successfully.<br />'; // Was Able To Successfully Upload Image $simg = imagecreatefromjpeg("$idir" . $url); // Make A New Temporary Image To Create The Thumbanil From $currwidth = imagesx($simg); // Current Image Width $currheight = imagesy($simg); // Current Image Height if($currheight > $currwidth){ // If Height Is Greater Than Width $zoom = $lwidth / $currheight; // Length Ratio For Width $newheight = $lheight; // Height Is Equal To Max Height $newwidth = $currwidth * $zoom; // Creates The New Width }else{ // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height) $zoom = $lwidth / $currwidth; // Length Ratio For Height $newwidth = $lwidth; // Width Is Equal To Max Width $newheight = $currheight * $zoom; // Creates The New Height } $dimg = imagecreate($newwidth, $newheight); // Make New Image For Thumbnail imagetruecolortopalette($simg, false, 256); // Create New Color Pallete $palsize = ImageColorsTotal($simg); for ($i = 0; $i < $palsize; $i++) { // Counting Colors In The Image $colors = ImageColorsForIndex($simg, $i); // Number Of Colors Used ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']); // Tell The Server What Colors This Image Will Use } imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight); // Copy Resized Image To The New Image (So We Can Save It) imagejpeg($dimg, "$idir" . $rand.'-'.$url); // Saving The Image $newurl = $rand.'-'.$url; $sql="INSERT INTO `gallery` (`cat`, `url`, `title`, `content`) VALUES ('$category', '$newurl', '$title', '$content')"; mysql_query($sql); //$sql = "UPDATE gallery SET url = '".$rand.'-'.$url."' WHERE id = '".$id."' LIMIT 1"; //mysql_query($sql); $oldimg = "images/gallery/$url"; unlink($oldimg); //imagedestroy($simg); // Destroying The Temporary Image //imagedestroy($dimg); // Destroying The Other Temporary Image print 'Image uploaded successfully.'; // Resize successful ?> <script language="javascript"> alert("Image Succesfully Uploaded. <?=$imgsql?>"); window.location.href=''; </script> <? }else{ print '<font color="#FF0000">ERROR: Unable to upload image.</font>'; // Error Message If Upload Failed } }else{ print "<font color='#FF0000'>ERROR: Wrong filetype (has to be a .jpg or .jpeg. Yours is "; // Error Message If Filetype Is Wrong print $file_ext; // Show The Invalid File's Extention print '.</font>'; } } } ?> Thanks in advance, Nick. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted January 7, 2009 Share Posted January 7, 2009 I'm slightly lost as to what you're trying to achieve with all the color and palette functions. As far as i can see, this part: imagetruecolortopalette($simg, false, 256); // Create New Color Pallete $palsize = ImageColorsTotal($simg); for ($i = 0; $i < $palsize; $i++) { // Counting Colors In The Image $colors = ImageColorsForIndex($simg, $i); // Number Of Colors Used ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']); // Tell The Server What Colors This Image Will Use Is redundant. I'm not sure why you wanted to convert the original image to a palette? And the next lines really do nothing. ImageColorAllocate() does nothing except to provide color identifiers for use with other functions, except that the first call will fill the background of an image created with imagecreate(). But given that the image identifier you're using is the one you're going to copy to, there doesn't seem much point. Perhaps i've missed something along the way. Quote Link to comment Share on other sites More sharing options...
Mutley Posted January 7, 2009 Author Share Posted January 7, 2009 I've removed those lines but still get the error, at the first: $simg = imagecreatefromjpeg("$idir" . $url); Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted January 7, 2009 Share Posted January 7, 2009 The error is the memory limit one, yes? And what is your memory limit and image size? I just ran a test on a ~3Mb image and just creating the image from the jpeg used nearly 50MBs of memory. Quote Link to comment Share on other sites More sharing options...
Mutley Posted January 7, 2009 Author Share Posted January 7, 2009 It's the memory limit one. I upload 2.5mb+ images. I can't believe it uses nearly 50mb of memory! Is there no way around this??? Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted January 7, 2009 Share Posted January 7, 2009 It's the memory limit one. I upload 2.5mb+ images. I can't believe it uses nearly 50mb of memory! Is there no way around this??? I don't think so. The only alternative would be to have some application run on the server which resizes the images, which could be called by your PHP script. Of course, that would only be better if you could find something that resized whilst using less resources. Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted January 7, 2009 Share Posted January 7, 2009 Use Imagemagick convert command to manipulate your images through exec(). Most shared hosts have it installed. Quote Link to comment Share on other sites More sharing options...
mikeschroeder Posted January 7, 2009 Share Posted January 7, 2009 Its purely a memory limit issue, but it depends entirely on the dimensions of the image not the size of the file. lets assume the image is 3000 x 3000 => 9000000 pixels. If the image is an 8bit RGB file then there are 24bits of data per pixel, 8bits per channel. that is 216000000 bits. or approximately 26MB of memory needed to load that image. 8bit rgb = 24bits/pixel 8bit cmyk = 32bits/pixel 16bit rgb = 48bits/pixel 16bit cmyk = 64bits/pixel these calculations are rough too, i'm sure compression etc comes into play and the image formats probably dictate this slightly. I have been using the imagick extenstion for php and it doesn't seem to suffer from the same limitations, not sure if it passes the actual work directly on to imagemagick or not. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.