Resizing an image to the desired size and proportion sounds like a simple process, but I found it quite challenging using Preview since I don’t own a legitimate copy of Photoshop.
Fortunately I came across a nice tool that exists since the archaic times of computers (for me at least), since 1999, that gives me very handy commands to solve this workflow. It’s called ImageMagick.
An example workflow
Without leaving the terminal, I would like to make a small icon (eventually a base64 encoded one) of this image:
First step, install imagemagick with your favourite package manager.
Then download a Github Octocat to your PC:
wget https://assets-cdn.github.com/images/modules/logos_page/Octocat.png -O octocat.png
The image will be available under the name octocat.png.
I would like to have a bit more information about the format, dimensions etc. of this image:
identify octocat.png
Output:
octocat.png PNG 800x665 800x665+0+0 8-bit sRGB 43.7KB 0.000u 0:00.000
Ok, this is way to big for an icon, it’s 800 px wide and 665 px tall.
So it’s rectangular, that’s not usual for an icon.
So I check the documentation for convert (a small tool to convert/resize/etc images) with man convert
.
After some research and trial-and-error I found the command to resize the image, keep the aspect ratio without the excess whitespace, and to leave the PNG transparent.
The command with it’s arguments is the following:
convert # the utility
-resize 32x32 # new geometry/size of the image
-extent 32x32 # since resize will try to retain the aspect ratio, we extend the whitespace to this size
-gravity center # place the image centered horizontally and vertically
-background transparent # retain the transparent background
octocat.png # the input image
octocat_icon.png # the output image
optional: base64 encoding
You could link in your CSS to the asset itself.
But for further performance improvements, we can avoid one HTTP roundtrip with an inline base64 encoded image.
do not over do it, or Souders will haunt you
On my mac I have a tool called base64
bundled with OpenSSL.
We can use this to convert anything into a base64 encoded string.
base64 octocat_icon.png
And in your CSS (stylus) you could create an icon-class like this:
size_icon = 1em
[class^="icon"]
display inline-block
width size_icon
height @width
background-size 100%
background-repeat no-repeat
vertical-align middle
.icon-octocat
background-image url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAABmJLR0QAAAAAAAD5Q7t/AAAFwklEQVRYw+2XT4wbVx3HP+/NjMfjXXvH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