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 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.
1 |
/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.
1 |
/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' |
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.