Zend - The PHP Company




Thumbnails

Add Code


Thumbnail Generating script using GD  

Type: application
Added by: CeleronDude
Entered: 19/01/2002
Last modified: 01/12/2001
Rating: - (fewer than 3 votes)
Views: 7310
This script reads all images in a directory and resize it to a specific width( the height will be resized but kept at the right ratio so the thumbnail will not be disfigured). Right now the script is set for jpegs only since my server doesn't support GIF yet. Simply remove the // and it should allow GIF support (that is if your GD supports it) THe script is configured to reads images/funny but you can change it. :)


<?
$src_dir 
"images/funny/";
$folder opendir($src_dir);
while (
$file readdir($folder)){
if(
stristr($file".jpg")){
$funny[] = $file;   }}
is_array($funny);
srand((double)microtime()*1000000); 
$rand_max sizeof($funny) - 1;
$random rand(0,$rand_max);
$dest_dir "thumbnails/";
$image $funny[$random];
$size GetImageSize("$src_dir$image");
$imagetype $size[2];
$image_w $size[0];
$image_h $size[1];
$new_w 150;
$ratio $image_w $new_w;
$new_h $image_h $ratio;
$imagename str_replace(".jpg"""$image);
$newimagename $imagename "_thumb.jpg";
if(
file_exists("$dest_dir$newimagename")) {
echo 
"<a href="showfunny.php?image=$image"><img src="$dest_dir$newimagename" border="0"></a>";} else {
switch (
$imagetype) {
case 
1:
echo 
"Gif type not supported with this GD version";
// Remove these // if you know your GD supports GIF types.  Remove the line above also.
//$imagename = str_replace(".gif", "", $image);
//$newimagename = $imagename . "_thumb.gif";
//$dest = ImageCreate ($new_w, $new_h);
//$src = imageCreateFromGif("$src_dir$image");
//ImageCopyResized($dest, $src, 0,  0,  0, 0, $new_w, $new_h, $image_w, $image_h);
//ImageGif($dest,"$newimagename");
//ImageDestroy($dest);
//copy($newimagename, "$dest_dir$newimagename");
//unlink($newimagename);
//echo "<img src="$dest_dir$newimagename">";
break;

case 
2:
$imagename str_replace(".jpg"""$image);
$newimagename $imagename "_thumb.jpg";
$dest ImageCreate ($new_w$new_h);
$src imageCreateFromJPEG("$src_dir$image");
ImageCopyResized($dest$src0,  0,  00$new_w$new_h$image_w$image_h);
ImageJpeg($dest,"$newimagename",80);
ImageDestroy($dest);
copy($newimagename"$dest_dir$newimagename");
unlink($newimagename);
echo 
"<a href="showfunny.php?image=$image"><img src="$dest_dir$newimagename" border="0"></a>";
break;

default:
echo 
"Not gif nor jpeg";
}
}
?>


Usage Example




Rate This Script





Search



This Category All Categories