tags:

views:

235

answers:

5

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?

A: 

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.

thephpdeveloper
A: 

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

jwandborg
reading from temp location/file upload
Phil Jackson
A: 

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.

RMcLeod
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
A: 

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.

Sabeen Malik
+1  A: 

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.

Niels Bom