<?
/*
Resizer Script
--------------
Version:        3.0

Purpose:        Make images available for download for wallpapers or desktops.
Functionality:    Scale 4:3 images, crop for height (widescreen), add watermark, deliver.

Authors:        Aaron Linville <http://linville.org>
                Erik J. Barzeski <http://nslog.com/>

Variables:        image            the image name (without ".jpg")
                height            the desired height in pixels
                width            the desired width in pixels

License:        This code is copyright © 2007 Aaron Linville and Erik J. Barzeski.
                The right to re-use or re-purpose this code for your own use is granted
                so long as reasonable credit is given to the original authors, including
                a link to <http://nslog.com/desktops/resizer>. In other words, use it all
                you want, but be so kind as to give some credit.
*/




/* -------- */
/* SETTINGS */
/* -------- */

// ImageMagic Libraries
$imConvertPath        "/usr/local/bin/convert";            // location of "convert"
$imCompositePath    "/usr/local/bin/composite";        // location of "composite"

// Location of Images
// All paths relative to the location of *this* script
$origImageDir "src/";                                    // original files (2560 x 1920 recommended)
$cacheImageDir "cache/";                                // for saving images once build - must be writable by the web server
$watermarkPath "signature-small.tif";                    // watermark (TIFF files - transparency works too)


// Works with either GET or POST requests. GET will let people pass around bookmarks.
if(isset($_REQUEST['width']) && isset($_REQUEST['height']) && isset($_REQUEST['image']))
{
    
$width $_REQUEST['width'];
    
$height $_REQUEST['height'];
    
    
// Check for valid width/height
    
if(!ctype_digit($width) || !ctype_digit($height) || $height 300 || $height 1920 || $width 400 || $width 2560)
    {
        exit(
"Error - Please specify a valid height and width. Both must be integers (and 400-2560 for width and 300-1920 for height).");
    }
    
    
// Check for valid filename
    
$image basename($_REQUEST['image'], ".jpg");

    
// Include Statistics Module - comment out the line below if you don't want to keep stats
    
include('downloader_stats.inc');

    
$origImagePath $origImageDir $image ".jpg";
    
$cacheTmpImagePath $cacheImageDir $image "-" $width "x" $height".tmp.jpg";
    
$cacheImagePath $cacheImageDir $image "-" $width "x" $height".jpg";

    
// Don't waste time generating images that are already generated
    
if(!file_exists($cacheImagePath))
    {
        list(
$origWidth$origHeight) = getimagesize($origImagePath);
        
        
$origRatio $origWidth $origHeight;
        
$newRatio $width $height;
        
        
$cmd "$imConvertPath $origImagePath ";

        if(
$newRatio $origRatio)
        {
            
$cmd .= "-resize " $width "x ";
        }
        else
        {
            
$cmd .= "-resize x" $height " ";
        }

        
$cmd .= "-gravity center -crop " $width "x" $height "+0+0 +repage ";
        
$cmd .= $cacheTmpImagePath;

        
system("$cmd"$retVal);
        if(
$retVal 0)
        {
            exit(
"Error - convert failed");
        }
        
        
$cmd "$imCompositePath -gravity southeast ";
        
$cmd .= "$watermarkPath $cacheTmpImagePath $cacheImagePath";        
        
system("$cmd"$retVal);
        if(
$retVal 0)
        {
            exit(
"Error - composite failed");
        }
        
        
unlink($cacheTmpImagePath);
    }
    
    
// Output the picture
    
if($filesize filesize($cacheImagePath))
    {
        
$fp fopen($cacheImagePath'rb'); // Read, force binary
        
$buffer fread($fp$filesize);
        
fclose($fp);
    
        
$ext substr($cacheImagePath, -3);
        switch(
$ext)
        {
            case 
"gif"$ctype="image/gif"; break;
            case 
"png"$ctype="image/png"; break;
            case 
"jpg"$ctype="image/jpeg"; break;
            default: 
$ctype="application/force-download";
        }
    
        
header("Content-Length: " $filesize);
        
header("Content-Type: " $ctype);
        
header("Content-Disposition: inline; filename=\"" basename($cacheImagePath".jpg") . "\"");
    
        print 
$buffer;
        
        return;
    }
    else
    {
        exit(
"Error - Can't find final image.");
    }
}
else
{
    exit(
"Error - Missing requests variables.");
}

?>