Scripts

Speciale image resizer

Ja alweer een saai image resize script! Ik heb dit script geschreven voor mijn eigen forum, omdat het zich enkele voordelen biedt, en niet zomaar een image resizer is. Alhoewel saai? Dit script heeft wel een aantal eigenaardigheden: - het is object georienteerd - resized images, en behoudt mooie kwaliteit - cached images - pagina's laden sneller - nooit meer dode plaatjes - je forum layout wordt niet meer verziekt vanwege grote plaatjes die users posten. - bespaart andere servers veel dataverkeer Dit is wel leuk he? Echter is er een nadeel (alhoewel het is maar je hoe het bekijkt) - Als iemand een plaatje upload, (http://domein.nl/plaatje.jpg) en daarna het plaatje wijzigd en opnieuw upload onder dezelfde locatie, zal hij het plaatje niet updaten, maar moet de gebruiker het bestand een andere naam geven of je met het bestand uit de cache wissen. - bij forums met heel veel plaatjes, kost het misschien veel meer dataverkeer, aan de andere kant ook weer niet omdat hij ze resized etc Misschien dat ik in de toekomst nog een aanvullende versie maak waarbij mensen een plaatje uit de cache kunnen verwijderen. In de code vindt je meteen gebruiks voorbeeld. Ik ben benieuwd naar het commentaar :)

speciale-image-resizer
<?
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!';
}
?>

Reacties

0
Nog geen reacties.