snippet library logo
in category
PHP / Image Manipulation / thumbnailing on the fly
Snippet details
ID: 512
Viewed: 1653
Added: 2003-09-30
Version: 1
Sorry no demo

User Rated at:
Rate This:
Snippets in this catagory         
Show Printable Version
here is a short little script that will take an image and create a thumbnail view of the same image. this will not just resize it but it creates a new image and makes it the correct size so it actually leaves the default image alone.

Browsers: all
GD Library: 2.0+

General Details
Snippet uploaded by: snippet
Email : webmaster@snippetlibrary.com
Snippet By: snippet

<!---Head--->
none

<!---Body--->


<?php 

    $error 
False;
    
$img_size 20;
    
$image "someimage.jpg";

    
// check if the 'image' parameter has been given 
    
if (!isset($image)){ // if not generate an error
        
$error True;
    }
    if(!
$bigimage=@imagecreatefromjpeg($image)){
    
$error true;         
    }
     
    if(
$error){ // generate an error image 
        
$img ImageCreate(123100);
        
$bg ImageColorAllocate($img255255255); 
        
$white ImageColorAllocate($img25500); 
        
ImageString($img53020"Picture"$white); 
        
ImageString($img54540"Not"$white); 
        
ImageString($img52560"Available"$white); 
        
ImagePNG($img); 
        
ImageDestroy($img); 
        exit(); 
    } 
     
    
$x=imageSX($bigimage); 
    
$y=imageSY($bigimage);         
     
    if(
$resize == 'no' || (($x $y) && ($x $img_size)) || (($y $x) && ($y $img_size))){ 

        
imagejpeg($bigimage); 
        exit(); 
    } 
     
    
// find the larger dimension 
    
if ($x>$y) {    // if it is the width then 
        
$dx 0;                    // the left side of the new image 
        
$w $img_size;                // the width of the new image 
        
$h = ($y $x) * $img_size;    // the height of the new image 
        
$dy 0;                    // the top of the new image 
    
} else {    // if the height is larger then 
        
$dy 0;                    // the top of the new image 
        
$h $img_size;                // the height of the new image 
        
$w = ($x $y) * $img_size;    // the width of the new image 
        
$dx 0;    // the left edgeof the new image 
    
}

    
$thumbimage =ImageCreateTrueColor($w$h);
    
ImageCopyResampled($thumbimage$bigimage$dx$dy00$w$h$x$y);

    
imagejpeg($tnimage);     

    
// release the memory used by the thumbnail image 
    
ImageDestroy($thumbimage);
     
    
// release the memory used by the original big image 
    
ImageDestroy($bigimage);

?>




No Reviews to show


Please completely fill out the form below if you want to review this snippet. All reviews are subject to validation.


Subject:

Reviewed By:

Write a review:





Terms of Conditions
Powered By
Avian Hosting
© 2005 snippetlibrary.com All Rights Reserved.