Though it is possible to define a value as low as 0, a value lower than 90 will result in noticeable degradation.Īs an alternative, it is possible to specify a desired file size. 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. If desired, you may compress images past this point (lossy compression), though it will permanently reduce the quality of those images. The above techniques allow for lossless compression that preserves image quality. 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 / jpegoptim *.jpg Lossy compression jpgįor example: jpegoptim samplefile1.jpg samplefile2.jpg samplefile3.jpg All JPGs within a directory Use the following syntax to compress multiple JPGs with one command: jpegoptim. jpgįor example, to compress the file named samplefile.jpg, the command would resemble: jpegoptim samplefile.jpg Multiple JPGs Use the following syntax, but replace with actual name of the file to be compressed, and choose the proper extension: jpegoptim. It is possible to compress a single file, multiple files at once, or all files within a directory. With jpegoptim, you can use the CLI to compress JPG files while preserving image quality. When referring to the below commands, replace the angle brackets () and everything between them with the indicated information. If you are a Nexcess client, installation of these tools is unnecessary all Nexcess servers include 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. The three command-line utilities available to all Nexcess clients are jpegoptim, OptiPNG, and Gifsicle. If you are not, then consider using an application instead. This article assumes you are already somewhat familiar with Linux. Optimizing images in this manner is known as lossless compression. You can also use Imagemin by itself as a Node script.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. If you use one of these build tools, checkout the codelabs for Imagemin with webpack, gulp, or grunt. To compress the images in the "images/" directory and save them to the same directory, run the following command (overwrites the original files): $ imagemin images/* -out-dir =images Imagemin npm module # Imagemin uses the appropriate plugin based on the image format of the input. The Imagemin CLI works with 5 different plugins: imagemin-gifsicle, imagemin-jpegtran, imagemin-optipng, imagemin-pngquant, and imagemin-svgo. Image Format Lossy Plugin(s) Lossless Plugin(s) JPEG imagemin-mozjpeg imagemin-jpegtran PNG imagemin-pngquant imagemin-optipng GIF imagemin-giflossy imagemin-gifsicle SVG Imagemin-svgo WebP imagemin-webp Imagemin CLI # These aren't the only plugins available, but they'd all be good choices for your project. The table below lists popular Imagemin plugins. They offer significantly greater filesize savings, and you can customize the compression levels to meet your needs. If a plugin doesn't mention whether it is "lossy" or "lossless," you can tell by its API: if you can specify the image quality of the output, then it is "lossy."įor most people, lossy plugins are the best choice. Lossy compression reduces file size, but at the expense of possibly reducing image quality. The most important thing to consider when choosing a plugin is whether it is "lossy" or "lossless." In lossless compression, no data is lost. Popular image formats may have multiple plugins to pick from. Imagemin is built around "plugins." A plugin is an npm package that compresses a particular image format (e.g. Generally, the npm module is the best choice because it offers more configuration options, but the CLI can be a decent alternative if you want to try Imagemin without touching any code. Imagemin is available as both a CLI and an npm module. Imagemin is an excellent choice for image compression because it supports a wide variety of image formats and is easily integrated with build scripts and build tools. Lighthouse currently reports on opportunities to compress images in JPEG format only.