[ Home ] [ About ][ Posts ][ Git ][ Contact ][ Links ][ Bookmarks ]

How to optimize image for websites from the command line

Estimated reading time: 2 minutes

When hosting your website with a lot of images, your users need a lot of bandwidth just to load some images, but most of the time the image doesn’t need to be that big for the user to load. Here is a guide on how to optimize images for these kind of situations, credits to Eric Murphy. I typed the video in text format as it is such a good video that I want to take notes of that.

  1. Install imagemagick
sudo pacman -S imagemagick

Optimise JPGs

convert image.jpg -resize 400x400 image.min.jpg # resize image as you don't always need that big
convert image.jpg -quality 75 image.min.jpg # reduce quality of image, 75% is a good compromise between file size and quality
convert image.jpg -strip image.min.jpg # remove metadata (eg camera details, location, etc.)

convert image.jpg -sampling-factor 4:2:0 -strip -quality 75 -interlace JPEG -colorspace sRGB -resize 0 converted.jpg # best for web quality wise

You can make it into a shell script


convert $1 -sampling-factor 4:2:0 -strip -quality 75 -interlace JPEG -colorspace sRGB -resize $2 $3

Optimise PNGs

Mostly same as JPGs

Optimise batch of files at a time

mogrify -sampling-factor 4:2:0 -strip -quality 75 -interlace JPEG -colorspace sRGB -resize 400 -path "opt" *.jpg # converting all jpgs and put all converted jpgs into opt folder

Optimise SVGs

First install svgo with npm

npm i -g svgo

Then run

svgo logo.svg -o logo.min.svg

Batch convert SVGs

svgo -f ./assets/svgs -o ./assets/optimiszedsvgs # -f specifiying the directory and -o is the output directory