Optional support for ImageMagick 6 available
7 November 2024 .zip | 5.04 KB | Blog, Releases, Tech notes
As previously discussed I don't like the quality of thumbnails generated with PHP's native GD2 library. They tend to be a bit blurry and often suffer from colour shifts as GD2 can't read colour profiles and actually discards them. So I've added optional support for ImageMagick V6. This gives much better quality thumbnails than PHP's native GD2 library and has configurable compression and sharpening. It is also colour-space aware, although I am only supporting sRGB for the moment as this is the standard for the web and standard displays.
Unfortunately, the PECL extension for ImageMagick is broken for PHP V8.3 (and has been for over a year), so I had to add ImageMagick support by calling it through exec(). Not everyone will want to do that, so I am releasing it as optional upgrade, via an alternative ResizeImage trait file that must be swapped in. Set up is as follows:
Requirements
- ImageMagic V6 available on your webserver. Most hosting companies provide this.
- An sRGB.icc colour profile available on your webserver. If you don't have one, install a colour management package like icc-profiles-free, or you can directly download a copy of the sRGB.icc profile (V2 recommended) and put it somewhere convenient.
- Enable access to PHP's exec() function. it is usually disabled by default through the disable_functions settting in php.ini.
Installation
- Swap in the alternative ResizeImage.php trait file linked below. Rename the existing file so you can revert if necessary. The file needs to be placed in:
trust_path/libraries/tuskfish/class/Tfish/Traits/ - Configure the file path to the sRGB.icc colour profile on your server in the method createThumbnailWithExec().
Rebuilding your image cache
- Clear the image cache (html/cache), which will cause thumbnails to be progressively rebuilt as pages are requested and cache misses are detected.
- Optionally, you can adjust the level of compression and sharpening to your liking in createThumbnailWithExec(). By default, I set JPG compression to 75, which offers a good trade off between image quality and file size, and sharpening to 0.5, which is a mild sharpening to account for loss of clarity in downsampling.
I recommend keeping your JPG compression between 65 and 80, any lower and your images will get a bit scrappy, and higher the file size will start to resemble the original master image. I pre-sharpen my images in a photo editor before upload, so I don't want to overdo things. If you upload images without pre-sharpening them, you could experiment with increasing the sharpening to 1, which is still conservative but will give them some extra crispness.
Be aware that ImageMagick uses more resources than GD2. On my little webserver (2GB RAM) I noticed a one or two second delay the first time a page was loaded, as a new thumbnail was generated for the cache. On the gallery section, index pages carrying 20 large images would time out. To counter this, I temporarily set the gallery pagination preference to 5 images and flipped through the index pages to generate the thumbnails, before reverting to 20. Once the thumbnails are built, ImageMagick is no longer called, as images will be directly retrieved from the image cache.
I also flipped through the pagination on the front page of my site to generate the thumbnails. These carried ten smaller images each and I did not encounter any resource problems, but I just wanted to get all the thumbnails built up front so I could check for issues.
Note that the 'Clear cache' link in Tuskfish settings only clears the HTML cache, not the image cache. Once your thumbnails are built, ImageMagick will only be called when you add a new piece of content or change it's image file. So it should have zero impact on front end site performance.
Caution
If you install ImageMagick yourself you should edit the policy.xml file to set reasonable limits on resource use, restrict it to legitimate file types (jpg, png, gif) and so on, to reduce opportunity for abuse.
PHP's exec() function is also commonly disabled for security reasons. Tuskfish only uses exec() for this one purpose and there is no user-side input into it whatsoever. But if you have other web applications installed on your server it is conceivable that they might. So, be careful with it. If you find you don't need or want ImageMagick later on, you should disable exec() again.
Future updates: PECL and ImageMagick V7
If the maintainers of the ImageMagick PECL extension ever fix it for PHP 8.3, I will shift Tuskfish to support that instead, and roll it into the regular release, to get rid of the requirement for exec() to be enabled. I did also try to use ImageMagick V7 first, but ran into problems. It seems that there are some issues with PHP 8.3 support there as well. Since PHP 8.2 support ends in a few weeks, I hope that these issues will be resolved sometime soon.
Download the updated ResizeImage.php file below:
Copyright, all rights reserved.