rss

Self Picture

Natural Language Processing, Information Extraction and Search consultant.

 learn more   get in touch 

Logo - I Build Search
Sep 09
2009

Using PHP and ImageMagick to resize images digg

Today, I had to write some code to generate thumbnails in PHP. The php-gd library wasn’t installed and I had to work with ImageMagick. Not the most elegant of solutions, but it works:

functions.php
1
2
3
4
5
6
7
8
9
10
11
define('PRAVIN_THUMBNAIL_DIR', '/home/firedev/public_html/wp-content/cache/thumbnails/');
function pravin_resize($img_path, $width, $height) {
    $resolution = '"' . $width . 'x' . $height . '"';
    $output_path = PRAVIN_THUMBNAIL_DIR . md5($img_path) . "-$resolution.jpg";
    // If file does not exist OR the thumbnail was generated more than 
    // 5 mins (5 x 60 sec) then re-create the thumbnail
    if(!file_exists($output_path) || (time() - filemtime($output_path)) > (5 * 60)) {
        system("/usr/bin/convert -resize $resolution $img_path $output_path");
    }
    return $output_path;
}

One Response (rss) (trackback)

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

This page and its contents are copyright © 2010, Pravin Paratey.