<?php  
//    Image thumbnail0r (with an "0" :]) 
//    mike[@]filespanker.com 
// 
//    PARAMETERS: 
// 
//    img:   
//        The image file as absoulte to the web root 
//    h: 
//        The thumbnail's height.    Default: 30 
//    w: 
//        The thumbnail's width.    Default: 30 
//    mode: 
//        1          = stretch:      The image is resized to height and width 
//        0[default] = proportioned: The image is shrunken, but keeps proportions 
//    type: 
//        [optional] 
//        jpg         = JPEG 
//        gif        = GIF 
//        png        = PNG 
//        If this is not set, it is determined by its file extension. 
// 
//    NOTE: if only w or h is set in mode 0, the other size will be proportionally calculated
//
//
//    This script's functions rely completely on your gd lib version. 
// 
//    So, if I recall correctly: 
//        gd v1.5 or lower : GIF 
//        gd v1.6 or higher: PNG 
//        gd v1.8 or higher: PNG and JPEG   
// 
//    So, all three image types should never work on the same gd lib :[ 
//    You can thank Unisys for that. 
//     
//    Before mailing me, please actually look at the code.    
//Theres not much I could have really screwed up, and its probably an 
//    issue with your gd library.  Try up/downgrading it.   

// Configuration: 


//// CODE 
$type = @$HTTP_GET_VARS["type"];
$mode = @$HTTP_GET_VARS["mode"];
$h = @$HTTP_GET_VARS["h"];
$w = @$HTTP_GET_VARS["w"];
$img = @$HTTP_GET_VARS["img"];


if (!isset($w))  
    { 
    $w = "";  
    } 

if (!isset($h))  
    { 
    $h = "";  
    } 


SetType($mode,   'integer');  
SetType($w,      'integer');  
SetType($h,     'integer');  
SetType($img,     'string' );  

function percent($p, $w)  
    { 
    return (real)(100 * ($p / $w));  
    } 

function unpercent($percent, $whole)  
    {  
    return (real)(($percent * $whole) / 100);  
    }  

// Initialization 

// Make sure the file exists... 
$img = getenv("DOCUMENT_ROOT") . $img;
if (!file_exists($img))  
    {  
    echo "Error: could not find file: $img.";  
    exit();  
    }  

// If the user defined a type to use. 
if (!isset($type))  
    {  
    $ext = explode('.', $img);  
    $ext = $ext[count($ext)-1];  
        switch(strtolower($ext))  
            {  
            case 'jpeg'  :   
                $type = 'jpg';  
            break;  
            default :  
                $type = $ext;  
            break;  
            }  
    }  

// Create the image... 
switch (strtolower($type))  
    {  
   case 'jpg':
        $tmp = imagecreatefromjpeg($img);     
    break;  

    case 'gif':  
        $tmp = @imagecreatefromgif($img);     
    break;  
     
    case 'png':  
        $tmp = @imagecreatefrompng($img);     
    break;  
     
    default:  
        echo 'Error: Unrecognized image format.';  
        exit();  
    break;  
    }  

if ($tmp)  
    {  
    // Resize it 
     
    $ow  = imagesx ($tmp);    // Original image width 
    $oh  = imagesy ($tmp);    // Original image height 

    if ($mode)  
        {  
        // Just smash it up to fit the dimensions 
        if ($w == "") $w = 50;
        if ($h == "") $h = 50;
        $nw = $w;  
        $nh = $h;  
        }  
    else  
        {  
        // Make it proportional. 
        if ($w == "" || $h == "") {
        	//if only one is set (w or h) count the other one proportionally
        	if ($w == "") {
        		$nh = $h;
        		$nw = unpercent(percent($nh, $oh), $ow);
        	} else {
		        $nw  = $w;  
		        $nh = unpercent(percent($nw, $ow), $oh);          
        	}
        	
        } else {
        	
		//if w and h are set shrink to one of them
	        if ($ow > $oh)  
	            {  
	            $nw  = $w;  
	            $nh = unpercent(percent($nw, $ow), $oh);          
	            }  
	        else if ($oh > $ow)  
	            {  
	            $nh = $h;  
	            $nw = unpercent(percent($nh, $oh), $ow);  
	            }  
	        else  
	            {  
	            $nh = $h;  
	            $nw = $w;  
	            }  
        }
        }  

   //bug fix - sometimes (by more requests) nw or nh is not calculated
    if ($nw<=0) $nw=50;
    if ($nh<=0) $nh=50;


    $out = imagecreatetruecolor($nw, $nh);  
    imagecopyresampled($out, $tmp, 0, 0, 0, 0, $nw, $nh, $ow, $oh);  
    //ImageCopyResampleBicubic ($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h,
    //ImageCopyResampleBicubic ($out, $tmp, 0, 0, 0, 0, $nw, $nh, $ow, $oh);

    imagedestroy($tmp);  
    }  
else  
    {  
    echo 'Could not create image resource.';  
    exit;  
    }  


if ($out)  
    {  
    switch (strtolower($type))  
        {  
        case 'jpg':  
            header('Content-type: image/jpeg');  
            imagejpeg($out);  
        break;  

        case 'gif':  
            header('Content-type: image/gif');  
            imagegif($out);  
        break;  

        case 'png':  
            header('Content-type: image/png');  
            imagepng($out);  
        break;  
        }  
    imagedestroy($out);  
    }  
else  
    {  
    echo 'ERROR: Could not create resized image.';  
    }  


function ImageCopyResampleBicubic ($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h,
$src_w,$src_h) {
/*
port to PHP by John Jensen July 10 2001 -- original code (in C, for the PHP GD Module) by jernberg@fairytale.se
*/
        
        for ($i = 0; $i < imagecolorstotal($src_img); $i++) { // get pallete. Is this algoritm correct?
                $colors = ImageColorsForIndex ($src_img, $i);
                ImageColorAllocate ($dst_img, $colors['red'], $colors['green'], $colors['blue']);
        }
        
        $scaleX = ($src_w - 1) / $dst_w;
        $scaleY = ($src_h - 1) / $dst_h;

        $scaleX2 = $scaleX / 2.0;
        $scaleY2 = $scaleY / 2.0;
        
        for ($j = $src_y; $j < $dst_h; $j++) {
        $sY = $j * $scaleY;

                for ($i = $src_x; $i < $dst_w; $i++) {
                        $sX = $i * $scaleX;
        
                        $c1 = ImageColorsForIndex ($src_img, ImageColorAt ($src_img, (int) $sX, (int) $sY + $scaleY2));
                        $c2 = ImageColorsForIndex ($src_img, ImageColorAt ($src_img, (int) $sX, (int) $sY));
                        $c3 = ImageColorsForIndex ($src_img, ImageColorAt ($src_img, (int) $sX + $scaleX2, (int) $sY + $scaleY2));
                        $c4 = ImageColorsForIndex ($src_img, ImageColorAt ($src_img, (int) $sX + $scaleX2, (int) $sY));
        
                        $red = (int) (($c1['red'] + $c2['red'] + $c3['red'] + $c4['red']) / 4);
                        $green = (int) (($c1['green'] + $c2['green'] + $c3['green'] + $c4['green']) / 4);
                        $blue = (int) (($c1['blue'] + $c2['blue'] + $c3['blue'] + $c4['blue']) / 4);
        
                        $color = ImageColorClosest ($dst_img, $red, $green, $blue);
                        ImageSetPixel ($dst_img, $i + $dst_x, $j + $dst_y, $color);
                }
        }
}

?>
