[I wish I could] Improve thumbnail quality with ImageMagick
29 October 2024 | Blog
Update 2: I got annoyed and added optional support for ImageMagick V6, some manual configuration is required.
Update: The PECL extension for ImageMagick is broken for PHP 8.3. It has been busted for over a year and nobody seems to give a damn. PHP 8.2 reaches end of life on 31 December and 8.1 is already dead. This means: ImageMagick is basically out of service for contemporary PHP.
Now it is possible to manually compile and install ImageMagick, so long as you are running your own server and don't mind also installing a bunch of dependencies required to get it up and running. But since the goal here is to produce a tool the convenient use of others, most of whom will use commercial webhosts, that is not a viable option.
Yet again I am reminded of how frustrating open source can be. ImageMagick is not some obscure library, it is installed on most commercial web hosts, and PHP is still the dominant website language. How can the extension sit there broken for more than a year?
I've never been happy with thumbnails generated by PHP's native GD2 library, so I'm adding optional support for ImageMagick in the next release. If ImageMagick is installed, Tuskfish will use it by default, and if not, it will fall back to GD2. ImageMajick is widely available on most, if not all, commercial web hosts.
ImageMagick offers two benefits over GD2:
- It generates much better quality thumbnails than PHP's native GD2 library. The thumbnails generated by GD2 are a bit fuzzy, and larger images can actually look pretty bad.
- ImageMagick is aware of colour profiles and will preserve colour space information. GD2 is not only unaware of colour profiles, but actually discards them, which causes colour shifts in images that can render them drab and washed out (converting your images to sRGB will still minimise the damage, as most browsers will assume untagged images are sRGB).
To keep things simple, I will only be supporting the sRGB colour profile, which is the default for the web. You should already be converting your master images in sRGB before using them on websites anyway, because browsers generally will not be able to render other profiles correctly.
Should you need to distribute images with other colour profiles, you can. When you upload image content you still have separate 'image' and 'media' upload options. The 'image' field is used to build thumbnails for display, and you should ensure that version is sRGB. Upload the master image with custom colour profile that you want to distribute in the 'media' field, that is used for downloads (not display) and can carry any colour profile you want.
How to rebuild all your thumbnails site wide
It's automatic. To replace your GD2 thumbnails with nice sharp ImageMagick ones, simply delete all the thumbnails in html/cache. Tuskfish will rebuild fresh thumbnails on demand as each page is reloaded, and they will be added to the cache (assuming the cache is enabled).
Creative Commons Attribution.