Jul 2, 2012

Create Thumbnail

To generate thumbnail, here is the  simple and easiest way. It will help you in creating thumb for jpg, png and gif images.


/* generateThumb()
 *
 * @param mixed $source Path to source file
 * @param mixed $destination Path to destination file
 * @param mixed $width Thumbnail file width
 * @param mixed $height Thumbnail file height
 * @return bool
 */
function generateThumb($source, $destination, $width = 100, $height = 100){ $ext = strtolower(substr($source, strrpos($source, ".")+1)); $format = ($ext == 'jpg')?'jpeg':$ext; $from_format = "imagecreatefrom".$format; $source_image = $from_format ( $source ); $source_width = imagesx ( $source_image ); $source_height = imagesy ( $source_image ); $ratio1 = $source_width/ $width; $ratio2 = $source_height / $height; if($ratio1 > $ratio2){ $width = $width; $height = $source_height/$ratio1; }else{ $width = $source_width/$ratio2; $height = $height; } $target_image = imagecreatetruecolor($width,$height); imagecopyresampled($target_image, $source_image, 0, 0, 0, 0, $width, $height, $source_width, $source_height); $image_function = "image" . $format; return $image_function ( $target_image, $destination, 100 ); }
Here is how you use it...


if(generateThumb("images/a.jpg", "images/thumbs/a.jpg", 150, 100)){ echo "done"; } else { echo "failed"; }

No comments:

Post a Comment

Want to tell something about this post. Please feel free to write...