Skip to content

Instantly share code, notes, and snippets.

@bengarrett
Last active June 20, 2022 14:23
Show Gist options
  • Save bengarrett/a630fba5de1a68c6de6b to your computer and use it in GitHub Desktop.
Save bengarrett/a630fba5de1a68c6de6b to your computer and use it in GitHub Desktop.
Generate and optimise thumbnails using GraphicsMagick and pngquant

Generate and optimise thumbnails using GraphicsMagick and pngquant

Install

Install gm and pngquant.

apt-get install gm pngquant

Generate

Generate a thumbnail. You can replace -thumbnail with -resize for a clearer thumbnail that will result in a larger file size.

gm convert source_image.png -thumbnail 200x200 thumbnail_image.png

Generate a thumbnail while keeping horizonal aspect ratio.

gm convert source_image.png -thumbnail 200x thumbnail_image.png

Generate a thumbnail while keeping vertical aspect ratio.

gm convert source_image.png -thumbnail x200 thumbnail_image.png

Generate a thumbnail with a fix size and a fill-in background colour.

gm convert source_image.png -thumbnail 200x200 -background "#fff" -gravity center -extent 200x200 thumbnail_image.png

Optimise

Optimise thumbnail

pngquant --ext=.png -f thumbnail_image.png

Batch optimise thumbnails

find . -name "*.png" -print0 | xargs -0 sudo pngquant --ext=.png -f

GraphicsMagick. pngquant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment