2

Background:

I have an image resizing script that takes an uploaded image and resizes them to a specified size.( 100x 100 and 250x250 ) However, i have an image that keeps causing the error

PHP Fatal error:  Allowed memory size of 67108864 bytes exhausted (tried to allocate 21600 bytes) in .....

After i've done some looking around, i found that the reason this particular image(and no thte others) was causing the error probably due to it's dimensions.(5400 * 7200 ) After further looking around on SO, i came across the solution to this problem by increasing the memory size in php.ini to 64MB.

However, from PHP GD Allowed memory size exhausted the amount of memory needed for image manipulation seems to be derived from (width * height * 8 ) which would give (5400 * 7200 * 8 =311040000 ). My php ini filehowever says

Maximum amount of memory a script may consume (128MB)

which is far less than the required amount of 311040000 which is in the region of 300+mb? If so, is there any thing i can do to solve this problem?

I'm currently working on localhost.

Thanks!

Community
  • 1
  • 1
Kenneth .J
  • 1,433
  • 8
  • 27
  • 49
  • This is GD. If your image is smaller than estimated memory amount - that doesn't mean that all be well - because don't forget about internal routines which can take memory as well (and best chances are - there are lots of overheads there). Try to use [imagick](http://us1.php.net/imagick) – Alma Do Jan 15 '14 at 10:10
  • It's very likely that the image you are dealing with is not 8-bit (256 colours), as your calculation assumes. Are you converting the image to truecolour before resizing? – Mathew Jan 15 '14 at 10:20

2 Answers2

2

If you're working under apache webserver, you can just write the following in your .htaccess file

<IfModule mod_php5.c>

    php_value memory_limit 512M

</IfModule>

This should do the trick.

Eternal1
  • 5,447
  • 3
  • 30
  • 45
  • @AlmaDo Actually there's nothing wrong with this answer to OP's question "If so, is there any thing i can do to solve this problem?" +1 from me, to balance your -1 – Markus Malkusch Jan 15 '14 at 10:17
  • 'is there any thing i can do to solve this problem?' hardly seems equal to 'why current memory isn't enough'; Increasing memory limit solves OP problem, doesn't it? – Eternal1 Jan 15 '14 at 10:17
  • No it doesn't. Because it is still unreliable (you can't predict how much memory you'll need) - and, besides, it's unclear if OP actually _can_ increase memory limit – Alma Do Jan 15 '14 at 10:19
  • @MarkusMalkusch who said it was my downvote? My point is: `memory-limit`-related answers are _too narrow_ - and only partly correct. They do not answer the question about "why 128M isn't enough" and it's unclear that memory increasing is applicable. – Alma Do Jan 15 '14 at 10:24
  • @AlmaDo True it could be anybody. I'll remove my comment. Sorry. – Markus Malkusch Jan 15 '14 at 10:25
0

You can set PHP to use as much memory as required (even to system memory exhaustion), but setting the limit to '0' - but doing so within anything running within Apache, as mod_php (or, for that matter as a fastCgi, with fpm) is a very bad idea. The reason is that the Apache process will then balloon to the largest size it took, slowing down the system generally an taking up memory long-term, until it is destroyed - this could be days or more.

 php_value memory_limit -1

The command-line version of PHP doens't normally have the limit even set, making it very suitable for larger tasks.

If you are going to be resizing large images (which with camera phones these days, is pretty much all pictures), it would be far better to run the conversion out of Apache, from the command line. You might not even need PHP and GD to be able to do that. One way I've done this before is with various job-queues - at the simplest, a database table with the list of files to resize, which a command-line process goes through one at a time, and it can also restart itself after each completed image - to ensure that all the memory used, is cleared.

Alister Bulman
  • 34,482
  • 9
  • 71
  • 110