People often ask how we do what we do. This series explains…
We use book covers all over the Pragmatic online store: on the book pages, in the order summary, on your bookshelf listing, and so on. We display the images in five different sizes, from 40x48 thumbnails up to full-size 72dpi images. And, to make life even more interesting, we overlay those images with variable little icons to show that the book is in beta, that an eBook update is available, and so on.
Being pragmatic (read “lazy”) we didn’t want to manage all those combinations manually, so Mike Clark and John Long cooked up the software and CSS to make it all automatic.
It all starts on my desktop machine, where I use InDesign to create the covers that I’ll send to the printer. As a part of that process, I export full-size 72dpi and 300dpi JPEG versions of the front cover. Our production folks check these images out of our repository and upload them to our admin system.
Perhaps surprisingly for those used to modern Rails, we don’t use a library such as PaperClip to do this upload. Even if it had been around when we wrote the store, we do enough custom processing that I’m not sure it would have added much value. Instead we just use a raw file_field_tag to upload the full-size JPEG.
We save the original covers away—you can view them by clicking on the large cover image on the main page for each title. We then feed the cover through ImageMagick to create the scaled versions. We could have done this using library calls, but we were concerned about reported memory leaks, and so preferred to shell out:
Next, we create the versions of the covers that have the eBook, beta, and the PDF-out-of-date stamps. There’s a version of each stamp for each different cover size (we tried scaling them down from a large master, but they looked horrible). Here’s the code that generates the two sizes of cover with the eBook stamp:
The code that does the overlaying uses some RMagick magic:
A little processing up front, and we have 16 versions of a book cover all ready to serve in the store. A wee post-deploy hook symlinks the master covers directory into the application each time we update it.
Dave Thomas is one of the Pragmatic Programmers.