Documentation

ImageManager
in package

Image manager

Tags
TODO

This class currently mixes two things: It represents an image and it provides helper methods. Helper methods should probably be static.

copyright

Cloudrexx AG

author

Paulo M. Santos pmsantos@astalavista.net

author

Michael Ritter michael.ritter@cloudrexx.com

subpackage

lib

Table of Contents

Constants

IMG_TYPE_GIF  = 1
IMG_TYPE_JPEG  = 2
IMG_TYPE_PNG  = 3
IMG_TYPE_WEBP  = 18

Properties

$orgImageHeight  : int
$orgImageWidth  : int
$iccProfile  : string
Caches the ICC default color space profile
$imageCheck  : bool
$newImage  : resource
$newImageFile  : string
$newImageHeight  : int
$newImageQuality  : int
$newImageType  : int
$newImageWidth  : int
$orgImage  : resource
$orgImageFile  : string
$orgImageType  : int

Methods

__construct()  : mixed
Constructor
_createThumb()  : bool
Creates a thumbnail of a picture
_createThumbWhq()  : bool
Create a thumbnail of a picture.
_getImageSize()  : array<string|int, mixed>
Gets the size of the image
_isImage()  : bool|int
Returns the image type as determined by getimagesize() for the given file.
addBackgroundLayer()  : mixed
Add Background Layer
cropImage()  : bool
Crops the image with the given coordinates.
fixImageColorSpace()  : mixed
Ensure the image uses the correct color space profile
fixImageOrientation()  : mixed
Fix image orientation based on its Exif-data
getQuality()  : int
Returns the proper quality value for creating an image of the current type
getThumbnailFilename()  : string
Returns the file name for the thumbnail image
loadImage()  : bool
Gets several image information and saves it in the image variables.
resizeImage()  : booelan
Resizes the original image to the given dimensions and stores it as a new.
resizeImageWithAspectRatio()  : bool
Resize the image with aspect ratio and fill the white color in empty area
rotateImage()  : bool
Rotates the image 90 degrees to the left or right.
saveNewImage()  : bool
Saves the new image wherever you want
showNewImage()  : bool
Outputs the new image in the browser
_imageCreateFromFile()  : resource
Creates an image from an image file
_resetVariables()  : void
Resets all object variables.
createNewImageForResize()  : void
create new image for resize
getIccProfile()  : string
Get the ICC default color space profile.
outputImage()  : void
Outputs the edited image ($this->newImage)
resize()  : bool
setTransparency()  : void
Add transparency to new image

Constants

IMG_TYPE_GIF

public int IMG_TYPE_GIF = 1

Image type GIF

IMG_TYPE_JPEG

public int IMG_TYPE_JPEG = 2

Image type JPEG

IMG_TYPE_PNG

public int IMG_TYPE_PNG = 3

Image type PNG

IMG_TYPE_WEBP

public int IMG_TYPE_WEBP = 18

Image type WEBP

Properties

$orgImageHeight

public int $orgImageHeight

Height of this image

Tags
todo

This should be protected and be accessed via getter instead

todo

While this is inteded to be a int this is sometimes set to string

$orgImageWidth

public int $orgImageWidth

Width of this image

Tags
todo

This should be protected and be accessed via getter instead

todo

While this is inteded to be a int this is sometimes set to string

$iccProfile

Caches the ICC default color space profile

protected static string $iccProfile

Never use this directly, always use getIccProfile() instead!

$imageCheck

protected bool $imageCheck = \false

Whether the current image has been checked to be a valid image

$newImage

protected resource $newImage

Image currently being edited

Tags
todo

While this is inteded to be a resource this is sometimes set to string

todo

This should not exist. Instead we should directly work with $orgImage

$newImageFile

protected string $newImageFile

Filename of the image currently being edited

Tags
todo

This should not exist. Instead we should directly work with $orgImage

$newImageHeight

protected int $newImageHeight

Height of the image currently being edited

Tags
todo

While this is inteded to be a int this is sometimes set to string

todo

This should not exist. Instead we should directly work with $orgImage

$newImageQuality

protected int $newImageQuality

Quality of the image currently being edited

Tags
todo

While this is inteded to be a int this is sometimes set to string

todo

This should not exist. Instead we should directly work with $orgImage

$newImageType

protected int $newImageType

Current image type, one of the IMG_TYPE_... constants below

Tags
todo

While this is inteded to be a int this is sometimes set to string or bool

todo

This should not exist. Instead we should directly work with $orgImage

$newImageWidth

protected int $newImageWidth

Width of the image currently being edited

Tags
todo

While this is inteded to be a int this is sometimes set to string

todo

This should not exist. Instead we should directly work with $orgImage

$orgImage

protected resource $orgImage

Current image

Tags
todo

While this is inteded to be a resource this is sometimes set to string

$orgImageFile

protected string $orgImageFile

Filename of this image

$orgImageType

protected int $orgImageType

Current image type, one of the IMG_TYPE_... constants below

Tags
todo

While this is inteded to be a int this is sometimes set to string or bool

Methods

__construct()

Constructor

public __construct() : mixed
Tags
TODO:

This should directly do what loadImage() does.

_createThumb()

Creates a thumbnail of a picture

public _createThumb(string $strPath, string $strWebPath, string $file[, int $maxSize = 80 ][, int $quality = 90 ][, string $thumb_name = '' ][, bool $generateThumbnailByRatio = false ]) : bool

Note that all "Path" parameters are required to bring along their own trailing slash. This is a no-op for SVG images, and returns true.

Parameters
$strPath : string
$strWebPath : string
$file : string
$maxSize : int = 80

The maximum width or height

$quality : int = 90
$thumb_name : string = ''
$generateThumbnailByRatio : bool = false
Return values
bool

True on success, false otherwise

_createThumbWhq()

Create a thumbnail of a picture.

public _createThumbWhq(string $strPath, string $strWebPath, string $file[, int $maxWidth = 80 ][, int $maxHeight = 80 ][, int $quality = 90 ][, string $thumbNailSuffix = '.thumb' ][, string $strPathNew = null ][, string $strWebPathNew = null ][, string $fileNew = null ][, mixed $fixSize = false ][, mixed $crop = false ]) : bool

This is very much like , but provides more parameters. Both the width and height of the thumbnail may be specified; the picture will still be scaled to fit within the given sizes while keeping the original width/height ratio. In addition to that, this method tries to delete an existing thumbnail before attempting to write the new one. This is a no-op for SVG images, and returns true. Note that all "Path" parameters are required to bring along their own trailing slash.

Parameters
$strPath : string

The image file folder

$strWebPath : string

The image file web folder

$file : string

The image file name

$maxWidth : int = 80

The maximum width of the image

$maxHeight : int = 80

The maximum height of the image

$quality : int = 90

The desired jpeg thumbnail quality

$thumbNailSuffix : string = '.thumb'

Suffix of the thumbnail. Default is 'thumb'

$strPathNew : string = null

Image file store folder. Default is $strPath

$strWebPathNew : string = null

Image file web store folder. Default is $strWebPath

$fileNew : string = null

Image file store name. Default is $file

$fixSize : mixed = false
$crop : mixed = false
Tags
todo

Refactor redundant code

Return values
bool

True on success, false otherwise.

_getImageSize()

Gets the size of the image

public _getImageSize(string $file) : array<string|int, mixed>
Parameters
$file : string

The path of the image

Tags
access

private

todo

This method is completely redundant. It does exactly the same as calling @getimagesize($file) directly! - Remove.

Return values
array<string|int, mixed>

The array as returned by @getimagesize($file) on success, false otherwise

_isImage()

Returns the image type as determined by getimagesize() for the given file.

public static _isImage(string $file) : bool|int

Only accepts supported web image types (GIF, JPG, PNG or WEBP). If the function imagecreatefromgif() is not available, GIF images aren't accepted. False is returned for images/files that are not supported.

Parameters
$file : string

The file path of the image

Return values
bool|int

The file type on success, false otherwise

addBackgroundLayer()

Add Background Layer

public addBackgroundLayer(array<string|int, mixed> $bgColor[, mixed $width = null ][, mixed $height = null ]) : mixed

This scales the image to a size that it fits into the rectangle defined by width $width and height $height. Spaces at the edges will be padded with the color $bgColor.

Parameters
$bgColor : array<string|int, mixed>

is an array containing 3 values, representing the red, green and blue portion (0-255) of the desired color.

$width : mixed = null
$height : mixed = null

cropImage()

Crops the image with the given coordinates.

public cropImage(int $x, int $y, int $width, int $height) : bool
Parameters
$x : int

X-coordinate for the new image.

$y : int

Y-coordinate for the new image.

$width : int

Width for the new image.

$height : int

Height for the new image.

Tags
access

public

Return values
bool

True on success, false otherwise.

fixImageColorSpace()

Ensure the image uses the correct color space profile

public fixImageColorSpace(string $filePath) : mixed

The W3C has defined sRGB as the default color space (https://www.w3.org/Graphics/Color/sRGB). sRGB (standard Red Green Blue) has been standardized by the International Electrotechnical Commission (IEC) as IEC 61966-2-1 (https://webstore.iec.ch/publication/6168). The International Color Consortium (ICC) has published a color profile for the default color space sRGB. The profile is called 'IEC 61966-2-1 Default RGB Colour Space - sRGB' (http://color.org/srgbprofiles.xalter). Therefore, it is recommended that images (who do use color profiles) do use the profile by the ICC. Otherwise the display is unkown as not all end user devices do properly support color space profiles and do instead most likely assume that an image uses the IEC 61966-2-1 Default RGB Colour Space. Further, PHP's GD-library does also not support color space profiles. Therefore, it does also always assume an image uses the ICC standard profile. The latter causes unexpected results when performing any image manipulation with GD on images that are not using the ICC standard color space profile. As a result, we need to ensure that all images that are uploaded are using the ICC standard color space profile. If required, this means that we have to convert any non-confirming images on the fly.

Parameters
$filePath : string

Image file path

fixImageOrientation()

Fix image orientation based on its Exif-data

public fixImageOrientation(string $filePath) : mixed
Parameters
$filePath : string

File path

getQuality()

Returns the proper quality value for creating an image of the current type

public getQuality() : int

The value returned is the original value set, except for type PNG. The latter requires the quality to be in the range [0..9] instead of [0..100].

Return values
int

The quality, scaled for the current type

getThumbnailFilename()

Returns the file name for the thumbnail image

public static getThumbnailFilename(string $file_name) : string

Replaces the .png extension with .jpg if a thumbnail in PNG format is already present. This is for backwards compatibility with versions up to 2.2 which did not create PNG thumbnails. Appends the .thumb extension if not already present.

Parameters
$file_name : string

The image file name

Return values
string

The thumbnail file name

loadImage()

Gets several image information and saves it in the image variables.

public loadImage(string $file) : bool

SHOULD NOT be called for SVG images, as the image functions used do not support that format. This method does not use exceptions because it is called by older methods without catchers.

Parameters
$file : string

Path and filename of the existing image.

Tags
access

public

Return values
bool

True on success, false otherwise.

resizeImage()

Resizes the original image to the given dimensions and stores it as a new.

public resizeImage(string $width, string $height, string $quality) : booelan

This method does not use exceptions because it is called by older methods without catchers.

Parameters
$width : string

The width of the new image.

$height : string

The height of the new image.

$quality : string

The quality for the new image.

Tags
access

public

Return values
booelan

True on success, false otherwise.

resizeImageWithAspectRatio()

Resize the image with aspect ratio and fill the white color in empty area

public resizeImageWithAspectRatio(string $width, string $height, string $quality) : bool
Parameters
$width : string

The width of the new image.

$height : string

The height of the new image.

$quality : string

The quality for the new image.

Return values
bool

rotateImage()

Rotates the image 90 degrees to the left or right.

public rotateImage(float $angle) : bool
Parameters
$angle : float

Rotation angle, in degrees. The rotation angle is interpreted as the number of degrees to rotate the image anticlockwise.

Tags
access

public

Return values
bool

True on success, false otherwise.

saveNewImage()

Saves the new image wherever you want

public saveNewImage(string $file[, booelan $forceOverwrite = false ][, booelan $injectTransparency = true ]) : bool
Parameters
$file : string

The path for the image file to be written.

$forceOverwrite : booelan = false

Force overwriting existing files if true.

$injectTransparency : booelan = true

Whether or not to set transparency info on new image.

Tags
todo

In case the PHP script has no write access to the location set by $this->newImageFile, the image shall be sent to the output buffer and then be put into the new file location using the FileSystemFile object.

todo

Check for all usages of this method if argument $injectTransparency must be set to FALSE.

access

public

Return values
bool

True on success, false otherwise.

showNewImage()

Outputs the new image in the browser

public showNewImage() : bool
Tags
access

public

Return values
bool

True on success, false otherwise

_imageCreateFromFile()

Creates an image from an image file

protected _imageCreateFromFile(string $file) : resource

SHOULD NOT be called for SVG images, as the image functions used do not support that format.

Parameters
$file : string

The path of the image

Tags
access

private

Return values
resource

The image on success, the empty or false string otherwise

_resetVariables()

Resets all object variables.

protected _resetVariables() : void

createNewImageForResize()

create new image for resize

protected createNewImageForResize(string $width, string $height, string $quality[, mixed $setWhiteBackground = false ]) : void
Parameters
$width : string

The width of the new image.

$height : string

The height of the new image.

$quality : string

The quality for the new image.

$setWhiteBackground : mixed = false

getIccProfile()

Get the ICC default color space profile.

protected getIccProfile() : string

The profile will be loaded from the filesystem if not yet done.

Return values
string

The ICC default color space profile

outputImage()

Outputs the edited image ($this->newImage)

protected outputImage([resource|string|null $output = null ]) : void

Takes type and quality (if available) from $this->newImageType and $this->getQuality() into account. If $output is null the correct headers are set.

Parameters
$output : resource|string|null = null

(optional) File to save output to or null to output to browser

Tags
throws
Exception

If the type of the edited image is unknown

resize()

protected resize(mixed $width, mixed $height[, mixed $quality = 100 ][, mixed $offsetLeft = 0 ][, mixed $offsetTop = 0 ][, mixed $fit = false ][, mixed $backgroundFillColor = '' ]) : bool
Parameters
$width : mixed
$height : mixed
$quality : mixed = 100
$offsetLeft : mixed = 0
$offsetTop : mixed = 0
$fit : mixed = false
$backgroundFillColor : mixed = ''
Tags
todo

Merge with static::resizeImage()

Return values
bool

setTransparency()

Add transparency to new image

protected setTransparency() : void

Define a color as transparent or add alpha channel, depending on the image file type.


        
On this page

Search results