fbpx

Images / Server Setup

MozJPEG as drop-in replacement for libjpeg in CentOS/RHEL and Amazon Linux

by , , revisited on


We have by far the largest RPM repository with NGINX module packages and VMODs for Varnish. If you want to install NGINX, Varnish, and lots of useful performance/security software with smooth yum upgrades for production use, this is the repository for you.
Active subscription is required.

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%!

  1. Bragi Austfjörð

    When I check ldd /usr/lib64/php/modules/gd.so | grep jpeg
    I get this error ldd: /usr/lib64/php/modules/gd.so: No such file or directory

    Reply
    • Danila Vershinin

      Hi Bragi,

      This only means that the PHP GD module was not installed or installed to a non-standard location.
      To check whether a given software makes use of MozJPEG, of course, you need to install the software itself.

      The GD module can be installed with yum install php-gd

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.