\CodeIgniter\Images\HandlersImageMagickHandler

Class ImageMagickHandler

To make this library as compatible as possible with the broadest number of installations, we do not use the Imagick extension, but simply use the command line version.

hmm - the width & height accessors at the end use the imagick extension.

FIXME - This needs conversion & unit testing, to use the imagick extension

Summary

Methods
Properties
Constants
__construct()
withFile()
getFile()
getResource()
resize()
crop()
convert()
rotate()
flatten()
flip()
_flip()
text()
reorient()
getEXIF()
fit()
getVersion()
save()
__call()
getWidth()
getHeight()
_resize()
_crop()
_flatten()
_getWidth()
_getHeight()
No public properties found
No constants found
ensureResource()
_rotate()
_text()
calcAspectRatio()
calcCropCoords()
process()
reproportion()
getResourcePath()
$config
$image
$width
$height
$filePermissions
$xAxis
$yAxis
$masterDim
$textDefaults
$resource
N/A
No private methods found
No private properties found
N/A

Properties

$config

$config : \Config\Images

Configuration settings.

Type

\Config\Images

$width

$width : integer

Image width.

Type

integer

$height

$height : integer

Image height.

Type

integer

$filePermissions

$filePermissions : \CodeIgniter\Images\Handlers\type

File permission mask.

Type

\CodeIgniter\Images\Handlers\type

$xAxis

$xAxis : integer

X-axis.

Type

integer

$yAxis

$yAxis : integer

Y-axis.

Type

integer

$masterDim

$masterDim : string

Master dimensioning.

Type

string

$textDefaults

$textDefaults : array

Default options for text watermarking.

Type

array

$resource

$resource : 

Stores image resource in memory.

Type

Methods

__construct()

__construct(\CodeIgniter\Images\Handlers\type  $config = null) 

Constructor.

Parameters

\CodeIgniter\Images\Handlers\type $config

Throws

\CodeIgniter\Images\Handlers\type

withFile()

withFile(string  $path) : $this

Sets another image for this handler to work on.

Keeps us from needing to continually instantiate the handler.

Parameters

string $path

Returns

$this

getResource()

getResource() : resource

Returns the temporary image used during the image processing.

Good for extending the system or doing things this library is not intended to do.

Returns

resource

resize()

resize(integer  $width, integer  $height, boolean  $maintainRatio = false, string  $masterDim = 'auto') : \CodeIgniter\Images\Handlers\BaseHandler

Resize the image

Parameters

integer $width
integer $height
boolean $maintainRatio

If true, will get the closest match possible while keeping aspect ratio true.

string $masterDim

Returns

\CodeIgniter\Images\Handlers\BaseHandler

crop()

crop(integer|null  $width = null, integer|null  $height = null, integer|null  $x = null, integer|null  $y = null, boolean  $maintainRatio = false, string  $masterDim = 'auto') : mixed

Crops the image to the desired height and width. If one of the height/width values is not provided, that value will be set the appropriate value based on offsets and image dimensions.

Parameters

integer|null $width
integer|null $height
integer|null $x

X-axis coord to start cropping from the left of image

integer|null $y

Y-axis coord to start cropping from the top of image

boolean $maintainRatio
string $masterDim

Returns

mixed

rotate()

rotate(float  $angle) : mixed

Rotates the image on the current canvas.

Parameters

float $angle

Returns

mixed

flatten()

flatten(integer  $red = 255, integer  $green = 255, integer  $blue = 255) : mixed

Flattens transparencies, default white background

Parameters

integer $red
integer $green
integer $blue

Returns

mixed

flip()

flip(string  $dir = 'vertical') : $this

Flips an image either horizontally or vertically.

Parameters

string $dir

Either 'vertical' or 'horizontal'

Returns

$this

_flip()

_flip(string  $direction) : $this

Flips an image along it's vertical or horizontal axis.

Parameters

string $direction

Throws

\Exception

Returns

$this

text()

text(string  $text, array  $options = array()) : $this

Overlays a string of text over the image.

Valid options:

  • color Text Color (hex number)
  • shadowColor Color of the shadow (hex number)
  • hAlign Horizontal alignment: left, center, right
  • vAlign Vertical alignment: top, middle, bottom
  • hOffset
  • vOffset
  • fontPath
  • fontSize
  • shadowOffset

Parameters

string $text
array $options

Returns

$this

reorient()

reorient(boolean  $silent = false) : $this

Reads the EXIF information from the image and modifies the orientation so that displays correctly in the browser. This is especially an issue with images taken by smartphones who always store the image up-right, but set the orientation flag to display it correctly.

Parameters

boolean $silent

If true, will ignore exceptions when PHP doesn't support EXIF.

Returns

$this

getEXIF()

getEXIF(string|null  $key = null, boolean  $silent = false) : mixed

Retrieve the EXIF information from the image, if possible. Returns an array of the information, or null if nothing can be found.

EXIF data is only supported fr JPEG & TIFF formats.

Parameters

string|null $key

If specified, will only return this piece of EXIF data.

boolean $silent

If true, will not throw our own exceptions.

Returns

mixed

fit()

fit(integer  $width, integer  $height = null, string  $position = 'center') : boolean

Combine cropping and resizing into a single command.

Supported positions:

  • top-left
  • top
  • top-right
  • left
  • center
  • right
  • bottom-left
  • bottom
  • bottom-right

Parameters

integer $width
integer $height
string $position

Returns

boolean

getVersion()

getVersion() : string

Get driver version

Returns

string

save()

save(string|null  $target = null, integer  $quality = 90) : boolean

Saves any changes that have been made to file. If no new filename is provided, the existing image is overwritten, otherwise a copy of the file is made at $target.

Example: $image->resize(100, 200, true) ->save();

Parameters

string|null $target
integer $quality

Returns

boolean

__call()

__call(string  $name, array  $args = array()) : mixed

Provide access to the Image class' methods if they don't exist on the handler itself.

Parameters

string $name
array $args

Returns

mixed

getWidth()

getWidth() : integer

Return image width.

accessor for testing; not part of interface

Returns

integer

getHeight()

getHeight() : \CodeIgniter\Images\Handlers\type

Return image height.

accessor for testing; not part of interface

Returns

\CodeIgniter\Images\Handlers\type

_resize()

_resize(boolean  $maintainRatio = false) : \CodeIgniter\Images\Handlers\ImageMagickHandler

Handles the actual resizing of the image.

Parameters

boolean $maintainRatio

Throws

\Exception

Returns

\CodeIgniter\Images\Handlers\ImageMagickHandler

_flatten()

_flatten(integer  $red = 255, integer  $green = 255, integer  $blue = 255) : $this

Flattens transparencies, default white background

Parameters

integer $red
integer $green
integer $blue

Throws

\Exception

Returns

$this

_getWidth()

_getWidth() : \CodeIgniter\Images\Handlers\type

Return the width of an image.

Returns

\CodeIgniter\Images\Handlers\type

_getHeight()

_getHeight() : \CodeIgniter\Images\Handlers\type

Return the height of an image.

Returns

\CodeIgniter\Images\Handlers\type

ensureResource()

ensureResource() 

Make the image resource object if needed

_rotate()

_rotate(integer  $angle) : $this

Handles the rotation of an image resource.

Doesn't save the image, but replaces the current resource.

Parameters

integer $angle

Throws

\Exception

Returns

$this

_text()

_text(string  $text, array  $options = array()) 

Handler-specific method for overlaying text on an image.

Parameters

string $text
array $options

Throws

\Exception

calcAspectRatio()

calcAspectRatio(  $width, null  $height = null,   $origWidth,   $origHeight) : array

Calculate image aspect ratio.

Parameters

$width
null $height
$origWidth
$origHeight

Returns

array

calcCropCoords()

calcCropCoords(  $width,   $height,   $origWidth,   $origHeight,   $position) : array

Based on the position, will determine the correct x/y coords to crop the desired portion from the image.

Parameters

$width
$height
$origWidth
$origHeight
$position

Returns

array

process()

process(string  $action, integer  $quality = 100) : array

Handles all of the grunt work of resizing, etc.

Parameters

string $action
integer $quality

Throws

\Exception

Returns

array —

Lines of output from shell command

reproportion()

reproportion() : void

Re-proportion Image Width/Height

When creating thumbs, the desired width/height can end up warping the image due to an incorrect ratio between the full-sized image and the thumb.

This function lets us re-proportion the width/height if users choose to maintain the aspect ratio when resizing.

getResourcePath()

getResourcePath() : resource|boolean

Get Image Resource

This simply creates an image resource handle based on the type of image being processed. Since ImageMagick is used on the cli, we need to ensure we have a temporary file on the server that we can use.

To ensure we can use all features, like transparency, during the process, we'll use a PNG as the temp file type.

Throws

\Exception

Returns

resource|boolean