Adding Annotation To Cropped Thumbnails Using ImageMagick
You’ve probably seen them before, generally done by imagehosters. Yep, that’s them. The little thumbnails with the resolution & file size written in them. Well I’m going to show you how to make them using ImageMagick & PHP. If you don’t have ImageMagick you can find out how to install it in my previous tutorial [...]
You’ve probably seen them before, generally done by imagehosters. Yep, that’s them. The little thumbnails with the resolution & file size written in them. Well I’m going to show you how to make them using ImageMagick & PHP. If you don’t have ImageMagick you can find out how to install it in my previous tutorial here. Just ignore the part about installing Imagick, the instructions are for Dreamhost but it’s pretty much the same.
Cropping The Thumbnails
First let’s go back over how to make square thumbnails, which I did in an earlier tutorial.
/path/to/convert -limit memory 50mb -limit map 128mb -size 120x108 'path/to/file.jpg' -thumbnail 120x108^ -gravity center -extent 120x108 'path/to/output.jpg'
Let’s go over that a little. I’ve added the memory & map limits onto the command because I am on a shared server with a 90MB memory limit. If it reaches these limits it will start to dump to HDD instead so make sure you have plenty HDD space on the server. Now your probably going to say “Woah! They aren’t square, are you stupid?” The answer is no. The missing amount is so we can add the annotation onto the picture. Also if you want to change where the crop starts you can change -gravity center to north or south.
Adding The Annotation
Now let’s add on the annotation command.
/path/to/convert -limit memory 50mb -limit map 128mb -size 120x108 'path/to/file.jpg' -thumbnail 120x108^ -gravity center -extent 120x108 -gravity south -background black -fill '#cccccc' -font '/path/to/font.ttf' -pointsize 8 -splice 0x12 -annotate +0+1 '%[width]x%[height] %[size]' 'path/to/output.jpg'As you can see the start is the command from before, but now we have an extra bit. We reset the gravity to south, change this to north if you want the annotation on the top of the image. We make the background black (you can use hex between single quotes) and then we fill the text with an off white. I personally don’t like ImageMagick’s default fonts so I have uploaded a font to the server & used that. If you are going to use it you need to specify it from root & you must have the license to use the font if one is needed. Next we set the pointsize, we splice on 12 pixels and add the annotation with 1 pixel of padding from the bottom. Finally we set what the annotation should say. These are special variables that are preset by ImageMagick when an image is loaded. They hold the original image width & height, the x is just for a seperator. The size is the size of the original image. Finally we set the output path again.
That’s it. You should now have a thumbnail that looks something like the one in figure one. If you don’t check you’ve wrote or copied everything properly first, if you have and you still have problems give me a shout & I’ll help you as much as I can.
Edit: Oh My God… I can’t believe what I’ve done. If you’ve noticed well done you. I’ve made the thumbnails 120×106 and then added 12 pixels on. That isn’t square that’s 120×118. Yes, I know. At least I’ve admited I made a mistake. I’ve altered the sizes to be right now. That’ll teach me for getting a D in my math GCSE.


Leave a comment