Hello,
I'm trying to create grayscale image from input image. Input image is 3MB large, resolution is 4288x2848x24BPP

When I run the script, it ends in Fatal Error: Allowed memory size...

So I've assigned 100M to memory_limit and it still doesn't work. It works when memory limit is set to 200M, but in my opinion it's to big for 3MB large image. The server has only 512MB of RAM.

Is there something wrong with my code?

$imageName; // path to 3MB large image
$img = imagecreatefromjpeg($imageName); // this is where the script dies: Fatal Error...
imagefilter($img, IMG_FILTER_GRAYSCALE);
imagejpeg($img, $greyScaleImage, 100);
imagedestroy($img);

Thanks for help, or any advice how to achieve grayscaled image

    The filesize of an image has nothing to do with the amount of memory that the GD library requires in order to process it. The memory consumption is based on the uncompressed version of that image.

    Somewhere in a user-contributed note on php.net, there's a suggested formula for guesstimating the amount of memory required; it involved multiplying the image width, height, and bits-per-pixel together (plus a slight overhead).

      Thanks for reply.

      I have found sth here:
      http://alexrabe.de/2008/05/13/understand-gd-library/

      But I count
      4288 2848 24 / 8 * 1,65 = cca. 60 MB

      I've set memory limit to 100MB, and it doesn't work.

      Maybe, if I'm creating a NEW grayscale image, I need to set memory limit to 124MB (for 2 images)...

      I really don't know 🙁

        Are you assuming it's 24 bit? I think it actually does it at 32-bit, though that would still only take you to about 80MB.

        I tried to allow for enough for a few images and I still got errors when I was fiddling about this at one stage. I gave up in the end and just decided the pictures would have to be smaller.

          @: Is there any chance you can post a link to this 3MB image file that you're using?

            I think GD allocates 32 bits per pixel (supporting an alpha layer?). And remember that if you are creating a second image resource based on it, e.g. imagecreatetruecolor() to be used with the original image resource in imagecopyresampled(), that resource is also going to need corresponding memory based on its number of pixels.

              Using PHP 5.3.2, I was able to load the image linked to above using a memory_limit value of 59M. Setting it to 58M caused the fatal 'out of memory' error.

              So either a) imageshack altered the image file upon upload somehow, or b) you need to do some profiling and figure out how your script is using memory.

                After imagecreatefromjpeg($img) I've added echo memory_get_peak_usage(true) and the result was something around 61MB. Samesize picture, but transparent PNG, used 110MB.

                Thanks for help guys...256MB per script is just fine.

                  Write a Reply...