What is MozJPEG and why you’d want it
MozJPEG is the new improved JPEG encoder which produces smaller JPEG files.
MozJPEG is compatible with the now standard
libjpeg-turbo encoder’s API and ABI, and can be used as a drop-in replacement for it.
libjpeg-turbo devs assess MozJPEG as possibly useful in some situations. These are the main deciding factors when considering the use of MozJPEG:
- MozJPEG is slower at encoding
- MozJPEG produces smaller JPEG files!
So MozJPEG makes tradeoffs that are intended to benefit web use… Thus we should use it in a web server, duh! 🙂
Clearly MozJPEG is good for software that does JPEG encoding, like image optimizers out there. And so we have built a package for
jpegoptim that leverages it.
Do you really need a drop-in replacement?
However, some folks may want to use MozJPEG as drop-in replacement for
libjpeg-turbo. I guess there should be a careful evaluation of whether it’s suitable in your case.
As a rule of thumb, you would benefit from MozJPEG drop-in replacement, if you use a vast of array of JPEG encoding software on your system (e.g. PHP GD library and optimizers) and do not care about the added time to encode. Which is, typically, if you did the right thing of running those optimizers in the background and/or caching the encoding results.
All that said, we would like to say that this could be considered an experimental approach. So the drop-in package was placed in a test repository as of yet.
All warnings aside, let’s have all JPEG dependent software in your CentOS / RedHat 7 leverage the power of MozJPEG encoding!
Install libmozjpeg in CentOS 7
Configure test repository and import GPG key
yum install yum-utils yum-config-manager --add-repo https://extras.getpagespeed.com/redhat/7/test/x86_64 rpm --import https://extras.getpagespeed.com/RPM-GPG-KEY-GETPAGESPEED
The drop-in magic
libmozjpeg obsoletes libjpeg-turbo so the magic looks like this:
And that’s pretty much it! Once installed, simply restart whatever daemons that are involved in JPEG encoding routines (e.g. PHP-FPM) or directly start the optimizer programs on the command line. They will make use of MozJPEG right away.
How does this really work?
The standard library (
libjpeg-turbo), installs a shared library file
/lib64/libjpeg.so.62. Most of the packaged software that depends on JPEG encoding will dynamically load it at runtime.
You can see whether particular program or software module depends on dynamic JPEG library via
For example, to check whether PHP GD modules depends on the shared JPEG library you may run (paths are subject to your PHP package origin):
ldd /usr/lib64/php/modules/gd.so | grep jpeg
The output may look like this:
libjpeg.so.62 => /lib64/libjpeg.so.62 (0x00007f520b6f4000)
libmozjpeg has complete ABI compatibility with
libjpeg-turbo, so its package drops in (replaces standard) library file in the same location
/lib64/libjpeg.so.62 for all the software to benefit from it.
That’s all for upgrading your server with MozJPEG encoding! Expect size reduction of optimized / encoded files by 2-5%!
Also published on Medium.