Jump to content
Sign in to follow this  
son

Fatal error: Out of memory

Recommended Posts

Have issue with image upload script. The error is as follows:

Fatal error: Out of memory (allocated 56360960) (tried to allocate 19712 bytes)

and image file that caused this is 514KB. The entry in db is okay.

 

I checked with local settings on hosted server:

max_file_uploads -> 20upload_max_filesize -> 40Mmemory_limit -> 90Mpost_max_size -> 8M

 

As lowest value post_max_size is 8M which is 1024 KB am confused. The image is only half as big. My script runs through the creation of three sizes when uploading an image (thumbnail, medium and large version). The large version sits okay on webserver, it is only the medium and small version that does not run okay.

 

I run the problematic file through a Photoshop resize and uploaded the 529KB file and it created a 42KB large version, 12KB medium version and 2KB thumb. It worked well. Only then did I notice that the original file was actually smaller in size than 529KB. Not only that it is very weird that the save for web in Photoshop has actually increased file size, but the original file should definitely upload okay. It does not. Is there any sense in this?

 

Thanks,

Son

Share this post


Link to post
Share on other sites

Are you working with PHP GD functions? Remember to use image_destroy() as soon as you're done with an image resource, and if possible, only have one image resource being processed at a time.

 

It's not the uploading that's the problem, it's that the program is using a lot of resources.

Share this post


Link to post
Share on other sites

I use image_destroy(), so not sure if that is the issue. Paste code below...

function resize_image($opts){  $src = isset($opts['source']) ? $opts['source'] : '';  $dest = isset($opts['dest']) ? $opts['dest'] : '';  $w = isset($opts['w']) ? intval($opts['w']) : 0;  $h = isset($opts['h']) ? intval($opts['h']) : 0;    if ($src == '')  {        return;  }  if ($w == 0 && $h == 0)  {        return;  }  if ($dest == '')        $dest = $src; // resize in place  // open the image$extVar = explode('.',$src);$ext = strtolower(array_pop($extVar));  switch ($ext)  {        case 'jpg':        case 'jpeg':          $i = imagecreatefromjpeg($src);        break;        case 'gif':          $i = imagecreatefromgif($src);        break;        case 'png':          $i = imagecreatefrompng($src);        break;        default:          return;  }  $new_w = imagesx($i);  $new_h = imagesy($i);  if (($w != 0 && $new_w <= $w && $h == 0) ||          ($w == 0 && $h != 0 && $new_h <= $h) ||          ($w != 0 && $new_w <= $w && $h != 0 && $new_h <= $h))  {        // image is small enough        if ($dest != $src)          copy($src, $dest);        return;  }  // determine new size  if ($w != 0 && $new_w > $w)  {        $new_h = ($w / $new_w) * $new_h;        $new_w = $w;  }  if ($h != 0 && $new_h > $h)  {        $new_w = ($h / $new_h) * $new_w;        $new_h = $h;  }    // resize  $new = imagecreatetruecolor($new_w, $new_h);    if ($ext == 'png' || $ext == 'gif')  {        $trnprt_indx = imagecolortransparent($i);        // If we have a specific transparent color        if ($trnprt_indx >= 0)         {          // Get the original image's transparent color's RGB values          $trnprt_color = imagecolorsforindex($i, $trnprt_indx);          // Allocate the same color in the new image resource          $trnprt_indx = imagecolorallocate($new, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);          // Completely fill the background of the new image with allocated color.          imagefill($new, 0, 0, $trnprt_indx);          // Set the background color for new image to transparent          imagecolortransparent($new, $trnprt_indx);        }        // Always make a transparent background color for PNGs that don't have one allocated already        elseif ($ext == 'png')        {          // Turn off transparency blending (temporarily)          imagealphablending($new, false);          // Create a new transparent color for image          $color = imagecolorallocatealpha($new, 0, 0, 0, 127);          // Completely fill the background of the new image with allocated color.          imagefill($new, 0, 0, $color);          // Restore transparency blending          imagesavealpha($new, true);        }  }  imagecopyresampled($new, $i, 0, 0, 0, 0, $new_w, $new_h, imagesx($i), imagesy($i));  imagedestroy($i);  // save the image  switch ($ext)  {        case 'jpg':        case 'jpeg':          imagejpeg($new, $dest);        break;        case 'gif':          imagegif($new, $dest);        break;        case 'png':          imagepng($new, $dest);        break;  }  imagedestroy($new);}

This script is working well on other sites. In fact it has been generally employed and it is not written by me... This is the first time I encounter an issue with it. Can you see any issue. I cannot see what would be wrong with this piece of code...

 

Son

Share this post


Link to post
Share on other sites

The problem isn't with upload_max_filesize or post_max_size. The system doesn't seem to be allocating enough memory for PHP to process the image. I guess you should try increasing the memory_limit directive, the default is 128M and you only have 90M.

Share this post


Link to post
Share on other sites

Thanks Ingolme. Unfortunately, it is a hosted site with no access to php.ini or is there another way to implement this (locally in script not possible, is it?)...

Son

Share this post


Link to post
Share on other sites

is there another way to implement this (locally in script not possible, is it?)...

 

no, changing php.ini settings at run time (via PHP script) won't work for this. (i believe)

Edited by thescientist

Share this post


Link to post
Share on other sites

You can change the memory limit at runtime, but you can't make it higher than what is specified in php.ini. You can also specify the memory limit in a .htaccess file.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...