In the past I've used GD when working with images, but I've read ImageMagick is better than GD and am considering using it in my next project.
I initially just planned on using the PECL imagick extension, but then I read this post on the ImageMagick forum:
Imagick is not supported well if at all any more and was not created by the ImageMagick team. So I am not sure what to tell you at this point other than to just use PHP exec() for everything. You are not gaining much if anything from Imagick and it does not support many of the newer Imagemagick features.
If I decide to use ImageMagick, should I avoid the imagick extension and instead use shell_exec()
?
If It is possible for you to do everything through exec
then it is probably the most performant way of calling ImageMagick.
If you want to use the Imagick API feel free, it is maintained (I'm a maintainer) and if you find any features of ImageMagick that are not exposed through it, please let us know: https://github.com/mkoppanen/imagick
The only limitation on calling ImageMagick through either exec or Imagick is that you really don't want to be calling any image processing functions directly in a web-server. It is much, much, much better to have the image processing queued up in a background task, and processed by a 'queue processor' so that your web-server isn't being slowed down by the image processing.
The queue processor I use is a simple setup using:
Supervisord to run a PHP script.
A small PHP script that waits to read image processing requests off of a queue, unpack the arguments and call the appropriate image processing function.
It then saves the processed file. You could use event notification to tell the web-server the processed file is now available; instead of that, I use consistent hashing to generate the filename. That allows the web-server to check whether the file has been generated or not, just by checking file_exists(). e.g. if the function to generate the image is charcoalimage
the generated filename would be "charcoalimage_".sha1($parameters)
, so both the web-server wanting the image and the queue processor can calculate the same filename.
While it's being processed any requests for that image while it hasn't been generated are 'delayed' by sleeping for a fraction of a second. The browser redirected to the same URL but with an extra parameter added, to try to prevent browsers from thinking there is a redirect loop. When the image has been made by the background task, obviously the file is just served by the web-server.
The queue setup described above is implemented in code here - however it is really quite poorly written, and even more poorly documented so am not sure it will be much help as it is.
It may be easier to understand by going to http://www.phpimagick.com/ and finding an example that is slow to generate (such as Oilpaint Image), and watching the network tab in your browser.
When a request for an image is made, the original url:
http://www.phpimagick.com/image/Imagick/oilPaintImage?image=Lorikeet&radius=5
sends a request to the background processing queue, sleeps a little while, and redirects the browser to:
http://www.phpimagick.com/image/Imagick/oilPaintImage?image=Lorikeet&radius=5&job=1
If the image has been generated, it is now served up, otherwise the browser is now redirected to:
http://www.phpimagick.com/image/Imagick/oilPaintImage?image=Lorikeet&radius=5&job=2
etc. until either the image is generated (and so served) or the browser gets fed up and stops following the redirects.
The whole point of the above is that image processing is really, really slow. For some of the functions in ImageMagick, it's not unusual for the processing to take 20-40 seconds. You really don't want any slow requests in PHP. Everything should be either completed very fast, or queued up to a background task.
Once slow things are in a queue and being processed by a background task, it becomes absolutely trivial to throw more processing resources at it as required, monitor the queue for latency, or failed jobs. All of those things are much harder to do when the processing is being done in the web-server.