<?php
$image
=addslashes($_GET["f"]);

# paths
$imgpath ""# ending with "/" !
$nopicurl "notfound.jpg"# starting in $imagepath!!!
$nofileurl "notfound.jpg"# starting in $imagepath!!!


if(!isset($image) || empty($image)){
   
$imageurl $imgpath $nopicurl;
   echo(
"NO FILE GIVEN");
   exit;
}
elseif(! 
file_exists($imgpath trim($image))){
   
$imageurl $imgpath $nofileurl;
   echo(
"NO IMAGE THERE!");
    
var_dump($_GET);
print 
"Couldn't find: $imgpath . trim($image) .";
   exit;
}
else
   
$imageurl $imgpath trim($image);
   
$imageData getImageSize($imageurl); 

   
# Standard height and width.
$w = (int)addslashes($_GET["w"]);
$h = (int)addslashes($_GET["h"]);

$fpng = (bool)escapeshellarg($_GET["fpng"]);
if(
$_GET["debug"]) print "FPNG is $fpng.<br/>\n";

$originalX $imageData[0];
$originalY $imageData[1];
if(
$_GET["debug"]) print "Original is w:$originalX, h:$originalY.<br/>\n";

    
// Don't make image bigger
    
$smallX min($imageData[0],$w);
    
$smallY min($imageData[1],$h);

    if(
$_GET["debug"]) print "Desired is w:$smallX, h:$smallY.<br/>\n";
    
    
// Figure out aspect ratio
    
if($imageData[1]/$imageData[0] > $smallY/$smallX) {
        
//Height is constraint, use it
        //Example: 774/580 > 100x435 
        // W should become 580*(150/774)
        
if($_GET["debug"]) print "round($originalX*$smallY/$originalY)";
        
$smallX round($originalX*$smallY/$originalY);
        
$zoom $h/$imageData[1];
    } else {
        
$smallY round($originalY*$smallX/$originalX);
        
        
$zoom $smallX/$imageData[0];
                
    }

if(!
$_GET["force"]) {
    
$w $smallX;
    
$h $smallY;

}



if(
$_GET["debug"]) print "Zoom is $zoom.<br/>\n";





$filename "data/" $h "x" $w"x" $image;
//print $filename . "Got here!";
if(file_exists($filename) && !$_GET["nocache"] && $image != '00410_katy-perry.jpg') {
    
showImage($filename);
    exit;
}




# colour- & textvalues
$picBG "250,0,0"# RGB-value !
$picFG "104,104,104"# RGB-value !
$copyright "mortalpowers.com"
$font 1;

# minimal & maximum zoom
$minZoom 1# per cent related on orginal (!=0)
$maxZoom 200# per cent related on orginal (!=0)

 

# reading image
switch($imageData[2]) {
     case 
1:
       
# GIF image
       
$timg imageCreateFromGIF($imageurl);
       break;
   case 
2:
       
# JPEG image
       
$timg imageCreateFromJPEG($imageurl);
       break;
   case 
3:
       
# PNG image
       
$timg imageCreateFromPNG($imageurl);
       break;
}




# zoom check to the original
if($zoom 10000/$minZoom$zoom 10000/$minZoom;
if(
$zoom 10000/$maxZoom$zoom 10000/$maxZoom;

# calculate new image sizes

if($_GET["debug"]) print "New sizes are $w and $h.<br/>\n";

# set start positoin of the image
# always centered 
if($_GET["debug"]) print "($w-$smallX) / 2<br/>\n";
if(
$_GET["debug"]) print "($h-$smallY) / 2<br/>\n";

$posX = ($w-$smallX) / 2;
$posY = ($h-$smallY) / 2;


# creating new image with given sizes
$imgh imageCreateTrueColor($w$h);

# setting colours
$cols explode(","$picBG);
$bgcol imagecolorallocatealpha($imghtrim($cols[0]), trim($cols[1]), trim($cols[2]),0);

$cols explode(","$picFG);
$fgcol imagecolorallocatealpha($imghtrim($cols[0]), trim($cols[1]), trim($cols[2]),105);

# fill background
if(!$fpng) {    imageFill($imgh00$bgcol);} else {
$trans_colour imagecolorallocatealpha($imgh000127);
imagefill($imgh00$trans_colour);
}
if(
$_GET["debug"]) print "$posX,$posY,0,0,  $smallX$smallY,$originalX$originalY, fpng is $fpng.<br/>\n";

# create small copy of the image
$result imageCopyResampled($imgh$timg$posX,$posY,0,0,  $smallX$smallY,$originalX$originalY);
if(
$_GET["debug"]) print "Result was $result.<br/>\n";

# writing copyright note
imageStringUp($imgh$font$w-9$h-3$copyright$fgcol);
//header("Content-type: image/jpeg");
//imageJPEG($imgh);
//exit;
if($_GET["debug"]) print "Filename is $filename.<br/>\n";

imagealphablending($imghfalse);
imagesavealpha($imghtrue);

# output
if($fpng) {
    
$imageData[2] = 3;
}
switch(
$imageData[2]) {
     case 
1:
   
# GIF image
      // header("Content-type: image/gif");
       
imageGIF($imgh$filename);
       break;
   case 
2:
   
# JPEG image
       //header("Content-type: image/jpeg");
       
imageJPEG($imgh$filename);
       break;
   case 
3:
   
# PNG image
      // header("Content-type: image/png");
       
imagePNG($imgh$filename);
       break;
   default:
           print 
"File type not found.";
}
chmod($filename0775);

# cleaning cache
imageDestroy($timg);
imageDestroy($imgh);

if(
$_GET["debug"]) print "Test ($filename)";
if(
$_GET["debug"]) print '<img src="data:image/jpg;base64,' base64_encode(file_get_contents($imageurl)) . '" style="border:3px solid;width:30px;" alt="missing image"/>';

showImage($filename);

function 
showImage($filename) {
    
$i file_get_contents($filename);
    
$hash md5($i);
    
$ext strtolower(substr($filename, -3,3));
    if(
$ext == "jpg") {
        
$ext "jpeg";
    }
    if(!
$_GET["debug"]) {
        
//header_remove("expires");
    
header("Content-type: image/" $ext);
        
header("Cache-Control: max-age=". (60 60 24 3));
        
header("ETag: \"$hash\"");
header("Expires: 01 Jun 2010 05:00:00 GMT");
    }
    print 
$i;
}