Page 1 of 1

Scaling imagemagick usage

Posted: 2008-07-05T01:42:36-07:00
by Bemmu
I'm looking for ideas on how to optimize my usage of ImageMagick or switch to something else. Here's basically the list of steps I have to do:

- Create a bunch of separate text annotations (in possibly different colors and fonts) that beautifully fits a given space. Currently I do this by making those annotations in a very large point size and then scaling the image down to fit the desired dimensions.
- Put a PNG/GIF background image under the text created in previous step. I accomplish this by having the annotations in the previous step happen on a transparent background, then I overlay that scaled text over the background image in this step.
- Write out as JPEG.

Example: http://tinyurl.com/5wx6nx

These together end up taking anything from 600ms upwards, which is intolerable because I have to serve 20+ such images per second. Currently I am renting 20 Amazon EC2 servers to keep up with the demand, but this certainly is no long term solution! Any ideas?

I'm using command line now in that 600ms+ case. I also did an experiment using the PHP Imagick API, but there seems to be no major speed difference. Got 300ms in my test for just making a single ~1000x100 text and encoding it as PNG. Making identical image took 100ms for GD, but I'm reluctant to use GD as it seems more limited, and still 100ms is pretty slow.

One thing I've been considering is having a font cache like XWindows has and keeping all the characters in memory, then just blit them from in-memory surfaces, scale the result and encode as PNG. I'm really looking for an order of magnitude type improvement here, if I could somehow get this to <50ms then I would only need one server, which would make it much simpler and cheaper too.

Re: Scaling imagemagick usage

Posted: 2008-07-06T10:57:14-07:00
by Bemmu
Okay, decided to try writing a lighttpd plugin that uses libjpeg. Thanks for all that read my post!

Re: Scaling imagemagick usage

Posted: 2008-07-06T23:41:07-07:00
by anthony
To fit a annotation to fit a particular size you can use the 'scaling' method described, but you may find that the font will look rather 'thin' in places.

Basically fonts are drawn to bet fit the pixel resolution being used, which effects its handling of 'large' pixels. See Im Examples...
http://imagemagick.org/Usage/text/#pointsize

for better results. Use label: without any -pointsize, but instead a -size setting of the area in which you want the font to fit. You can then overlay that onto the image in the appropriate place.
http://imagemagick.org/Usage/text/#label