views:

113

answers:

3

I have a huge volume of thumbnailing to do. Currently, I am using ImageMagick, but it's proving too inefficient (it's too slow, uses too much CPU/memory, etc.).

I have started to evaluate GraphicsMagick, which I expected to get "wow" results from. I didn't get them. Can someone take a quick look at my benchmark script (does simple speed and file size comparison only; no CPU and memory checks yet):

http://pastebin.com/2gP7Eaxc

Here's a sample output I got:

'gm convert' took 75.0039 seconds to execute 10 iteration(s).
'convert' took 83.1421 seconds to execute 10 iteration(s).
Average filesize of gm convert: 144,588 bytes.
Average filesize of convert: 81,194 bytes. 

GraphicsMagick is not that much faster -- and the outputted file sizes are SIGNIFICANTLY higher than ImageMagick.

A: 

For on-the-fly thumbnail generation, I use gd. A how-to is provided here:

http://icant.co.uk/articles/phpthumbnails/

For a one-time conversion batch, I use the lightweight and free image viewing program, Irfanview. It has a batch conversion feature which provides many options for controlling resampling methods and compression level.

JYelton
GD2 here. But it's pretty RAM-hungry, my host sometimes refuses to cooperate ;)
mingos
A: 

I you want to use GD2, try this function I use. It's pretty easy to use:

function scaleImage($source, $max_width, $max_height, $destination) {
    list($width, $height) = getimagesize($source);
    if ($width > 150 || $height > 150) {
    $ratioh = $max_height / $height;
    $ratiow = $max_width / $width;
    $ratio = min($ratioh, $ratiow);
    // New dimensions
    $newwidth = intval($ratio * $width);
    $newheight = intval($ratio * $height);

    $newImage = imagecreatetruecolor($newwidth, $newheight);

    $exts = array("gif", "jpg", "jpeg", "png");
    $pathInfo = pathinfo($source);
    $ext = trim(strtolower($pathInfo["extension"]));

    $sourceImage = null;

    // Generate source image depending on file type
    switch ($ext) {
        case "jpg":
        case "jpeg":
        $sourceImage = imagecreatefromjpeg($source);
        break;
        case "gif":
        $sourceImage = imagecreatefromgif($source);
        break;
        case "png":
        $sourceImage = imagecreatefrompng($source);
        break;
    }

    imagecopyresampled($newImage, $sourceImage, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

    // Output file depending on type
    switch ($ext) {
        case "jpg":
        case "jpeg":
        imagejpeg($newImage, $destination);
        break;
        case "gif":
        imagegif($newImage, $destination);
        break;
        case "png":
        imagepng($newImage, $destination);
        break;
    }
    }
}
SimpleCoder
How do I specify the pixel resolution, compression to use, etc.?
StackOverflowNewbie
To specify 65% quality (works on `imagejpeg`, `imagegif`, etc.): `imagejpeg($newImage, $destination, 65);`
SimpleCoder
Not sure about pixel resolution or compression specifically, but modifying the quality will indirectly control these.
SimpleCoder
+2  A: 

I assume you have some sort of queue of images that require thumbs and your app works through them? You could look at siphoning off some of the work to something like EC2. If your queue gets over a certain size fire up a pre-prepared EC2 instance to process the load instead. You could even fire up several machines if the queue was massive.

You don't need these instances to run all the time - you only need them when your own server isn't able to handle the load.

Obviously you'd need to forecast your costs to see if it was worth it but given you only pay for the time you use and the prices start at 8.5c/hour it might be economical enough for your needs.

rojoca
I'm surprised there isn't a public AMI for thumbnail generation. Seems like a good project to learn EC2 with if anybody's looking for one :-)
dkamins