Archive
Tag "PEAR"

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

?>

Read More