Zend - The PHP Company




Thumbnails

Add Code


yaThumbnail Functions  

Type: code fragment
Added by: majinz
Entered: 25/04/2002
Last modified: 05/12/2001
Rating: - (fewer than 3 votes)
Views: 6136
This Code gives you 2 new powerful Functions. The first is an on-the-fly Thumbnailer. The second Funktion generates also an Thumbnail on the fly, but the Script saves the Thumbnail on the server. So the Script gets very fast. The Script uses the GB Libary. If there are Bugs, mail me.


<?php
   
function printThumbnail($imgfile,$max_width=100,$max_height=100) {
       list(
$org_width,$org_height,$orgtype) = getimagesize($imgfile);
       
$div_width $org_width $max_width;
       
$div_height $org_height $max_height;
       if(
$div_width >= $div_height) {
           
$new_width $max_width;
           
$new_height round($org_height $div_width);
       }
       else {
           
$new_height $max_height;
           
$new_width round($org_width $div_height);
       }
       switch(
$orgtype) {
           case 
1$im imagecreatefromgif($imgfile); break;
           case 
2$im imagecreatefromjpeg($imgfile); break;
           case 
3$im imagecreatefrompng($imgfile); break;
       }
       if(
$im) {
           
$tn imagecreate($new_width,$new_height);
           if(
$tn) {
               
imagecopyrisized($tn,$im,0,0,0,0,$new_width,$new_height,$org_width,$org_height);
               switch(
$orgtype) {
                   case 
1header("Content-Type: image/gif"); imagegif($tn); break;
                   case 
2header("Content-Type: image/jpeg"); imagejpeg($tn); break;
                   case 
3header("Content-Type: image/png"); imagepng($tn); break;
               }
               
imagedestroy($tn);
           }
       }
   }
   function 
saveThumbnail($imgfile,$thfile,$max_width=100,$max_height=100,$jpgq=60) {
       list(
$org_width,$org_height,$orgtype) = getimagesize($imgfile);
       
$div_width $org_width $max_width;
       
$div_height $org_height $max_height;
       if(
$div_width >= $div_height) {
           
$new_width $max_width;
           
$new_height round($org_height $div_width);
       }
       else {
           
$new_height $max_height;
           
$new_width round($org_width $div_height);
       }
       switch(
$orgtype) {
           case 
1$im imagecreatefromgif($imgfile); break;
           case 
2$im imagecreatefromjpeg($imgfile); break;
           case 
3$im imagecreatefrompng($imgfile); break;
       }
       if(
$im) {
           
$tn imagecreate($new_width,$new_height);
           if(
$tn) {
               
imagecopyresized($tn,$im,0,0,0,0,$new_width,$new_height,$org_width,$org_height);
               switch(
$orgtype) {
                   case 
1imagegif($tn,$thfile); return 1; break;
                   case 
2imagejpeg($tn,$thfile,$jpgq); return 2; break;
                   case 
3imagepng($tn,$thfile); return 3; break;
               }
               
imagedestroy($tn);
           }
       }
   }
   function 
printSavedThumbnail($imgfile,$imgpath,$thpath,$max_width=100,$max_height=100,$jpgq=60) {
       if(!
file_exists($thpath.$imgfile)) {
           if(
$imgtype saveThumbnail($imgpath.$imgfile,$thpath.$imgfile,$max_width,$max_height,$jpgq))
               
printImageFile($imgtype,$thpath,$imgfile);
       }
       else {
           list(,,
$imgtype) = getimagesize($thpath.$imgfile);
           if(
$imgtypeprintImageFile($imgtype,$thpath.$imgfile);
       }
   }
   function 
printImageFile($imgtype,$imgfile) {
       switch(
$imgtype) {
           case 
1header("Content-Type: image/gif"); break;
           case 
2header("Content-Type: image/jpeg"); break;
           case 
3header("Content-Type: image/png"); break;
       }
       
$fh fopen($imgfile,"r");
       if(
$fh) {
           
fpassthru($fh);
           
fclose($fh);
       }
   }
?>


Usage Example


   // The Path to the Image dir.
   $imgpath = "./images/";
   // The Path to the Thumbnail dir (where the Thumbnails are saved)
   $thpath = "./images/thumb/";
   // The Max. Width
   $max_width = 200;
   // The Max. Height
   $max_height = 200;
   // If the Image is the JPEG Format, set here the Quality.
   $jpeg_qual = 100;
   
   // This is the On-the-Fly Method
   if(file_exists($imgpath."logo.jpg")) printThumbnail("logo.jpg");

   // OR use the this Method. Its better because it is faster then on-the-fly!
   if(file_exists($imgpath."logo.jpg")) printSavedThumbnail("logo.jpg",$imgpath,$thpath,$max_width,$max_height,$jpeg_qual);


Rate This Script





Search



This Category All Categories