Take the tour ×

In cases such as this error:

Fatal error: Out of memory (allocated 31981568) (tried to allocate 3264 bytes)

Can I use the GD lib to reduce its file size first before getting to this stage?

share|improve this question
Note: This is a follow-up for stackoverflow.com/questions/1672558/… –  soulmerge Nov 4 '09 at 11:15
add comment

5 Answers

In short: no.

GD uses memory to reduce the size of an image, if the image is too big the memory limit is exceeded and an error is given.

You can calculate how big an image can be so you can stay under a certain memory limit here: http://www.dotsamazing.com/en/labs/phpmemorylimit

An option, although an unpopular one with shared hosts, is increasing the memory limit, you can do this with ini_set() or in an .htaccess file. Please check if your host allows this. If you have your own host, configure Apache accordingly.

An (also mentioned) option is using Imagemagick, a program that runs on the server that you can call to do the resizing for you. The memory limit for this program can be different than the one for PHP, but there probably will be a limit as well. Contact your host for more info.

share|improve this answer
add comment

You can instead set a higher memory limit.

ini_set('memory_limit', $file_size * 2);

Because if you want to reduce the size using GD, you still need to allocate memory for that file before you can reduce the size.

Also remember to set a file size limit to your image uploads.

You can use the filesize() function to check the file size before reading/opening the file.

share|improve this answer
add comment

Are you reading a file from disk with a PHP script?

share|improve this answer
reading from temp location/file upload –  Phil Jackson Nov 4 '09 at 11:04
add comment

This is obviously continuing from your previous post. If I remember rightly it's an uploaded image you're working with? If so what is the size of the image? If it's really large you should consider limiting the size of image uploads.

share|improve this answer
You are correct. The reason why i asked id that a "friend" of mine also in the business told me that he made a script that would reduce the filesize to pass through memory limits (heartinternet) and as i need such a script, was wondering if it could be done. –  Phil Jackson Nov 4 '09 at 12:03
add comment

That would happen when u want the memory to do more than it can .. you might want to look into imagemagick , so instead of resizing via PHP just send request to imagemagick to do the resizing.

Or the easier way would be to increase the memory limit per script of php scripts via ini.

share|improve this answer
add comment

Your Answer


