Dynamic Thumbnailing with Imagemagick in PHP

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.';
}

?>

This entry was posted in Miscellaneous and tagged , , , , . Bookmark the permalink.

11 Responses to Dynamic Thumbnailing with Imagemagick in PHP

  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

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>