PHP Class GdThumb, thinksns

This is the GD Implementation of the PHP Thumb library.
Inheritance: extends ThumbBase
Show file Open project: medz/thinksns-4 Class Usage Examples

Protected Properties

Property Type Description
$currentDimensions array The current dimensions of the image
$maxHeight integer The maximum height an image can be after resizing (in pixels)
$maxWidth integer The maximum width an image can be after resizing (in pixels)
$newDimensions array The new, calculated dimensions of the image
$oldImage resource The prior image (before manipulation)
$options array 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
$percent integer The percentage to resize the image by
$workingImage resource The working image (used during manipulation)

Public Methods

Method Description
__construct ( string $fileName, $options = [], $isDataStream = false ) : GdThumb Class Constructor
__destruct ( ) Class Destructor
adaptiveResize ( $width, $height ) : GdThumb Adaptively Resizes the Image
crop ( integer $startX, integer $startY, integer $cropWidth, integer $cropHeight ) : GdThumb Vanilla Cropping - Crops from x,y with specified width and height
cropFromCenter ( integer $cropWidth, integer $cropHeight = null ) : GdThumb Crops an image from the center with provided dimensions
getCurrentDimensions ( ) Returns $currentDimensions.
getImageAsString ( ) : string Returns the Working Image as a String
getMaxHeight ( ) Returns $maxHeight.
getMaxWidth ( ) Returns $maxWidth.
getNewDimensions ( ) Returns $newDimensions.
getOldImage ( ) Returns $oldImage.
getOptions ( ) Returns $options.
getPercent ( ) Returns $percent.
getWorkingImage ( ) Returns $workingImage.
resize ( integer $maxWidth, integer $maxHeight ) : GdThumb Resizes an image to be no larger than $maxWidth or $maxHeight
resizePercent ( integer $percent ) : GdThumb Resizes an image by a given percent uniformly
rotateImage ( string $direction = 'CW' ) Rotates image either 90 degrees clockwise or counter-clockwise
rotateImageNDegrees ( integer $degrees ) : GdThumb Rotates image specified number of degrees
save ( string $fileName, string $format = null ) : GdThumb Saves an image
setCurrentDimensions ( object $currentDimensions ) Sets $currentDimensions.
setMaxHeight ( object $maxHeight ) Sets $maxHeight.
setMaxWidth ( object $maxWidth ) Sets $maxWidth.
setNewDimensions ( object $newDimensions ) Sets $newDimensions.
setOldImage ( object $oldImage ) Sets $oldImage.
setOptions ( array $options = [] ) Sets $this->options to $options
setPercent ( object $percent ) Sets $percent.
setWorkingImage ( object $workingImage ) Sets $workingImage.
show ( boolean $rawData = false ) : GdThumb Shows an image

Protected Methods

Method Description
calcHeight ( integer $width, integer $height ) : array Calculates a new width and height for the image based on $this->maxWidth and the provided dimensions
calcImageSize ( integer $width, integer $height ) Calculates the new image dimensions
calcImageSizePercent ( integer $width, integer $height ) Calculates new dimensions based on $this->percent and the provided dimensions
calcImageSizeStrict ( integer $width, integer $height ) Calculates new image dimensions, not allowing the width and height to be less than either the max width or height
calcPercent ( integer $width, integer $height ) : array Calculates a new width and height for the image based on $this->percent and the provided dimensions
calcWidth ( integer $width, integer $height ) : array Calculates a new width and height for the image based on $this->maxWidth and the provided dimensions
determineFormat ( ) Determines the file format by mime-type
preserveAlpha ( ) Preserves the alpha or transparency for PNG and GIF files
verifyFormatCompatiblity ( ) Makes sure the correct GD implementation exists for the file type

Method Details

__construct() public method

Class Constructor
public __construct ( string $fileName, $options = [], $isDataStream = false ) : GdThumb
$fileName string
return GdThumb

__destruct() public method

Class Destructor
public __destruct ( )

adaptiveResize() public method

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
public adaptiveResize ( $width, $height ) : GdThumb
return GdThumb

calcHeight() protected method

Calculates a new width and height for the image based on $this->maxWidth and the provided dimensions
protected calcHeight ( integer $width, integer $height ) : array
$width integer
$height integer
return array

calcImageSize() protected method

These calculations are based on both the provided dimensions and $this->maxWidth and $this->maxHeight
protected calcImageSize ( integer $width, integer $height )
$width integer
$height integer

calcImageSizePercent() protected method

Calculates new dimensions based on $this->percent and the provided dimensions
protected calcImageSizePercent ( integer $width, integer $height )
$width integer
$height integer

calcImageSizeStrict() protected method

Calculates new image dimensions, not allowing the width and height to be less than either the max width or height
protected calcImageSizeStrict ( integer $width, integer $height )
$width integer
$height integer

calcPercent() protected method

Calculates a new width and height for the image based on $this->percent and the provided dimensions
protected calcPercent ( integer $width, integer $height ) : array
$width integer
$height integer
return array

calcWidth() protected method

Calculates a new width and height for the image based on $this->maxWidth and the provided dimensions
protected calcWidth ( integer $width, integer $height ) : array
$width integer
$height integer
return array

crop() public method

Vanilla Cropping - Crops from x,y with specified width and height
public crop ( integer $startX, integer $startY, integer $cropWidth, integer $cropHeight ) : GdThumb
$startX integer
$startY integer
$cropWidth integer
$cropHeight integer
return GdThumb

cropFromCenter() public method

If no height is given, the width will be used as a height, thus creating a square crop
public cropFromCenter ( integer $cropWidth, integer $cropHeight = null ) : GdThumb
$cropWidth integer
$cropHeight integer
return GdThumb

determineFormat() protected method

This function will throw exceptions for invalid images / mime-types
protected determineFormat ( )

getCurrentDimensions() public method

Returns $currentDimensions.
See also: GdThumb::$currentDimensions

getImageAsString() public method

This function is useful for getting the raw image data as a string for storage in a database, or other similar things.
public getImageAsString ( ) : string
return string

getMaxHeight() public method

Returns $maxHeight.
See also: GdThumb::$maxHeight
public getMaxHeight ( )

getMaxWidth() public method

Returns $maxWidth.
See also: GdThumb::$maxWidth
public getMaxWidth ( )

getNewDimensions() public method

Returns $newDimensions.
See also: GdThumb::$newDimensions
public getNewDimensions ( )

getOldImage() public method

Returns $oldImage.
See also: GdThumb::$oldImage
public getOldImage ( )

getOptions() public method

Returns $options.
See also: GdThumb::$options
public getOptions ( )

getPercent() public method

Returns $percent.
See also: GdThumb::$percent
public getPercent ( )

getWorkingImage() public method

Returns $workingImage.
See also: GdThumb::$workingImage
public getWorkingImage ( )

preserveAlpha() protected method

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! :)
protected preserveAlpha ( )

resize() public method

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.
public resize ( integer $maxWidth, integer $maxHeight ) : GdThumb
$maxWidth integer The maximum width of the image in pixels
$maxHeight integer The maximum height of the image in pixels
return GdThumb

resizePercent() public method

Percentage should be whole number representation (i.e. 1-100)
public resizePercent ( integer $percent ) : GdThumb
$percent integer
return GdThumb

rotateImage() public method

Rotates image either 90 degrees clockwise or counter-clockwise
public rotateImage ( string $direction = 'CW' )
$direction string

rotateImageNDegrees() public method

Rotates image specified number of degrees
public rotateImageNDegrees ( integer $degrees ) : GdThumb
$degrees integer
return GdThumb

save() public method

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)
public save ( string $fileName, string $format = null ) : GdThumb
$fileName string The full path and filename of the image to save
$format string The format to save the image in (optional, must be one of [GIF,JPG,PNG]
return GdThumb

setCurrentDimensions() public method

Sets $currentDimensions.
See also: GdThumb::$currentDimensions
public setCurrentDimensions ( object $currentDimensions )
$currentDimensions object

setMaxHeight() public method

Sets $maxHeight.
See also: GdThumb::$maxHeight
public setMaxHeight ( object $maxHeight )
$maxHeight object

setMaxWidth() public method

Sets $maxWidth.
See also: GdThumb::$maxWidth
public setMaxWidth ( object $maxWidth )
$maxWidth object

setNewDimensions() public method

Sets $newDimensions.
See also: GdThumb::$newDimensions
public setNewDimensions ( object $newDimensions )
$newDimensions object

setOldImage() public method

Sets $oldImage.
See also: GdThumb::$oldImage
public setOldImage ( object $oldImage )
$oldImage object

setOptions() public method

Sets $this->options to $options
public setOptions ( array $options = [] )
$options array

setPercent() public method

Sets $percent.
See also: GdThumb::$percent
public setPercent ( object $percent )
$percent object

setWorkingImage() public method

Sets $workingImage.
See also: GdThumb::$workingImage
public setWorkingImage ( object $workingImage )
$workingImage object

show() public method

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
public show ( boolean $rawData = false ) : GdThumb
$rawData boolean Whether or not the raw image stream should be output
return GdThumb

verifyFormatCompatiblity() protected method

Makes sure the correct GD implementation exists for the file type
protected verifyFormatCompatiblity ( )

Property Details

$currentDimensions protected property

The current dimensions of the image
protected array $currentDimensions
return array

$maxHeight protected property

The maximum height an image can be after resizing (in pixels)
protected int $maxHeight
return integer

$maxWidth protected property

The maximum width an image can be after resizing (in pixels)
protected int $maxWidth
return integer

$newDimensions protected property

The new, calculated dimensions of the image
protected array $newDimensions
return array

$oldImage protected property

The prior image (before manipulation)
protected resource $oldImage
return resource

$options protected property

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
protected array $options
return array

$percent protected property

The percentage to resize the image by
protected int $percent
return integer

$workingImage protected property

The working image (used during manipulation)
protected resource $workingImage
return resource