      1: <?php
      2:
      3: /**
      4:  * A utility class for carrying out various image file tasks
      5:  *
      6:  * @package alpha::util
      7:  * @since 1.1
      8:  * @author John Collins <dev@alphaframework.org>
      9:  * @version $Id: AlphaImageUtils.inc 1496 2012-02-12 20:32:21Z alphadev $
     10:  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
     11:  * @copyright Copyright (c) 2012, John Collins (founder of Alpha Framework).
     12:  * All rights reserved.
     13:  *
     14:  * <pre>
     15:  * Redistribution and use in source and binary forms, with or
     16:  * without modification, are permitted provided that the
     17:  * following conditions are met:
     18:  *
     19:  * * Redistributions of source code must retain the above
     20:  *   copyright notice, this list of conditions and the
     21:  *   following disclaimer.
     22:  * * Redistributions in binary form must reproduce the above
     23:  *   copyright notice, this list of conditions and the
     24:  *   following disclaimer in the documentation and/or other
     25:  *   materials provided with the distribution.
     26:  * * Neither the name of the Alpha Framework nor the names
     27:  *   of its contributors may be used to endorse or promote
     28:  *   products derived from this software without specific
     29:  *   prior written permission.
     30:  *
     31:  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
     32:  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
     33:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     34:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     35:  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     36:  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     37:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     38:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     39:  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     40:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     41:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     42:  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     43:  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     44:  * </pre>
     45:  *
     46:  */
     47: class AlphaImageUtils {
     48:     /**
     49:      * Generates a perfectly square thumbnail from the supplied original image file
     50:      *
     51:      * @param string $original The path to the original file
     52:      * @param string $thumbnail The path to the new thumbnail file to generate
     53:      * @param integer $dimensions The width/height of the generated thumbnail
     54:      * @throws IllegalArguementException
     55:      * @since 1.1
     56:      */
     57:     public static function generateSquareThumbnail($original, $thumbnail, $dimensions) {
     58:
     59:         if($dimensions <= 0)
     60:             throw new IllegalArguementException('Illegal dimensions value provided ['.$dimensions.'], should be greater than zero');
     61:
     62:         $newImage = imagecreatetruecolor($dimensions, $dimensions);
     63:         $imageInfo = getimagesize($original);
     64:         $originalX = 0;
     65:
     66:         switch ($imageInfo['mime']) {
     67:             case 'image/jpeg':
     68:                 $type = 'jpg';
     69:                 $originalImage = imagecreatefromjpeg($original);
     70:             break;
     71:             case 'image/gif':
     72:                 $type = 'gif';
     73:                 $originalImage = imagecreatefromgif($original);
     74:             break;
     75:             case 'image/png':
     76:                 $type = 'png';
     77:                 $originalImage = imagecreatefrompng($original);
     78:             break;
     79:             default:
     80:                 throw new IllegalArguementException('Unsupported image format ['.$imageInfo['mime'].']');
     81:         }
     82:
     83:         // in case the destination type is different from the source...
     84:         $pathParts = pathinfo($thumbnail);
     85:         if(!isset($pathParts['extension']))
     86:             $type = $pathParts['extension'];
     87:
     88:         list($originalWidth, $originalHeight) = $imageInfo;
     89:
     90:         if($originalWidth > $originalHeight){
     91:             $originalX = floor(($originalWidth - $originalHeight) / 2);
     92:             $sourceWidth = $sourceHeight = $originalHeight;
     93:         }else{
     94:             $sourceWidth = $sourceHeight = $originalWidth;
     95:         }
     96:
     97:         imagecopyresampled($newImage, $originalImage, 0, 0, $originalX, 0, $dimensions, $dimensions, $sourceWidth, $sourceHeight);
     98:
     99:         return AlphaImageUtils::saveImage($newImage, $type, $thumbnail);
    100:     }
    101:
    102:
    103:     /**
    104:      * Saves the GD image resource to the file path indicated
    105:      *
    106:      * @param image $imageResource The GD image resource to save
    107:      * @param string $type The image type (jpg, png, or gif)
    108:      * @param string $destination The desination file path of the image file to create
    109:      * @throws IllegalArguementException
    110:      * @since 1.1
    111:      */
    112:     public static function saveImage($imageResource, $type, $destination) {
    113:         if (!in_array($type, array('jpg', 'png', 'gif')))
    114:             throw new IllegalArguementException('Illegal image type ['.$type.'], cannot create file');
    115:
    116:         if(($type == 'jpg')) {
    117:             imagejpeg($imageResource, $destination);
    118:         }else{
    119:             $function = 'image'.$type;
    120:
    121:             $function($imageResource, $destination);
    122:         }
    123:
    124:         // free up memory
    125:         imagedestroy($imageResource);
    126:     }
    127:
    128: }
    129:
    130: ?>
    131: