What is MozJPEG and why you’d want it
MozJPEG is the new improved JPEG encoder that produces smaller JPEG files.
It is compatible with the now-standard libjpeg-turbo
encoder’s API and ABI, and can be used as a drop-in replacement for it.
The libjpeg-turbo
devs assess MozJPEG as possibly useful in some situations.
The main deciding factors when considering the use of MozJPEG are:
- MozJPEG is slower at encoding
- MozJPEG produces smaller JPEG files!
MozJPEG makes tradeoffs that are intended to benefit webservers.
By using MozJPEG, image optimization programs will produce smaller files without a reduction of quality.
MozJPEG improves JPEG encoding.
To leverage MozJPEG improved encoding, programs can be linked statically with it. For example, jpegoptim
with MozJPEG built-in.
But another great option to use MozJPEG on a system is to install a drop-in MozJPEG library.
MozJPEG as a drop-in replacement for libjpeg-turbo
MozJPEG can be installed as a drop-in library, which will override the standard JPEG encoding library on your system.
In rare cases, if you care about the added encoding time with MozJPEG, you should not install the drop-in system MozJPEG library.
But If you use a vast array of JPEG encoding software on your system (e.g. PHP GD library and image optimizers) and do not care about the added time to encode, the drop-in library is for you.
This instantly super-charges all JPEG encoding software on your system, with MozJPEG encoding optimizations.
Install MozJPEG in CentOS/RHEL 6, 7, 8 or Amazon Linux 2
sudo yum -y install https://extras.getpagespeed.com/release-latest.rpm
sudo yum -y install libmozjpeg
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?
libmozjpeg
leverages dynamic library loader, and any program which uses the standard library (libjpeg-turbo
), now uses the MozJPEG library instead.
You can see whether a particular program or software module will work with MozJPEG, via ldd
.
For example, to check whether PHP GD modules use MozJPEG, 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 => /usr/lib64/libmozjpeg/libjpeg.so.62 (0x00007f07b5c74000)
The libmozjpeg
has complete ABI compatibility with libjpeg-turbo
, so all the dependent software is guaranteed to work as usual (just better!)
Expect size reduction of optimized/encoded JPEG files by 2-5%!