Dynamic Thumbnailing with Imagemagick in PHP

Here is some sam­ple code that allows you to cre­ate a thumb­nail auto­mat­i­cally (assum­ing ImageMag­ick is already installed and func­tional) (based on the imag­ick PEAR module).

Code for dynamic thumb­nails 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.';
}

?>

  • ralph hard­wick

    I get a divi­sion by zero error…any ideas?

  • Thumb­news

    I also get a divi­sion by zero error. Need help.

  • nick

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

  • Jason

    this code does not work.

    instead of:

    "system("convert –geom­e­try $new_img_width x $new_img_height $current_file $thumb_name", $retval);"

    it needs to be:

    "exec("convert –geom­e­try $new_img_width x $new_img_height $current_file $thumb_name", $retval);"

  • namdev

    It gives me error

    Warn­ing: getim­a­ge­size
    No need to resize

    i think prob­lem is with
    // This part gets the new thumb­nail name
    $image_basename = $image_base[0];
    $image_ext = $image_base[1];

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

  • Sasha

    Gen­er­ated file is too big.

    Bet­ter to use –thumb­nail like this:

    $make_magick = exec("convert –thumb­nail $new_img_width x $new_img_height $current_file $thumb_name", $retval);

  • Ahsan

    I am using ur code on win­dows xp and it works fine if i put "dir" in the sys­tem com­mand but when i write the con­vert com­mand it doesn't do any­thing. I tried the com­mand in cmd in the same folder and it works great but not when exe­cuted from the php file. Please tell were am i going wrong?

  • Kristin

    When I run the file… it gets all the way to "Thumb­nail Cre­ated" but shows an x'd out image… and when i check the direc­tory the file isn't actu­ally cre­ated… any ideas?

  • freestream

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

  • freestream

    // Spec­ify your file details
    $current_file = "P1010204.JPG";
    $max_width = "150";

    // Get the cur­rent 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 thumb­nail name
    $image_basename = $image_base[0];
    $image_ext = $image_base[1];
    $thumb_name = $image_basename."-th.".$image_ext;

    // Deter­mine if the image actu­ally 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);

    // Con­vert the file
    $make_magick = system("convert –geom­e­try " . $new_img_width . "x" . $new_img_height . " " . $current_file . " " . $thumb_name, $retval);

    // Did it work?
    if (!($ret­val))
    {
    echo "Thumb­nail cre­ated: ";
    }
    else
    {
    echo "Error: Please try again.";
    }
    }
    else
    {
    echo "No need to resize.";
    }

  • her­mann

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