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 post is tagged imagemagick, imaging, PEAR, PHP, Tutorials

10 Comments
I get a division by zero error…any ideas?
I also get a division by zero error. Need help.
That means you didn’t have a file to open/convert
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);”
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;
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);
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?
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?
Well you can do it like that, but if you ever what it to work you might want to test this insted:
// 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.”; }
Incoming Links