Forum Moderators: coopster

Message Too Old, No Replies

Memory usage when rotating photos in PHP

avoid script hanging during large loop

         

bjmosk

6:13 pm on Jan 30, 2009 (gmt 0)

10+ Year Member



Hello,

I have a PHP script that loops through a potentially large number of JPG images - it must read the width and height, and rotate each one by 90 degrees. There may be a couple thousand images in this loop, and each image is a photo of good quality, 3+ MB in size.

All the PHP timeout and memory use settings have been adjusted properly to accommodate this script. However, it does hang quite frequently and I'm thinking it may be due to the memory required for such a task. I'm trying my best to clean up all my variables and objects on each iteration, but it doesn't seem to free all the memory it should. I've checked memory_get_usage() etc. which seems to confirm this (although I think I remember reading somewhere that the value may be unreliable).

I'm thinking of writing a separate PHP script to handle the image rotation. On each iteration of the above loop I'd kick off this script via exec(). It would rotate the photo and then end.

These are my questions:
1) Is this considered a safe solution? I try to avoid using exec() if I can.

2) Is the solution unnecessary? Should one PHP script be able to handle such a loop without a problem?

coopster

11:03 pm on Jan 30, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You could always set up a cron job that executes a php script that will iterate over a directory for you. Maybe just do a certain number during each execution? Another thought is that you could use a command line batch utility program to do the dirty work for you. ImageMagick could probably handle this quite quickly.

whoisgregg

11:17 pm on Feb 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you make sure to call imagedestroy [php.net]() on all the image buffers at the end of each loop, you should be fine.

If you're already doing that and still have a problem, I'd go with the cron job approach coopster suggested.