Zend - The PHP Company




Thumbnails

Add Code


lib_image.php  

Type: code fragment
Added by: iGoR75
Entered: 16/05/2002
Last modified: 05/12/2001
Rating: - (fewer than 3 votes)
Views: 7754
A Function which allows you to create proportional Thumbnails with some features: works with JPG/Gif requires GD 1.6 or higher - grayscale [on | off] - Border with Bordercolor [on | off] - antialiasing [on | off] (GD 2.x, JPG only) - interlace [on | off] (nice effect)


<?
#####################################################################################
#                        Class lib_image - Philipp Wiegel 2002
#                        mailto: pwiegel@visio-interactive.de
#                        http://www.visio-interactive.de
#Usage: createthumb( Resource File with absolut path,
#             Destination file with absolut path,
#             Proportional width (height is calculated),
#             Bordersize in Pixel e.g. 1
#             Bordercolor in HEX! e.g. 000000  without "#"
#             antialias [0|1],
#             gray [0|1] for grayscaling the Thumb,
#             rasterize [0|1] nice looking effect with interlaced lines);
#
#    Email me with questions and comments: pwiegel@visio-interactive.de
#    known BUG: bordercolors sometimes go crazy..... no time to fix this, maybe
#    anyone could do this. Don't forget to mail the code back to me then :)
#####################################################################################
    
    
function hex2rgb($hex)
    {
        
$r=substr($hex,0,2);
        
$g=substr($hex,2,2);
        
$b=substr($hex,4,2);
        
$a[1]=hexdec($r);
        
$a[2]=hexdec($g);
        
$a[3]=hexdec($b);
        return 
$a;
    }
    function 
create_thumb($resource$dest$x_scale$bordersize NULL$bordercolor NULL$antialias NULL$gray NULL$rasterize NULL)
    {
            
$check     GetImageSize($resource);
            
$factor $check[0]/$check[1];
            
$y_scalefloor($x_scale/$factor);
            
$im_destImageCreate($x_scale,$y_scale);
            if (
$check[2] == "2")
            {    
$im_src ImageCreateFromJPEG($resource); }
            if (
$check[2] == "1")
            {    
$im_src ImageCreateFromGIF($resource); }
            
            if (
$im_src)
            {
                  if (isset(
$antialias) and $antialias == "1")
                { 
ImageCopyResampled($im_dest,$im_src,0,0,0,0,$x_scale,$y_scale,$check[0],$check[1]); }
                else
                { 
ImageCopyResized($im_dest,$im_src,0,0,0,0,$x_scale,$y_scale,$check[0],$check[1]); }
                if (isset(
$bordersize) and !empty($bordersize))
                {
                  if (isset(
$bordercolor) and !empty($bordercolor))
                  { 
                      
$col hex2rgb($bordercolor); 
                    
$col ImageColorAllocate($im_dest,$col[1],$col[2],$col[3]);
                    
ImageRectangle($im_dest,0,0,$x_scale-1,$y_scale-1,$col);
                  }
                  }
                if (isset(
$gray) and $gray == "1") { ImageCopyMergeGray($im_dest,$im_dest,0,0,$x_scale,$y_scale,$check[0],$check[1],30); }
                
                if (isset(
$rasterize) and $rasterize == "1"
                { 
                    for (
$i=2$i<=$y_scale-2$i++)
                    { 
ImageLine($im_dest,0,$i,$x_scale,$i,10); $i++; }
                }
            }
            if (
$check[2] == "2")
            { if (
ImageJPEG($im_dest,$dest)) { return true; } }
            if (
$check[2] == "1")
            { if (
ImageGIF($im_dest,$dest)) { return true; } }
            
            
ImageDestroy($im_dest);
            
ImageDestroy($im_src);
    }
?>


Usage Example


createthumb("test.jpg","test_thumb.jpg",1,"000000",0,1,1);


Rate This Script





Search



This Category All Categories