<?
error_reporting(E_ALL);
Class clsImage {
    var $cacheImages = true;
    var $fileLocation;
    var $cacheDirectory; // no trailing slash
    var $imageSource;
    var $imageHeight;
    var $imageWidth;
    var $imageNewHeight;
    var $imageNewWidth;
    var $imageMaxHeight;
    var $imageMaxWidth;
    var $imageQuality;
    function clsImage( $file,$cacheDirectory ) {
        // ---
        // Constructor
        // Set thins like default values
        // ---
        $this->imageMaxHeight = 300;
        $this->imageMaxWidth = 400;
        
        // ---
        // Check if given cache dirtory directory exists, and then set it else throw error..
        // ---
        if( is_dir($cacheDirectory) ) {
            $this->cacheDirectory = $cacheDirectory;
        } else {
            $this->_errorImage('clsImage::constructor -> message = Cache directory does not exist!');
            exit;
        }
        
        // ---
        // Set image properties
        // Don't request imagesize if image is already cached
        // ---
        $this->fileLocation = $file;
        $this->imageSource = $file;
        if( !$this->_imageCached() ) {
            $image = @getImageSize($file);
            if( $image ) {
                $this->imageWidth = $image[0];
                $this->imageHeight = $image[1];
                $this->imageType = $image[2];
            } else {
                $this->_errorImage('clsImage::constructor -> message = Cannot retrieve information about image, image proberly doesn\'t exist');
                exit;
            }
        }
        
        // ---
        // Default image quality = 100 (%)
        // ---
        $this->imageQuality = 100;
    }
    function _setSizes() {
        // ---
        // Calculate the new sizes of the image
        // ---
        if( $this->imageWidth == $this->imageHeight ) {
            // ---
            // If image width and height is same size then we don't have to calculate the ratio
            // ---
            $this->imageNewWidth  = $this->imageMaxWidth;
            $this->imageNewHeight = $this->imageMax;
        }
        elseif( $this->imageWidth < $this->imageHeight ) {
            $ratio = $this->imageMaxHeight/$this->imageHeight;
            $this->imageNewWidth  = $ratio*$this->imageWidth;
            $this->imageNewHeight = $ratio*$this->imageHeight;
        } else {
            $ratio = $this->imageMaxWidth/$this->imageWidth;
            $this->imageNewWidth  = $ratio*$this->imageWidth;
            $this->imageNewHeight = $ratio*$this->imageHeight;
        }
        if( $this->imageNewWidth > $this->imageWidth ) {
            $this->imageNewWidth = $this->imageWidth;
        }
        if( $this->imageNewHeight > $this->imageHeight ) {
            $this->imageNewHeight = $this->imageHeight;
        }
    }
    function _writeToCache() {
        // ---
        // Function which writes and resized image to the cache
        // ---
        switch( $this->imageType ) {
            case 1:
                $input=imagecreatefromgif( $this->imageSource );
                break;
            case 2:
                $input=imagecreatefromjpeg( $this->imageSource );
                break;
            case 3:
                $input=imagecreatefrompng( $this->imageSource );
                break;
            default:
                $this->_errorImage('clsImage::constructor -> message = Unknown image type');
                exit;
        }
        $output = imageCreateTrueColor( $this->imageNewWidth ,$this->imageNewHeight );
        imageColorAllocate( $output,200,0,80 );
        imageCopyResampled( $output,$input,0,0,0,0,$this->imageNewWidth,$this->imageNewHeight,$this->imageWidth,$this->imageHeight );
        $fileLocation = $this->cacheDirectory.'/'.md5( $this->fileLocation ).'.jpg';
        imagejpeg( $output, $fileLocation, $this->imageQuality );
    }
    function _getImageFromCache() {
        $image = imagecreatefromjpeg( $this->cacheDirectory.'/'.md5($this->fileLocation).'.jpg' );
        imagejpeg($image);
    }
    function _imageCached() {
        // ---
        // Function which will return an boolean (true or false) if an image is cached before
        // ---
        if( file_exists( $this->cacheDirectory.'/'.md5($this->fileLocation).'.jpg' ) ) {
            return true;
        } else {
            return false;
        }
    }
    function _errorImage($text) {
        // ---
        // Prints an error image
        // ---
        $output=imagecreate( $this->imageNewWidth,$this->imageNewHeight );
        imagecolorallocate($output,125,125,125);
        imagestring($output,2,2,2,$text,imagecolorallocate($output,230,230,230));
        header("Content-Type: image/jpeg");
        imagejpeg($output);
    }
    function setMaxSizes( $maxHeight,$maxWidth ) {
        if( is_numeric( $maxHeight ) && is_numeric( $maxWidth ) ) {
            $this->imageMaxHeight = $maxHeight;
            $this->imageMaxWidth = $maxWidth;
        } else {
            $this->_errorImage('clsImage::setMaxSizes -> message = parameters are not numeric!');
            exit;
        }
    }
    function loadImage() {
        // ---
        // A public function which returns the image with the correct headers.
        // Set the headers first
        // ---
        header("Content-Type: image/jpeg");
        if( !$this->_imageCached() ) {
            $this->_setSizes();
            $this->_writeToCache();
        }
        $this->_getImageFromCache();
    }
}
if( !empty($_GET['location']) AND is_numeric($_GET['width']) AND is_numeric($_GET['height']) ) {
    $image = New clsImage($_GET['location'],'imagecache/');
    $image->setMaxSizes($_GET['height'],$_GET['width']);
    $image->loadImage();
} else {
    echo 'Invalid arguments!';
}
?>