MozJPEG as drop-in replacement for libjpeg in CentOS / RedHat 7

Danila Vershinin
Mar 10 · 3 min read

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.

The 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

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/ 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 ldd.

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):

The output may look like this:

The libmozjpeg has complete ABI compatibility with libjpeg-turbo, so its package drops in (replaces standard) library file in the same location /lib64/ 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%!

Originally published at GetPageSpeed.

Danila Vershinin

Written by

Performance Oriented DevOp

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade