How to optimize JPEGs, PNGs, and GIFs from the CLI

    October 7, 2019

    If you need to optimize images while at the same time preserving their quality and dimensions, it is possible to compress these images through the CLI. Optimizing images in this manner is known as lossless compression. This article assumes you are already somewhat familiar with Linux. If you are not, then consider using an application instead. 

    The three command line utilities available to all Nexcess clients are jpegoptim, OptiPNG, and Gifsicle. Each tool optimizes image files of the relevant type by compressing their size as much as possible while upholding the quality of those images. If you are a Nexcess client, installation of these tools is unnecessary; all Nexcess servers include jpegoptim, OptiPNG, and Gifsicle.

    When referring to the below commands, replace the angle brackets (<>) and everything between them with the indicated information.

    jpegoptim

    With jpegoptim, you can use the CLI to compress JPG files while preserving image quality. It is possible to compress a single file, multiple files at once, or all files within a directory.   

    Individual JPGs

    Use the following syntax, but replace <filename> with actual name of the file to be compressed, and choose the proper extension:

    jpegoptim <filename>.jpg

    For example, to compress the file named samplefile.jpg, the command would resemble:

    jpegoptim samplefile.jpg

    Multiple JPGs

    Use the following syntax to compress multiple JPGs with one command: 

    jpegoptim <filename_1>.jpg <filename_2>.jpg <file_name3>.jpg

    For example:

    jpegoptim samplefile1.jpg samplefile2.jpg samplefile3.jpg

    All JPGs within a directory

    To compress JPGS within a directory, go to the directory containing those images, then issue the command to compress all files within that directory:

    cd <directory>/
    jpegoptim *.jpg

    Lossy compression

    The above techniques allow for lossless compression that preserves image quality. If desired, you may compress images past this point (lossy compression), though it will permanently reduce the quality of those images. There are two methods.

    The first and most useful of these methods involves using the --max flag with a value of 0 - 100 to define the desired quality level. The highest quality image is 100. Though it is possible to define a value as low as 0, a value lower than 90 will result in noticeable degradation.

    The syntax is:

    jpegoptim --max=<quality_level> <filename>.jpg

    For example:

    jpegoptim --max=90 <filename>.jpg

    As an alternative, it is possible to specify a desired file size. To do so, use this syntax:

    jpegoptim --size=<desired_size> <filename>.jpg

    For example, if you wanted to compress a single file to 500k, it would resemble:

    jpegoptim --size=500k samplefile.jpg

    Or, if you wanted to compress all files within a directory to 1M, it would resemble:

    jpegoptim --size=1M *.jpg

    Additional information

    For more details about jpegoptim tool, run:

    man jpegoptim

    OptiPNG

    With OptiPNG, you can use the CLI to compress PNG files while preserving image quality and dimensions. As with jpegoptim, It is possible to compress a single file, multiple files at once, or all files within a directory.  The use of OptiPNG strongly resembles that of using jpegoptim.

    Individual PNGs

    Use the following syntax, but replace <filename> with actual name of the file to be compressed, and choose the proper extension:

    optipng <filename>.png 

    For example, to compress the file named samplefile.png, the command would resemble:

    optipng samplefile.png

    Multiple PNGs

    Use the following syntax to compress multiple PNGs with one command: 

    optipng <filename_1>.png <filename_2>.png <file_name3>.png 

    For example:

    optipng samplefile1.png samplefile2.png samplefile3.png

    All PNGs within a directory

    To compress PNGs within a directory, first go to the directory containing those images, then issue the command to compress all files within that directory: 

    cd <directory>/
    optipng *.png

    Lossy compression

    PNG was designed as a lossless format; therefore, loss compression is not possible.

    Additional information

    For more details about Optipng, run:

    man optipng

    Gifsicle

    Gifsicle allows you to optimize GIF files by compressing them to the smallest possible size while preserving image quality. It is possible to compress a single file, multiple files at once, or all files within a directory.  

    Individual GIFs

    Use the following syntax, but replace <filename> with actual name of the file to be compressed, and choose the proper extension:

    gifsicle --batch --optimize <filename>.gif 

    For example, to compress the file named samplefile.jpg, the command would resemble:

    gifsicle --batch --optimize samplefile.gif 

    For some files, the above command may not result in a smaller filesize. For these files you can run the same command with a higher optimization level than the default:

    gifsicle --batch --optimize=3 <filename>.gif

    Multiple GIFs

    Use the following syntax to compress multiple JPEGs with one command: 

    gifsicle --batch --optimize <filename_1>.gif <filename_2>.gif <file_name3>.gif

    For example:

    gifsicle --batch --optimize samplefile1.gif samplefile2.gif samplefile3.gif

    All GIFs within a directory

    To compress GIFs within a directory, go to the directory containing those images, then issue the command to compress all files within that directory:

    cd <directory>/
    gifsicle --batch --optimize *.gif

    Lossy compression

    Like PNG, GIF was designed as a lossless format; therefore, lossy compression is not possible.

    Additional information

    For more details about Gifsicle tool, run:

    man gifsicle


    For 24-hour assistance any day of the year, contact our support team by email or through your Client Portal.

    Was this article helpful?

    Send feedback

    Can’t find what you’re looking for?

    Our award-winning customer care team is here for you.

    Contact Support