Subversion Repositories travelsized

Rev

Rev 82 | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * This file is part of Travelsized CMS
 *              A content management system with modules, based on wiki syntax
 *
 * Author: Dan Jensen <admin@leinir.dk>
 * Copyright 2003/2004
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * The GNU General Public License is available at: http://www.gnu.org/copyleft/
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */


/**
 * Takes the filename of two images, the image to scale from and the one to scale to, as well as a target width and height
 *
 * @param     original       String containing the filename of the original image
 * @param     destination    String containing the filename where you wish the scaled image to be placed
 * @param     width          Integer defining the maximum width of the resulting image
 * @param     height         Integer defining the maximum height of the resulting image
 *
 * @return    mixed          True if it worked, or a translated string explaining the nature of the error if not
 */

function scaleImage( $original, $destination, $width, $height )
{
        if( checkImageMimetype( $original ) != false )
        {
                list( $x, $y ) = getimagesize( $original );
               
                // Adjust code to doing it as ajreading at classixshop dot com does in the PHP.net manual
                // It does more or less what my old code did, only a tiny bit cleaner... pretty code is a good thing :)
                $x_ratio = $width / $x;
                $y_ratio = $height / $y;
                if( $x <= $width && $y <= $height )
                {
                        $x2 = $x;
                        $y2 = $y;
                }
                else if( ($x_ratio * $y) < $height )
                {
                        $x2 = $width;
                        $y2 = ceil( $x_ratio * $y);
                }
                else
                {
                        $x2 = ceil( $y_ratio * $x );
                        $y2 = $height;
                }
               
                if( checkImageMimetype( $original ) == "jpg" )
                {
                        $src = imagecreatefromjpeg( $original );
                        $out = imagecreatetruecolor( $x2, $y2 );
                       
                        imagecopyresampled( $out, $src, 0, 0, 0, 0, $x2, $y2, $x, $y );
                        imagejpeg( $out, $destination );
                }
                else if( checkImageMimetype( $original ) == "png" )
                {
                        $src = imagecreatefrompng( $original );
                        $out = imagecreatetruecolor( $x2, $y2 );
                       
                        { // Resize with alpha...
                        imagesavealpha( $out, true );
                        imagealphablending( $out, false );
                        $transparent = imagecolorallocatealpha( $out, 255, 255, 255, 0 );
                        imagefilledrectangle( $out, 0, 0, $x2, $y2, $transparent );
                        }
                       
                        imagecopyresampled( $out, $src, 0, 0, 0, 0, $x2, $y2, $x, $y );
                        imagepng( $out, $destination );
                }
                else if( checkImageMimetype( $original ) == "gif" )
                {
                        $src = imagecreatefromgif( $original );
                        $out = imagecreatetruecolor( $x2, $y2 );
                        imagecopyresampled( $out, $src, 0, 0, 0, 0, $x2, $y2, $x, $y );
                        imagegif( $out, $destination );
                }
                imagedestroy( $out );
                imagedestroy( $src );
               
                $returnval = true;
        }
        else
        {
                if( checkImageMimetype( $original ) == false )
                        $returnval = i18n( "Image type not supported or not an image file!" );
                else
                        $returnval = i18n( "Unknown error" );
        }
       
        return $returnval;
}

/**
 * Takes the filename of two images, the image to crop a part of and the one to output the result to,
 * as well as a top corner and width and height of the area
 *
 * @param     original       String containing the filename of the original image
 * @param     destination    String containing the filename where you wish the cropped image to be placed
 * @param     topCornerX     Integer defining the X value of the top left corner of the crop area
 * @param     topCornerY     Integer defining the Y value of the top left corner of the crop area
 * @param     width          Integer defining the width of the cropped area
 * @param     height         Integer defining the height of the cropped area
 *
 * @return    mixed          True if it worked, or a translated string explaining the nature of the error if not
 */

function cropImage( $original, $destination, $topCornerX, $topCornerY, $width, $height )
{
        $img = imagecreatetruecolor( $width, $height );
        $ext = checkImageMimetype( $original );
       
        if( $ext == "jpg" )
                $org_img = imagecreatefromjpeg( $original );
        else if( $ext == "png" )
                $org_img = imagecreatefrompng( $original );
        else if( $ext == "gif" )
                $org_img = imagecreatefromgif( $original );
        else
                return i18n("The image type is not supported. Only jpeg, png and gif images are supported."); // If we reached this... the image type isn't supported
               
        $ims = getimagesize( $original );
       
        if( $ext == "png" )
        {
                imagesavealpha( $img, true );
                imagealphablending( $img, false );
                $transparent = imagecolorallocatealpha( $img, 255, 255, 255, 0 );
                imagefilledrectangle( $img, 0, 0, $width, $height, $transparent );
        }
       
        imagecopy( $img, $org_img, 0, 0, $topCornerX, $topCornerY, $ims[0], $ims[1]);
       
        if( $ext == "jpg" )
                imagejpeg( $img, $destination, 90 );
        else if( $ext == "png" )
                imagepng($img, $destination );
        else if( $ext == "gif" )
                imagegif( $img, $destination );
       
        imagedestroy($img);

        return true;
}
?>