PHP Thumbnail Class
Thu, 30 Sep 2010I decided to put together an open-source area where I'll write re-usable code for anyone that needs it. Last night I wrote a PHP thumbnail class, it's only setup for JPEG at the moment. I'll probably work on it later to supply it with ratio resizing also :)
I can't type as well lately because of wrist pains, I ordered a wrist piece to wrap around my hand when i type and that should be here shortly. I keep getting cramps and pains in my hand and knuckle. Anyways! Here you can download the php thumbnail class at the Open Source Area
Download, or View below (The download contains the latest version):
<?php
/**
* Thumbnail Generator 0.2 (Requires GD 2.0)
*
* @author JREAM
* @link http://www.jream.com
* @copyright 2010 Jesse Boyer (contact@jream.com)
* @license GNU General Public License 3 (http://www.gnu.org/licenses/)
*
* 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 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
* http://www.gnu.org/licenses/
*
* @uses
*
$Thumb = new Thumbnail('photo.jpg');
// Set the new size we want:
$Thumb->Size(400, 200);
// Process it:
$Thumb->Make();
// To see the result (optional)
$Thumb->Result;
*/
class Thumbnail
{
/** Source Image */
private $_srcImage;
/** Source Dimensions */
private $_srcImageX;
private $_srcImageY;
/** Destination Image */
private $_dstImage;
/** Destination Dimensions */
private $_dstImageX;
private $_dstImageY;
/** @var $Result Whether or not the file was created */
public $Result = NULL;
/**
* @desc Setup the name of the file to handle
*
* @param $filename The name of the file
* @param $prefix Text before the filename
* @param $quality Quality of the JPEG
*/
public function __construct($filename, $prefix = 'th_', $quality = 70)
{
$this->Filename = $prefix . $filename;
$this->Quality = $quality;
$this->_srcImage = imageCreateFromJPEG($filename);
/** Grab the Source Dimensions */
$this->_srcImageX = imagesX($this->_srcImage);
$this->_srcImageY = imagesY($this->_srcImage);
}
/**
* @desc Set the thumbnail size,
* Depending on the proportion of the image factor this in:
* if the image is wider than the height, the X will match.
* if the image is taller than the width, the Y will match.
*
* @param $x Desired width
* @param $y Desired height
*/
public function Size($x, $y)
{
$this->_dstImageX = (int) $x;
$this->_dstImageY = (int) $y;
/** Thanks to my brother Joe for helping me with math, I'm really bad! */
if ($this->_srcImageX > $this->_srcImageY) {
$this->_dstImageX = ($this->_srcImageX * $this->_dstImageY) / $this->_srcImageY;
}
if ($this->_srcImageX < $this->_srcImageY) {
$this->_dstImageY = ($this->_srcImageY * $this->_dstImageX) / $this->_srcImageX;
}
}
/**
* @desc Runs the operation and outputs the image
*/
public function Make()
{
/** Make sure the thumbnail sizes are set */
if (!isset($this->_dstImageX) || !isset($this->_dstImageY))
throw new Exception("Please set the size of your thumbnail using the Size() method.");
/** Create a new blank image */
$this->_dstImage = imageCreateTrueColor($this->_dstImageX, $this->_dstImageY);
/** Copy the resamples image onto the blank */
imageCopyResampled($this->_dstImage, $this->_srcImage, 0, 0, 0, 0, $this->_dstImageX, $this->_dstImageY, $this->_srcImageX, $this->_srcImageY);
/** Save the file, also allow the result to be grabbed from a var */
$this->Result = imageJPEG($this->_dstImage, $this->Filename, $this->Quality);
/** Finishing by removing from memory */
imageDestroy($this->_srcImage);
imageDestroy($this->_dstImage);
}
}