Create miniatures of pictures with ImageMagick

2 eggs, a small one on the left and a big one on the
right A while ago, I started to add some illustration pictures to my blog posts, usually on the start. It's now part of the fun of writing a blog post : I like that moment where I'm looking for something relevant to the article but find something totally different (yet still relevant). For example, I hoped to find a picture where we could see 2 cars, the regular one on one side, and the miniature toy on the other. I guess the eggs will do just fine. But this is not about eggs...

The fun stops at the moment I get the picture but want to make it smaller, like 230 pixels wide. I could just go with the full size and render it smaller with CSS, but, hey, I'm thinking about low-bandwith connections, and I'm a GTmetrix junkie (mostly the latter).

At the beginning of all this, I took the habit of firing up Gimp, and resize the picture. But this takes too long. This is too manual. I always want the same thing : the picture gets resized to a 230 pixels width, and automatic height so it keeps its proportions. This can be... automated ?

Turns out, there's an app for that : ImageMagick. This tool makes wonders. Not only when used behind a PHP module, but also on the command-line. Back to my egg thing, I then just need to invoke the magick tool (pun intended) :

magick mogrify -resize 230 -format jpg -write
danielle-levis-pelusi-4mpsEm3EGak-unsplash_mini.jpg daniele-levis-pelusi-4mpsEm3EGak-unsplash.jpg

So, this command uses the mogrify sub-program to resize the image to a 230 pixels width, without specifying the height, which gets handled automagically. The output format is specified, and I want to write the output to another file (because, we never know, I might want to do something else with the source image. Of course mogrify can do more, and is well documented. Looks like it does the job, right ? I'm not finished yet. I can imagine a lot of things when I'm lazy, and I feel super-lazy.

So here comes the minigen.sh awesome script, the result of years of engineering by the top people from... no, actually it took me about 15 minutes and a quick visit to Stack Overflow because I can't remember the shell parameter expansion capabilities. So here it is :

#! /usr/bin/env bash

source_path=${1}
file_name=$(basename -- "${source_path}")
dir_name=$(dirname -- "${source_path}")
extension="${file_name##*.}"
file_noext="${file_name%.*}"

magick mogrify -resize 230 -format ${extension} -write
${dir_name}/${file_noext}_mini.${extension} ${source_path}

This script :

  • uses the path to the source file to write the target file;
  • appends a suffix to the name, before the extension;
  • keeps the format of the image;
  • and does the resizing stuff.

I could do more, like checking the input is actually an image file, but I'm pretty sure ImageMagick will do this better than me.

I hope you enjoyed this post ! If you did, please share it on your favorite social networks :-)

Photo by Daniele Levis Pelusi on Unsplash.