rss
logo

I provide consulting and custom development for Natural Language Processing, Information Extraction and Search solutions.Self Picture


 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="">

Latest Articles

Feb
19

Join a list of integers in Python

How do you run a string join on a list of integers in Python? After googling for about 10 mins, I gave up and did this. I am sure there is a better way of doing it! [Read More]
Jan
21

Writing a spider in 10 mins using Scrapy

I came across Scrapy a few days back and have grown to really love it. This tutorial will illustrate how you can write a simple spider using Scrapy to scrape data off Paul Smith. All this in 10 minutes. [Read More]

Featured Projects

Yahoo Messenger Client for *nix

Yahoo Messenger Client for *nix

Yux is an alternative Yahoo Messenger client for *nix systems that attempts to match the look and feel of the original Windows client.

[Read More]

Document Tagger

Document Tagger

DocTagger lets you automatically classify text documents. Use this as a starting point to write apps that can sort through volumes of unorganized data.

[Read More]

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