Source for file GdThumb.inc.php
Documentation is available at GdThumb.inc.php
* PhpThumb GD Thumb Class Definition File
* This file contains the definition for the GdThumb object
* PHP Version 5 with GD 2.0+
* PhpThumb : PHP Thumb Library <http://phpthumb.gxdlabs.com>
* Copyright (c) 2009, Ian Selby/Gen X Design
* Author(s): Ian Selby <ian@gen-x-design.com>
* Licensed under the MIT License
* Redistributions of files must retain the above copyright notice.
* @author Ian Selby <ian@gen-x-design.com>
* @copyright Copyright (c) 2009 Gen X Design
* @link http://phpthumb.gxdlabs.com
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
* GdThumb Class Definition
* This is the GD Implementation of the PHP Thumb library.
* The prior image (before manipulation)
* The working image (used during manipulation)
* The current dimensions of the image
* The new, calculated dimensions of the image
* The options for this class
* This array contains various options that determine the behavior in
* various functions throughout the class. Functions note which specific
* option key / values are used in their documentation
* The maximum width an image can be after resizing (in pixels)
* The maximum height an image can be after resizing (in pixels)
* The percentage to resize the image by
* @param string $fileName
public function __construct ($fileName, $options =
array())
// TODO: Port gatherImageMeta to a separate function that can be called to extract exif data
##############################
# ----- API FUNCTIONS ------ #
##############################
* Resizes an image to be no larger than $maxWidth or $maxHeight
* If either param is set to zero, then that dimension will not be considered as a part of the resize.
* Additionally, if $this->options['resizeUp'] is set to true (false by default), then this function will
* also scale the image up to the maximum dimensions provided.
* @param int $maxWidth The maximum width of the image in pixels
* @param int $maxHeight The maximum height of the image in pixels
public function resize ($maxWidth =
0, $maxHeight =
0)
// make sure our arguments are valid
throw
new InvalidArgumentException('$maxWidth must be numeric');
throw
new InvalidArgumentException('$maxHeight must be numeric');
// make sure we're not exceeding our image size if we're not supposed to
if ($this->options['resizeUp'] ===
false)
// get the new dimensions...
// create the working image
// and create the newly sized image
// update all the variables and resources to be correct
* Adaptively Resizes the Image
* This function attempts to get the image to as close to the provided dimensions as possible, and then crops the
* remaining overflow (from the center) to get the image to be the size specified
// make sure our arguments are valid
throw
new InvalidArgumentException('$width must be numeric and greater than zero');
throw
new InvalidArgumentException('$height must be numeric and greater than zero');
// make sure we're not exceeding our image size if we're not supposed to
if ($this->options['resizeUp'] ===
false)
// resize the image to be close to our desired dimensions
// reset the max dimensions...
if ($this->options['resizeUp'] ===
false)
// create the working image
// now, figure out how to crop the rest of the image...
// update all the variables and resources to be correct
* Resizes an image by a given percent uniformly
* Percentage should be whole number representation (i.e. 1-100)
throw
new InvalidArgumentException ('$percent must be numeric');
* Crops an image from the center with provided dimensions
* If no height is given, the width will be used as a height, thus creating a square crop
throw
new InvalidArgumentException('$cropWidth must be numeric');
if ($cropHeight !==
null &&
!is_numeric($cropHeight))
throw
new InvalidArgumentException('$cropHeight must be numeric');
if ($cropHeight ===
null)
$cropHeight =
$cropWidth;
$this->crop($cropX, $cropY, $cropWidth, $cropHeight);
* Vanilla Cropping - Crops from x,y with specified width and height
public function crop ($startX, $startY, $cropWidth, $cropHeight)
throw
new InvalidArgumentException('$startX must be numeric');
throw
new InvalidArgumentException('$startY must be numeric');
throw
new InvalidArgumentException('$cropWidth must be numeric');
throw
new InvalidArgumentException('$cropHeight must be numeric');
// ensure everything's in bounds
// create the working image
* Rotates image either 90 degrees clockwise or counter-clockwise
* @param string $direction
* Rotates image specified number of degrees
throw
new InvalidArgumentException('$degrees must be numeric');
throw
new RuntimeException('Your version of GD does not support image rotation.');
* This function will show the current image by first sending the appropriate header
* for the format, and then outputting the image data. If headers have already been sent,
* a runtime exception will be thrown
throw
new RuntimeException('Cannot show image, headers have already been sent');
header('Content-type: image/gif');
header('Content-type: image/jpeg');
header('Content-type: image/png');
* This function will make sure the target directory is writeable, and then save the image.
* If the target directory is not writeable, the function will try to correct the permissions (if allowed, this
* is set as an option ($this->options['correctPermissions']). If the target cannot be made writeable, then a
* RuntimeException is thrown.
* TODO: Create additional paramter for color matte when saving images with alpha to non-alpha formats (i.e. PNG => JPG)
* @param string $fileName The full path and filename of the image to save
* @param string $format The format to save the image in (optional, must be one of [GIF,JPG,PNG]
public function save ($fileName, $format =
null)
$validFormats =
array('GIF', 'JPG', 'PNG');
throw
new InvalidArgumentException ('Invalid format type specified in save function: ' .
$format);
// make sure the directory is writeable
// try to correct the permissions
if ($this->options['correctPermissions'] ===
true)
// throw an exception if not writeable
throw
new RuntimeException ('File is not writeable, and could not correct permissions: ' .
$fileName);
// throw an exception if not writeable
throw
new RuntimeException ('File not writeable: ' .
$fileName);
#################################
# ----- GETTERS / SETTERS ----- #
#################################
* Sets $this->options to $options
// make sure we've got an array for $this->options (could be null)
// make sure we've gotten a proper argument
throw
new InvalidArgumentException ('setOptions requires an array');
// we've yet to init the default options, so create them here
'correctPermissions' =>
false,
'alphaMaskColor' =>
array (255, 255, 255),
'preserveTransparency' =>
true,
'transparencyMaskColor' =>
array (0, 0, 0)
// otherwise, let's use what we've got already
* Returns $currentDimensions.
* @see GdThumb::$currentDimensions
* Sets $currentDimensions.
* @param object $currentDimensions
* @see GdThumb::$currentDimensions
* @see GdThumb::$maxHeight
* @param object $maxHeight
* @see GdThumb::$maxHeight
* @see GdThumb::$maxWidth
* @param object $maxWidth
* @see GdThumb::$maxWidth
* Returns $newDimensions.
* @see GdThumb::$newDimensions
* @param object $newDimensions
* @see GdThumb::$newDimensions
* @see GdThumb::$oldImage
* @param object $oldImage
* @see GdThumb::$oldImage
* @see GdThumb::$workingImage
* @param object $workingImage
* @see GdThumb::$workingImage
#################################
# ----- UTILITY FUNCTIONS ----- #
#################################
* Calculates a new width and height for the image based on $this->maxWidth and the provided dimensions
protected function calcWidth ($width, $height)
$newWidthPercentage =
(100 *
$this->maxWidth) /
$width;
$newHeight =
($height *
$newWidthPercentage) /
100;
'newHeight' =>
intval($newHeight)
* Calculates a new width and height for the image based on $this->maxWidth and the provided dimensions
$newHeightPercentage =
(100 *
$this->maxHeight) /
$height;
$newWidth =
($width *
$newHeightPercentage) /
100;
'newWidth' =>
ceil($newWidth),
* Calculates a new width and height for the image based on $this->percent and the provided dimensions
$newWidth =
($width *
$this->percent) /
100;
$newHeight =
($height *
$this->percent) /
100;
'newWidth' =>
ceil($newWidth),
'newHeight' =>
ceil($newHeight)
* Calculates the new image dimensions
* These calculations are based on both the provided dimensions and $this->maxWidth and $this->maxHeight
$newSize =
$this->calcWidth($width, $height);
$newSize =
$this->calcHeight($newSize['newWidth'], $newSize['newHeight']);
$newSize =
$this->calcWidth($newSize['newWidth'], $newSize['newHeight']);
* Calculates new image dimensions, not allowing the width and height to be less than either the max width or height
// first, we need to determine what the longest resize dimension is..
// and determine the longest original dimension
$newDimensions =
$this->calcHeight($width, $height);
if ($newDimensions['newWidth'] <
$this->maxWidth)
$newDimensions =
$this->calcWidth($width, $height);
elseif ($height >=
$width)
$newDimensions =
$this->calcWidth($width, $height);
if ($newDimensions['newHeight'] <
$this->maxHeight)
$newDimensions =
$this->calcHeight($width, $height);
$newDimensions =
$this->calcWidth($width, $height);
if ($newDimensions['newHeight'] <
$this->maxHeight)
$newDimensions =
$this->calcHeight($width, $height);
elseif ($height >
$width)
$newDimensions =
$this->calcHeight($width, $height);
if ($newDimensions['newWidth'] <
$this->maxWidth)
$newDimensions =
$this->calcWidth($width, $height);
* Calculates new dimensions based on $this->percent and the provided dimensions
* Determines the file format by mime-type
* This function will throw exceptions for invalid images / mime-types
// non-image files will return false
if ($formatInfo ===
false)
// make sure we really stop execution
$mimeType = isset
($formatInfo['mime']) ?
$formatInfo['mime'] :
null;
$this->triggerError('Image format not supported: ' .
$mimeType);
* Makes sure the correct GD implementation exists for the file type
$isCompatible =
$gdInfo['GIF Create Support'];
$isCompatible =
$gdInfo[$this->format .
' Support'];
// one last check for "JPEG" instead
$isCompatible =
$gdInfo['JPEG Support'];
$this->triggerError('Your GD installation does not support ' .
$this->format .
' image types');
* Preserves the alpha or transparency for PNG and GIF files
* Alpha / transparency will not be preserved if the appropriate options are set to false.
* Also, the GIF transparency is pretty skunky (the results aren't awesome), but it works like a
* champ... that's the nature of GIFs tho, so no huge surprise.
* This functionality was originally suggested by commenter Aimi (no links / site provided) - Thanks! :)
if ($this->format ==
'PNG' &&
$this->options['preserveAlpha'] ===
true)
$this->options['alphaMaskColor'][0],
$this->options['alphaMaskColor'][1],
$this->options['alphaMaskColor'][2],
// preserve transparency in GIFs... this is usually pretty rough tho
if ($this->format ==
'GIF' &&
$this->options['preserveTransparency'] ===
true)
$this->options['transparencyMaskColor'][0],
$this->options['transparencyMaskColor'][1],
$this->options['transparencyMaskColor'][2]
Documentation generated on Tue, 09 Aug 2011 09:04:57 +0200 by phpDocumentor 1.4.3