0

I am using imagecreatetruecolor() to create PNG images in PHP. I often need to increase my memory_limit via an ini_set() based on the size (dimensions in pixels) of the image.

So far it has been trial / error on how much memory should be allocated based on the maximum values I am expecting to receive, but I know these values can possibly come in higher as well.

Everything within the image is being drawn using:

imagefill();
imagecolorallocate();
imagerectangle();
imagefilledrectangle();
imagettftext();

And the final output is through:

imagepng();
imagedestroy();

Currently I have it set to 512MB and it is handling most of the maximum sizes I am expecting (3178 x 2878), but I am trying to also not over-allocate memory if I am drawing something very small.

Is there a method I can use to take my image dimensions in pixels and calculate the required memory that should be allocated? Obviously I'll need to add a little extra for the overhead of processing, but I need to figure out how much memory the actual image creation uses.

Thanks!

Bryan Zwicker
  • 642
  • 1
  • 6
  • 24
  • 1
    the setting of `memory_limit` doesn't actually allocate the memory, it simply states that it can use up to that amount of memory. – Jonathan Jun 01 '15 at 15:31
  • @Augwa Oh right... That was an oversight in my thought process. I guess I could just set that value at the highest my system would be able to handle and let the script use what it needs. – Bryan Zwicker Jun 01 '15 at 15:36

0 Answers0