Search code examples
phpimagemagickimagick

Best practice in converting large volumes of images (imagick and PHP)


I've already setup and finished coding the conversion of images from jpeg/png to webp. I will be using it to convert hundreds of images. But PHP has a timeout limit and changing it into large number is a bit unorthodox (or is it?). Right now the way I do it is a execute the page using a browser then it start converting but then it reaches the timeout limit before converting all images so I have to refresh the page so it will start converting again. This is working already. It just doesn't feel right, so my question is, do you have any suggestion to do it more appropriately? or maybe more conventional?

Hope someone can help me.

Thanks


Solution

  • Some measures you can take:

    1. Execute PHP via the command line instead of via a web browser. The command line version has no maximum execution time limit.
    2. Break the job into chunks. Figure out a way to break up the images so that you can convert batches at a time.
    3. Consider implementing an asynchronous job queue. If you are converting these images in response to a user interaction with your website, consider scheduling the conversion to run asynchronously via a job. This will also allow you to take advantage of point 1 above.
    4. Consider running the script on a different server. You don't neccessarily have to run jobs like this on your production web server. You can set up a separate server just for doing this kind of operation so that it doesn't take processing power away from your users. It could even be a temporary server that use use and shutdown afterwards if the conversion is not going to occur frequently.