code_chris's avatar

intervention/image hitting php memory_limit

Hi guys, I don't usually post looking for help as I can usually find a solution by searching, but this one's got me stumped.

I am building a website which allows the user to upload multiple images and then on the server I use intervention/image to resize and save them. This has been working fine on my vm but when I run it on the server it sometimes hits the memory_limit.

I have logged the memory usage in the code at every stage and on the vm it uses about 20mb average all of the time. On the server it often works, but sometimes it suddenly spikes to 70mb and then throws the error that memory is exceeded.

I was wondering if anyone else has had this problem, or knows of a good way to debug what is causing it to hit the memory_limit with some files. The strange thing is that it will always cause an error if i pick certain files, but then if i add a certain other image file with the others it works just fine, which doesn't seem to make sense since it's more images then.

Hopefully this makes sense and I'd appreciate any advise I can get right now!

0 likes
9 replies
PatrickBauer's avatar
Level 1

Image manipulation is a memory-heavy task. Are the images you're having trouble with very large? Not in terms of filesize, but in terms of pixel density (e.g. > 2000x2000px)? Take a look at this: http://www.dotsamazing.com/en/labs/phpmemorylimit And mind, that you need some more memory for your application itself.

code_chris's avatar

They are large images, some are 4000 x 3000. The thing is each time I loop through an image it is destroyed, and from my logging I can see that the memory is not increasing through each loop iteration, at least most of the time.

Here is one of my logs I threw together to try and debug it:

[2015-03-06 19:17:36] production.INFO: --------------------------------------------
[2015-03-06 19:17:36] production.INFO: ProjectsControler@store | 1 - 9073192
[2015-03-06 19:17:37] production.INFO: Create Project Command handler - 10077184
[2015-03-06 19:17:37] production.INFO: ProjectsControler@store | 2 - 10072960
[2015-03-06 19:17:37] production.INFO: UploadPicture | 1 -10106520
[2015-03-06 19:17:37] production.INFO: UploadPicture | before image make -10228472
[2015-03-06 19:17:37] production.INFO: UploadPicture | after image make -70650888
[2015-03-06 19:17:38] production.INFO: UploadPicture | after widen -13566160
[2015-03-06 19:17:38] production.INFO: UploadPicture | after crop -13266232
[2015-03-06 19:17:38] production.INFO: UploadPicture | after make directory -13274112
[2015-03-06 19:17:38] production.INFO: UploadPicture | after save -13431352
[2015-03-06 19:17:38] production.INFO: UploadPicture | second widen -11162648
[2015-03-06 19:17:38] production.INFO: UploadPicture | second crop -11199608
[2015-03-06 19:17:38] production.INFO: UploadPicture | second directory make -11199608
[2015-03-06 19:17:38] production.INFO: UploadPicture | second save -11067960
[2015-03-06 19:17:38] production.INFO: UploadPicture | added picture to database -11330104
[2015-03-06 19:17:38] production.INFO: UploadPicture | destroyed -11001776
[2015-03-06 19:17:38] production.INFO: UploadPicture | 1 -11001256
[2015-03-06 19:17:38] production.INFO: UploadPicture | before image make -11001824
[2015-03-06 19:17:39] production.INFO: UploadPicture | after image make -71154328
[2015-03-06 19:17:39] production.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Allowed memory size of 134217728 bytes exhausted (tried to allocate 2275041 bytes)' in /var/sites/d/website/laravel/vendor/intervention/image/src/Intervention/Image/Gd/Commands/ResizeCommand.php:56 Stack trace: #0 /var/sites/d/website/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(116): Illuminate\Foundation\Bootstrap\HandleExceptions->fatalExceptionFromError(Array) #1 [internal function]: Illuminate\Foundation\Bootstrap\HandleExceptions->handleShutdown() #2 {main}

It seems like it does the first image without problems and making the second image causes a massive memory spike, but using the same images on my vagrant machine doesn't cause the same problem, the memory stays low and it runs just fine, every time. I am running it on shared cloud hosting, could it be some kind of server setting that causes it? Or is my logging just really inaccurate?

code_chris's avatar

Oh wait I guess it is spiking on the first one, hmmm

PatrickBauer's avatar

They are large images, some are 4000 x 3000

That seems to be the culprit :D What is your current memory_limit setting and what size are the new image sizes you create with your application?

bashy's avatar

Just up the limit? No way around it, PHP will use the memory it needs to do a task.

I normally work with 512 limit if not 1024...

code_chris's avatar

The trouble is I'm using a reseller account at the moment and it's setup with cloud hosting so I don't think i can change the memory_limit. I have a custom php.ini which lets me change upload_max_filesize and post_max_size but the memory_limit made no difference in there.

It's set at 128 at the moment. The thing is when I upload the same files on the vm the log shows a different story that's why I wasn't sure, but I guess the logging is not telling the full story.

Some of the images are 4000 x 3000 and I resize them to about 800 x 600 on the server, ideally I would upload smaller images, but the client is not particularly good with computers so I'm looking for another solution.

I was considering resizing with javascript before uploading, do you think that's a viable option?

bobbybouwmann's avatar

If that's your only option, then you can do that

I think you should just contact your hosting and ask them to change it for you, most of the time they will change it for you, at least that's what they did for me!

code_chris's avatar

Yeah I just asked them and they said they would do it, I just thought that being on a shared hosting account that was one of the limits and I'd need a dedicated hosting option to increase it. Hopefully there's a way I can change it myself in future.

Thanks for the help guys, really appreciate it!

bashy's avatar

Well, companies running shared hosting will only allow so much resources to be used but it really depends on the company.

Ask them about it and see what they say. You may want to switch to something like a VPS or scaleable hosting where you have enough RAM to use for PHP.

Please or to participate in this conversation.