Dynamic Thumbnailing with Imagemagick in PHP

Written by Shanx May 27th, 2003

Dynamic Thumbnailing with Imagemagick in PHP

Continue reading →
Close

Here is some sample code that allows you to create a thumbnail automatically (assuming ImageMagick is already installed and functional) (based on the imagick PEAR module).

Code for dynamic thumbnails using PHP

<?php

// Specify your file details
$current_file = 'image.jpg';
$max_width = '150';

// Get the current info on the file
$current_size = getimagesize($current_file);
$current_img_width = $current_size[0];
$current_img_height = $current_size[1];
$image_base = explode('.', $current_file);

// This part gets the new thumbnail name
$image_basename = $image_base[0];
$image_ext = $image_base[1];
$thumb_name = $image_basename.'-th.'.$image_ext;

// Determine if the image actually needs to be resized
// and if it does, get the new height for it
if ($current_img_width > $max_width)
{
  $too_big_diff_ratio = $current_img_width/$max_width;
  $new_img_width = $max_width;
  $new_img_height = round($current_img_height/$too_big_diff_ratio);
  // Convert the file
  $make_magick = system("convert -geometry $new_img_width x $new_img_height $current_file $thumb_name", $retval);
  // Did it work?
  if (!($retval)) {
    echo 'Thumbnail created: <img src="' .$thumb_name .'">';
  }
  else {
    echo 'Error: Please try again.';
  }
}
else
{
  echo 'No need to resize.';
}

?>

11 Comments

  1. ralph hardwick says:

    I get a division by zero error…any ideas?

  2. Thumbnews says:

    I also get a division by zero error. Need help.

  3. nick says:

    That means you didn’t have a file to open/convert

  4. Jason says:

    this code does not work.

    instead of:

    “system(“convert -geometry $new_img_width x $new_img_height $current_file $thumb_name”, $retval);”

    it needs to be:

    “exec(“convert -geometry $new_img_width x $new_img_height $current_file $thumb_name”, $retval);”

  5. namdev says:

    It gives me error

    Warning: getimagesize
    No need to resize

    i think problem is with
    // This part gets the new thumbnail name
    $image_basename = $image_base[0];
    $image_ext = $image_base[1];

    $thumb_name = $image_basename.’-th.’.$image_ext;

  6. Sasha says:

    Generated file is too big.

    Better to use -thumbnail like this:

    $make_magick = exec(“convert -thumbnail $new_img_width x $new_img_height $current_file $thumb_name”, $retval);

  7. Ahsan says:

    I am using ur code on windows xp and it works fine if i put “dir” in the system command but when i write the convert command it doesn’t do anything. I tried the command in cmd in the same folder and it works great but not when executed from the php file. Please tell were am i going wrong?

  8. Kristin says:

    When I run the file… it gets all the way to “Thumbnail Created” but shows an x’d out image… and when i check the directory the file isn’t actually created… any ideas?

  9. freestream says:

    Well you can do it like that, but if you ever what it to work you might want to test this insted:

  10. freestream says:

    // Specify your file details
    $current_file = “P1010204.JPG”;
    $max_width = “150″;

    // Get the current info on the file
    $current_size = getimagesize($current_file);
    $current_img_width = $current_size[0];
    $current_img_height = $current_size[1];
    $image_base = explode(“.”, $current_file);

    // This part gets the new thumbnail name
    $image_basename = $image_base[0];
    $image_ext = $image_base[1];
    $thumb_name = $image_basename.”-th.”.$image_ext;

    // Determine if the image actually needs to be resized
    // and if it does, get the new height for it
    if ($current_img_width > $max_width)
    {
    $too_big_diff_ratio = $current_img_width/$max_width;
    $new_img_width = $max_width;
    $new_img_height = round($current_img_height/$too_big_diff_ratio);

    // Convert the file
    $make_magick = system(“convert -geometry ” . $new_img_width . “x” . $new_img_height . ” ” . $current_file . ” ” . $thumb_name, $retval);

    // Did it work?
    if (!($retval))
    {
    echo “Thumbnail created: “;
    }
    else
    {
    echo “Error: Please try again.”;
    }
    }
    else
    {
    echo “No need to resize.”;
    }

  11. hermann says:

    Works like a charm, but it’s very slow.
    Could it be improved?

Leave a Reply

Miscellaneous

I use the Nokia e61i as my mobile. Instead of my telco’s data plan (which offers me a meagre 1GB per month) I simply prefer to use my home wireless [...]

Continue reading →

View all

Web Tools

If you use Firefox (and if not, what are you waiting for?) you are familiar with useful extensions such as Video Downloader, which allow you to save local copies of [...]

Continue reading →

View all

Databases

This regexp worked for me. SELECT * FROM table WHERE NOT column ~ ( ‘^(‘|| $$[\09\0A\0D\x20-\x7E]|$$|| — ASCII $$[\xC2-\xDF][\x80-\xBF]|$$|| — non-overlong 2-byte $$\xE0[\xA0-\xBF][\x80-\xBF]|$$|| — excluding overlongs $$[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}|$$|| — straight 3-byte [...]

Continue reading →

View all

Windows

So you’ve been visited by the much dreaded CRC — Cyclical Redundancy Check error, most likely encountered while copying files between hard disks. On Mac OSX, this will usually appear [...]

Continue reading →

View all

Mac OSX

A simple app ought to do it. Download iRinger. It’s a Windows app. If you’re on Mac, you’ll want to use it within a virtual machine, like Parallels or VMWare [...]

Continue reading →

View all

System Maintenance

I use the Nokia e61i as my mobile. Instead of my telco’s data plan (which offers me a meagre 1GB per month) I simply prefer to use my home wireless [...]

Continue reading →

View all

Wordpress

Among many new exciting features, WordPress 2.6 released the ability to store each and every revision of your posts, like an elaborate update history. Now this can be a pretty [...]

Continue reading →

View all

Audio/Video

Panic, the makers of some fantastic software such as Transmit or Panic, also have the most light-weight audio converter for the Mac OSX platform. It’s called Audion: get it here. [...]

Continue reading →

View all

iPhone

A simple app ought to do it. Download iRinger. It’s a Windows app. If you’re on Mac, you’ll want to use it within a virtual machine, like Parallels or VMWare [...]

Continue reading →

View all