...

View Full Version : Dynamic Thumbnails



Element
09-13-2005, 07:09 AM
This isn't a request for help, mearly for people looking for a simple way to do thumbnails without the actual thumbnail file.

It works via url for example thumbit.php?image=URL yes its ment to be used in a IMG tag.



<?php

// Input
$s_image = $_GET['image'];
$max_width = 300;
$max_height = 300;
$quality = 75;

//Resizing and Output
if (preg_match("/.jpg/i", "$s_image")) {

header('Content-type: image/jpeg');
list($width, $height) = getimagesize($s_image);
$ratio = ($width > $height) ? $max_width/$width : $max_height/$height;
if($width > $max_width || $height > $max_height) {
$new_width = $width * $ratio;
$new_height = $height * $ratio;
} else {
$new_width = $width;
$new_height = $height;
}
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($s_image);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image_p, null, $quality);
imagedestroy($image_p);

}
elseif (preg_match("/.png/i", "$s_image")) {

header('Content-type: image/png');
list($width, $height) = getimagesize($s_image);
$ratio = ($width > $height) ? $max_width/$width : $max_height/$height;
if($width > $max_width || $height > $max_height) {
$new_width = $width * $ratio;
$new_height = $height * $ratio;
} else {
$new_width = $width;
$new_height = $height;
}
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefrompng($s_image);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagepng($image_p, null, $quality);
imagedestroy($image_p);

}
elseif (preg_match("/.gif/i", "$s_image")) {

header('Content-type: image/gif');
list($width, $height) = getimagesize($s_image);
$ratio = ($width > $height) ? $max_width/$width : $max_height/$height;
if($width > $max_width || $height > $max_height) {
$new_width = $width * $ratio;
$new_height = $height * $ratio;
} else {
$new_width = $width;
$new_height = $height;
}
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromgif($s_image);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagegif($image_p, null, $quality);
imagedestroy($image_p);

}
else {

exit;

}

?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum