2.17.2.6. Image optimization
There are several ways to optimize images:
- Without modifying source files — files are converted by the server on the fly.
- With modifying source files — files are converted beforehand using specialized utilities.
Without modifying source files
You can optimize images on the fly without modifying the source files using the following options in the PageSpeed module:
- inline_images — embedding small images directly into HTML as data: URL (which reduces the number of server requests).
- recompress_images — removing excess data from images (metadata and ICC color profiles) and converting them to the most suitable format (including conversion to WebP if the client's browser supports it) (speeds up image loading).
- resize_images — resizing images to the size at which they appear on the page (which speeds up image loading).
With modifying source files
Attention!
Before proceeding, be sure to create a backup of your site in case something goes wrong.By default, the hosting servers support utilities for optimizing image files in popular formats:
- GIF: gifsicle.
You can use them directly via the console or create your own script to access them and perform the necessary operations. There are many ready-made scripts for optimizing images, which consist of a set of commands for the listed utilities.
Ready-made script
Attention!
The script does not work properly with files that have Cyrillic characters in their names.- Or to the directory specified after the
-ooption. - Or to the
outputsubdirectory of the current directory, if the-okey was not specified.
To optimize JPG/JPEG and PNG files, you can use this Bash script.
The script is downloaded and run using the following commands:
wget https://gist.githubusercontent.com/lgiraudel/6065155/raw/24f667559eee61dd00a99a9940e06b46a125d3ec/optimize.sh
sh optimize.sh -i ~/example.com/images/input -o ~/example.com/images/output
Parameters:
-ior--input— directory containing the source images.-oor--output— directory where all optimized images will be saved.-qor--quiet— disabling the display of information during operation.-sor--no-stats— disabling the display of size statistics-hor--help— displaying information about the specified keys.
The script uses the following utilities: pngcrush, optipng, and jpegtran.
Utilities
pngcrush options source_file.png optimized_file.png
Ways to use the utility:
- Maximum compression without loss of quality:
pngcrush -rem alla -rem text -reduce -brute source_file.png optimized_file.png - Optimizing files in a directory without moving them:
for file in ~/example.com/www/images/*.png ; do pngcrush -reduce -brute -rem alla -rem gAMA -rem cHRM -rem iCCP -rem sRGB "$file" "${file%.png}-crushed.png" && mv "${file%.png}-crushed.png" "$file" ; done- Instead of
example.com/www/images, specify the path to the directory containing the images. Please note that optimization is not performed recursively across all directories; it is limited to the specified directory only. - The final optimized file will replace the source file.
Detailed instructions for using the utility:
pngcrush --help
- PNG
- BMP
- GIF
- PNM
- TIFF
Optimize a single image (the image file will be replaced with an optimized version):
optipng ~/example.com/www/images/image.png
Optimizing all images in a specific folder:
find ~/example.com/www/images/ -iname *.png -print0 | xargs -0 optipng -o7
- Instead of
example.com/www/images/, specify the path to the images directory. - Instead of
*.png, specify the desired file format, such as*.bmpor others.
Detailed instructions for using the utility:
optipng --help
Optimizing GIF animations with some loss of quality:
gifsicle -03 --lossy=80 -o optimized_file.gif source_file.gif
- To specify the compression level, change the value of the
–lossy=XXparameter.
Detailed instructions for using the utility:
gifsicle --help
Common ways to use the utility:
- Optimizing a single image:
jpegoptim image.jpg- Optimize all
.jpgfiles in the directory:jpegoptim ~/example.com/www/images/*.jpg- Instead of
example.com/www/images/, specify the path to the images directory.
- Removing all image metadata:
jpegoptim image.jpg --strip-all - Conversion to progressive JPEG:
jpegoptim image.jpg --all-progressive - Image optimization with loss of quality:
jpegoptim -m85 image.jpg- To control the quality level of the optimized image, change the value of the
-mXXkey.
Detailed instructions for using the utility:
jpegoptim --help
Common ways to use the utility:
- Optimizing a single image:
jpegtran -copy none -optimize -outfile optimized_file.jpg source_file.jpg- Image metadata is removed using the
-copy nonekey.
- Optimization and conversion to progressive JPEG:
jpegtran -copy none -optimize -progressive -outfile optimized_file.jpg source_file.jpg
Detailed instructions for using the utility:
jpegtran --help