Zend - The PHP Company




Thumbnails

Add Code


KY JPEG Thumbnail Generator  

Type: application
Added by: kadir
Entered: 11/11/2000
Last modified: 01/12/2000
Rating: *** (3 votes)
Views: 11365
Generates and saves proportional thumbnails of JPEG images.


<?php

#    +-------------------------------------------------+
#    |  J P G   T h u m b n a i l   G e n e r a t o r  |
#    +--------------------+----------------------------+
#    |  by Kadir Yazgan   |  ( kadiryazgan@ixir.com )  |
#    +--------------------+----------------------------+
#       v1.0
#
#    Generates a proportional thumbnail image with a given width.
#
#    This script only uses JPEG files. If required libraries are included GIF and PNG files can 
#    also be supported the same way.
#    
#
#    P a r a m e t e r s
#        $imgdir:    Directory where original images reside.
#        $tndir:        Directory where the thumbnails will be saved.
#        $tn_w:        Thumbnail width.


#-+ Include the GD library.
    
dl("php_gd.dll");

#-+ Define the original image
    
if (!isset($img))
    {
        
$img "default.jpg";
    }

#-+ Directory where original images reside
    
$imgdir "90/images/";

#-+ Directory where the thumbnails will be saved
    
$tndir "90/images/tn/";

#-+ Thumbnail width
    
$tn_w 100;
?>



<html>
<head>
<title> KY JPEG Thumbnail Generator </title>
</head>

<body>
<?php

#-+ Check if the file exists
    
if (!file_exists($imgdir.$img))
    {
        die (
"Error: File not found...");
    }

#-+ Check if the file extesion is .jpg
    
$ext explode('.'$img); 
    
$ext $ext[count($ext)-1]; 
    if (
strtolower($ext) != "jpg")
    {
        die (
"Error: File must be JPEG");
    }

#-+ Read the source image
    
$src_img ImageCreateFromJPEG($imgdir.$img);

#-+ Get image width and height
    
$org_h imagesy($src_img);
    
$org_w imagesx($src_img);

#-+ Calculate thumbnail height
    
$tn_h floor($tn_w $org_h $org_w);

#-+ Initialize destination image
    
$dst_img ImageCreate($tn_w,$tn_h);

#-+ Do it!
    
ImageCopyResized($dst_img$src_img0000$tn_w$tn_h$org_w$org_h); 

#-+ Save it!
    
ImageJPEG($dst_img$tndir.$img); 

#-+ Print report
    
echo "<h1>KY JPEG Thumbnail Generator</h1>";
    echo 
"Original dimensions: $org_w x $org_h<br>n";
    echo 
"Thumbnail dimensions: $tn_w x $tn_h<br>n";
    
printf ("<a href="%s"><img src="%s" alt="Click to view the original image"></a>"$imgdir.$img$tndir.$img);

?>
</body>
</html>


Usage Example




Rate This Script





Search



This Category All Categories