/home/coolpkct/www/websites/cake3.cool.rocks/admin/classes/thumbnail.php
<?php
/**
 * Part of SimpleViewer.net portfolio web site package.
 *
 * @package Showkase
 * @author Jack Hardie {@link http://www.jhardie.com}
 * @copyright Copyright (c) 2012, SimpleViewer Inc.
 */
defined('SK_ACCESS')||die('<h1>403: Forbidden</h1>');
require_once 'classes'.DIRECTORY_SEPARATOR.'helpers.php';
 
/**
 * Site Navigation
 *
 * @package Showkase
 */
Class Thumbnail
{
    /**
     * @var integer thumb width
     */
    protected $thumbWidth = THUMB_DISPLAY_SIZE;
    
    /**
     * @var integer thumb height
     */
    protected $thumbHeight = THUMB_DISPLAY_SIZE;
    
    /**
     * @var integer thumb jpg quality
     */
    protected $thumbQuality;
    
    /**
     * @var instance of site singleton
     */
    protected $site;
    
    /**
     * constructor
     */
    public function __construct($thumbWidth, $thumbHeight, $thumbQuality)
    {
        $memoryLimit = (ini_get('memory_limit') == '') ? MEMORY_LIMIT_FALLBACK : ini_get('memory_limit');
        $this->maxImageBytes = (MEMORY_LIMIT == 0) ? Helpers::getBytes($memoryLimit) : MEMORY_LIMIT * pow(2,20);
        if ($thumbWidth > 0) $this->thumbWidth = $thumbWidth;
        if ($thumbHeight >0) $this->thumbHeight = $thumbHeight;
        $this->thumbQuality = $thumbQuality;
    }
    /**
     * function createThumb creates and saves one thumbnail image.
     * thumbnails are always jpegs and always have extension jpg.
     *
     * @access private
     * @return void
     * @param string $filePath path to source image
     * @param string $thumbPath path to new thumbnail
     */
    function createThumb($filePath, $thumbPath)
    {
        $thumbPath = Helpers::changeFileExtension($thumbPath, 'jpg', array('jpeg', 'JPG', 'JPEG'));
        $dimensions = @getimagesize($filePath);
        if ($dimensions === false) {
            throw new Exception('cannot calculate size of image '.$filePath);
        }
        // $imageInfo['channels'] is not set for png images so just guess at 3
        $channels = 3;
        $memoryNeeded = Round(($dimensions[0] * $dimensions[1] * $dimensions['bits'] * $channels / 8 + Pow(2, 16)) * MEMORY_SAFETY_FACTOR);
        if ($memoryNeeded > $this->maxImageBytes) {
        	throw new Exception('uncompressed image '.$filePath.' exceeds '.($this->maxImageBytes/1048576).' MB');
        } 
    	  $imageWidth		= $dimensions[0];
    	  $imageHeight	= $dimensions[1];
    	  if ($dimensions[0] == 0 || $dimensions[1] == 0) {
            throw new Exception('Zero width or height for '.$filePath);
        }
        $imageAspect = $dimensions[1]/$dimensions[0];
        $thumbAspect = ($this->thumbWidth == 0)
            ? 1
            : $this->thumbHeight/$this->thumbWidth;
        if ($imageAspect >= $thumbAspect) {
            // thumbnail is full-width
            $cropWidth = $imageWidth;
            $cropHeight = $imageWidth * $thumbAspect;
            $deltaX = 0;
            $deltaY = ($imageHeight - $cropHeight)/2;
        } else {
            // thumbnail is full-height
            $cropWidth = $imageHeight / $thumbAspect;
            $cropHeight = $imageHeight;
            $deltaX = ($imageWidth - $cropWidth)/2;
            $deltaY = 0;
        }
        // get image identifier for source image
        switch ($dimensions[2]) {
            case IMAGETYPE_GIF :
                $imageSrc    = @imagecreatefromgif($filePath);
                break;
            case IMAGETYPE_JPEG :
                $imageSrc = @imagecreatefromjpeg($filePath);
                break;
            case IMAGETYPE_PNG :
                $imageSrc = @imagecreatefrompng($filePath);
                break;
            default :
                throw new Exception('Unidentified image type '.$filePath);
        }
        if ($imageSrc === false) {
            throw new Exception('Cannot get image identifier for '.$filePath);
        }
        // Create an empty thumbnail image. 
        $imageDest = @imagecreatetruecolor($this->thumbWidth, $this->thumbHeight);
        if ($imageDest === false) {
            @imagedestroy($imageSrc);
            throw new Exception('Cannot create true color image');
        }
        $grey = imagecolorallocate($imageDest, 192, 192, 192);
        imagefill($imageDest, 0, 0, $grey);
        try {
            if (!@imagecopyresampled($imageDest, $imageSrc, 0, 0, $deltaX, $deltaY, $this->thumbWidth, $this->thumbHeight, $cropWidth, $cropHeight)) {
                throw new Exception('Cannot create thumbnail using imagecopyresampled');
            }
            // save the thumbnail image into a file.
    		    if (!@imagejpeg($imageDest, $thumbPath, $this->thumbQuality)) {
                throw new Exception('Cannot save thumbnail');
            }
        } catch (Exception $e) {
            @imagedestroy($imageSrc);
    		    @imagedestroy($imageDest);
            unset ($imageSrc, $imageDest);
            throw new Exception('Unable to create new thumbnail ('.$e->getMessage().')');
        }
		// Delete both image resources.
		    @imagedestroy($imageSrc);
		    @imagedestroy($imageDest);
        unset ($imageSrc, $imageDest);
    }
}