Phil Jackson
Phil Jackson

Reputation: 10288

Image size reduction in PHP

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?

Upvotes: 0

Views: 1022

Answers (5)

Niels Bom
Niels Bom

Reputation: 9417

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.

Upvotes: 1

Sabeen Malik
Sabeen Malik

Reputation: 10880

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.

Upvotes: 0

RMcLeod
RMcLeod

Reputation: 2581

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.

Upvotes: 0

joar
joar

Reputation: 15947

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

Upvotes: 0

mauris
mauris

Reputation: 43619

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.

Upvotes: 0

Related Questions