Properties

$fileName

$fileName : string

The name of the file we're manipulating This must include the path to the file (absolute paths recommended)

Type

string

$filesystem

$filesystem : \Symfony\Component\Filesystem\Filesystem

Type

\Symfony\Component\Filesystem\Filesystem

$format

$format : string

What the file format is (mime-type)

Type

string

$remoteImage

$remoteImage : boolean

Whether or not the image is hosted remotely

Type

boolean

$plugins

$plugins : array

An array of attached plugins to execute in order.

Type

array

$oldImage

$oldImage : resource

The prior image (before manipulation)

Type

resource

$workingImage

$workingImage : resource

The working image (used during manipulation)

Type

resource

$currentDimensions

$currentDimensions : array

The current dimensions of the image

Type

array

$newDimensions

$newDimensions : array

The new, calculated dimensions of the image

Type

array

$options

$options : array

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

Type

array

$maxWidth

$maxWidth : integer

The maximum width an image can be after resizing (in pixels)

Type

integer

$maxHeight

$maxHeight : integer

The maximum height an image can be after resizing (in pixels)

Type

integer

$percent

$percent : integer

The percentage to resize the image by

Type

integer

Methods

__construct()

__construct(string  $fileName, array  $options = array(), array  $plugins = array()) 

Parameters

string $fileName
array $options
array $plugins

setOptions()

setOptions(array  $options = array()) : \PHPThumb\GD

Sets options for all operations.

Parameters

array $options

Returns

\PHPThumb\GD

getFileName()

getFileName() : string

Returns the filename.

Returns

string

setFileName()

setFileName(  $fileName) : \PHPThumb\PHPThumb

Sets the filename.

Parameters

$fileName

Returns

\PHPThumb\PHPThumb

getFormat()

getFormat() : string

Returns the format.

Returns

string

setFormat()

setFormat(  $format) : \PHPThumb\PHPThumb

Sets the format.

Parameters

$format

Returns

\PHPThumb\PHPThumb

getIsRemoteImage()

getIsRemoteImage() : boolean

Returns whether the image exists remotely, i.e. it was loaded via a URL.

Returns

boolean

__destruct()

__destruct() 

pad()

pad(  $width,   $height, array  $color = array(255, 255, 255)) : \PHPThumb\GD

Pad an image to desired dimensions. Moves the image into the center and fills the rest with $color.

Parameters

$width
$height
array $color

Returns

\PHPThumb\GD

resize()

resize(integer  $maxWidth, integer  $maxHeight) : \PHPThumb\GD

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.

Parameters

integer $maxWidth

The maximum width of the image in pixels

integer $maxHeight

The maximum height of the image in pixels

Returns

\PHPThumb\GD

adaptiveResize()

adaptiveResize(  $width,   $height) : \PHPThumb\GD

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

Parameters

$width
$height

Returns

\PHPThumb\GD

adaptiveResizePercent()

adaptiveResizePercent(  $width,   $height, integer  $percent = 50) : \PHPThumb\GD

Adaptively Resizes the Image and Crops Using a Percentage

This function attempts to get the image to as close to the provided dimensions as possible, and then crops the remaining overflow using a provided percentage to get the image to be the size specified.

The percentage mean different things depending on the orientation of the original image.

For Landscape images:

A percentage of 1 would crop the image all the way to the left, which would be the same as using adaptiveResizeQuadrant() with $quadrant = 'L'

A percentage of 50 would crop the image to the center which would be the same as using adaptiveResizeQuadrant() with $quadrant = 'C', or even the original adaptiveResize()

A percentage of 100 would crop the image to the image all the way to the right, etc, etc. Note that you can use any percentage between 1 and 100.

For Portrait images:

This works the same as for Landscape images except that a percentage of 1 means top and 100 means bottom

Parameters

$width
$height
integer $percent

Returns

\PHPThumb\GD

adaptiveResizeQuadrant()

adaptiveResizeQuadrant(  $width,   $height, string  $quadrant = 'C') : \PHPThumb\GD

Adaptively Resizes the Image and Crops Using a Quadrant

This function attempts to get the image to as close to the provided dimensions as possible, and then crops the remaining overflow using the quadrant to get the image to be the size specified.

The quadrants available are Top, Bottom, Center, Left, and Right:

+---+---+---+ | | T | | +---+---+---+ | L | C | R | +---+---+---+ | | B | | +---+---+---+

Note that if your image is Landscape and you choose either of the Top or Bottom quadrants (which won't make sence since only the Left and Right would be available, then the Center quadrant will be used to crop. This would have exactly the same result as using adaptiveResize(). The same goes if your image is portrait and you choose either the Left or Right quadrants.

Parameters

$width
$height
string $quadrant

T, B, C, L, R

Returns

\PHPThumb\GD

resizePercent()

resizePercent(integer  $percent) : \PHPThumb\GD

Resizes an image by a given percent uniformly, Percentage should be whole number representation (i.e. 1-100)

Parameters

integer $percent

Throws

\InvalidArgumentException

Returns

\PHPThumb\GD

cropFromCenter()

cropFromCenter(integer  $cropWidth, integer  $cropHeight = null) : \PHPThumb\GD

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

Parameters

integer $cropWidth
integer $cropHeight

Returns

\PHPThumb\GD

crop()

crop(integer  $startX, integer  $startY, integer  $cropWidth, integer  $cropHeight) : \PHPThumb\GD

Vanilla Cropping - Crops from x,y with specified width and height

Parameters

integer $startX
integer $startY
integer $cropWidth
integer $cropHeight

Returns

\PHPThumb\GD

rotateImage()

rotateImage(string  $direction = 'CW') 

Rotates image either 90 degrees clockwise or counter-clockwise

Parameters

string $direction

rotateImageNDegrees()

rotateImageNDegrees(integer  $degrees) : \PHPThumb\GD

Rotates image specified number of degrees

Parameters

integer $degrees

Returns

\PHPThumb\GD

imageFilter()

imageFilter(integer  $filter,   $arg1 = false,   $arg2 = false,   $arg3 = false,   $arg4 = false) : \PHPThumb\GD

Applies a filter to the image

Parameters

integer $filter
$arg1
$arg2
$arg3
$arg4

Returns

\PHPThumb\GD

show()

show(boolean  $rawData = false) : \PHPThumb\GD

Shows an image

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

Parameters

boolean $rawData

Whether or not the raw image stream should be output

Returns

\PHPThumb\GD

getImageAsString()

getImageAsString() : string

Returns the Working Image as a String

This function is useful for getting the raw image data as a string for storage in a database, or other similar things.

Returns

string

save()

save(string  $fileName, string  $format = null) : \PHPThumb\GD

Saves an image

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.

Parameters

string $fileName

The full path and filename of the image to save

string $format

The format to save the image in (optional, must be one of [GIF,JPG,PNG]

Returns

\PHPThumb\GD

getCurrentDimensions()

getCurrentDimensions() 

Returns $currentDimensions.

setCurrentDimensions()

setCurrentDimensions(  $currentDimensions) : \PHPThumb\GD

Parameters

$currentDimensions

Returns

\PHPThumb\GD

getMaxHeight()

getMaxHeight() : integer

Returns

integer

setMaxHeight()

setMaxHeight(  $maxHeight) : \PHPThumb\GD

Parameters

$maxHeight

Returns

\PHPThumb\GD

getMaxWidth()

getMaxWidth() : integer

Returns

integer

setMaxWidth()

setMaxWidth(  $maxWidth) : \PHPThumb\GD

Parameters

$maxWidth

Returns

\PHPThumb\GD

getNewDimensions()

getNewDimensions() 

Returns $newDimensions.

setNewDimensions()

setNewDimensions(object  $newDimensions) 

Sets $newDimensions.

Parameters

object $newDimensions

getOptions()

getOptions() 

Returns $options.

getPercent()

getPercent() 

Returns $percent.

setPercent()

setPercent(object  $percent) 

Sets $percent.

Parameters

object $percent

getOldImage()

getOldImage() 

Returns $oldImage.

setOldImage()

setOldImage(object  $oldImage) 

Sets $oldImage.

Parameters

object $oldImage

getWorkingImage()

getWorkingImage() 

Returns $workingImage.

setWorkingImage()

setWorkingImage(object  $workingImage) 

Sets $workingImage.

Parameters

object $workingImage

validateRequestedResource()

validateRequestedResource(  $filename) : boolean

Check the provided filename/url. If it is a url, validate that it is properly formatted. If it is a file, check to make sure that it actually exists on the filesystem.

Parameters

$filename

Returns

boolean

calcWidth()

calcWidth(integer  $width, integer  $height) : array

Calculates a new width and height for the image based on $this->maxWidth and the provided dimensions

Parameters

integer $width
integer $height

Returns

array

calcHeight()

calcHeight(integer  $width, integer  $height) : array

Calculates a new width and height for the image based on $this->maxWidth and the provided dimensions

Parameters

integer $width
integer $height

Returns

array

calcPercent()

calcPercent(integer  $width, integer  $height) : array

Calculates a new width and height for the image based on $this->percent and the provided dimensions

Parameters

integer $width
integer $height

Returns

array

calcImageSize()

calcImageSize(integer  $width, integer  $height) 

Calculates the new image dimensions

These calculations are based on both the provided dimensions and $this->maxWidth and $this->maxHeight

Parameters

integer $width
integer $height

calcImageSizeStrict()

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

Parameters

integer $width
integer $height

calcImageSizePercent()

calcImageSizePercent(integer  $width, integer  $height) 

Calculates new dimensions based on $this->percent and the provided dimensions

Parameters

integer $width
integer $height

determineFormat()

determineFormat() 

Determines the file format by mime-type

This function will throw exceptions for invalid images / mime-types

verifyFormatCompatiblity()

verifyFormatCompatiblity() 

Makes sure the correct GD implementation exists for the file type

preserveAlpha()

preserveAlpha() 

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! :)