Jump to content

Uncropping thumbnail images


ryanrowlett

Recommended Posts

Hello, I am new to PHP. The end goal is to add masonry.js to a showkase gallery site (its a plugin-based CMS that syncs with Adobe Lightroom). The default functionality is for incoming images to crop into a square. I want them to display at a given width and then be however long they are without cropping into a square.

 

I believe this code contains the culprit. Any help would be greatly appreciated!

 

Thanks,

Ryan

 

<?php
/**
 * Part of SimpleViewer.net portfolio web site package.
 *
 * @package Showkase
 * @author Jack Hardie {@link http://www.jhardie.com}
 * @copyright Copyright © 2014, SimpleViewer Inc.
 */
defined('SK_ACCESS')||die('<h1>403: Forbidden</h1>');
require_once 'classes'.DIRECTORY_SEPARATOR.'helpers.php';
 
/**
 * Creates image thumbnail
 *
 * @package Showkase
 */
Class Thumbnail
{
    
    /**
     * constructor
     */
    public function __construct()
    {
        $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);
    }
 
    /**
     * 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
     * @param integer thumb width
     * @param integer thumb height
     * @param integer thumb quality
     */
    function createThumb($filePath, $thumbPath, $thumbWidth, $thumbHeight, $thumbQuality)
    {
        $thumbPath = Helpers::changeFileExtension($thumbPath, 'jpg', array('jpeg', 'JPG', 'JPEG'));
        if ($thumbWidth <= 0) {
            $thumbWidth = THUMB_DISPLAY_SIZE;
        }
        if ($thumbHeight <=0) {
            $thumbHeight = THUMB_DISPLAY_SIZE;
        }
        $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 = $thumbHeight/$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($thumbWidth, $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, $thumbWidth, $thumbHeight, $cropWidth, $cropHeight)) {
                throw new Exception('Cannot create thumbnail using imagecopyresampled');
            }
            // save the thumbnail image into a file.
       if (!@imagejpeg($imageDest, $thumbPath, $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);
    }
 
}
 
Link to comment
Share on other sites

I don't know that this code contains a 'culprit' or that there necessarily even is one. Hard to see when only seeing a single class without context. This class appears to be responsible for creating thumbnails, that's it. It seems unlikely that the original image would be entirely discarded, so that's where you'd want to start looking.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.