ImageManager
in package
Image manager
Tags
Table of Contents
Constants
- IMG_TYPE_GIF = 1
- IMG_TYPE_JPEG = 2
- IMG_TYPE_PNG = 3
- IMG_TYPE_WEBP = 18
Properties
- $imageCheck : mixed
- $newImage : mixed
- $newImageFile : mixed
- $newImageHeight : mixed
- $newImagePosX : mixed
- $newImagePosY : mixed
- $newImageQuality : mixed
- $newImageType : mixed
- $newImageWidth : mixed
- $orgImage : mixed
- $orgImageFile : mixed
- $orgImageHeight : mixed
- $orgImageType : mixed
- $orgImageWidth : mixed
- $iccProfile : string
- Will contain the ICC default color space profile
Methods
- __construct() : void
- Constructor
- _checkTrailingSlash() : void
- Add a trailing slash to the given path if there's none already
- _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
- _imageCreateFromFile() : resource
- Creates an image from an image file
- _isImage() : int
- Returns the image type as determined by getimagesize() for the given file.
- _resetVariables() : void
- Resets all object variables.
- addBackgroundLayer() : mixed
- Add Background Layer
- createNewImageForResize() : mixed
- create new image for resize
- 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
- getImageDim() : array<string|int, mixed>
- Scale down the original image dimensions to a given maximum pixel size
- 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 serveral 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
- getIccProfile() : string
- Get the ICC default coolor space profile.
- setTransparency() : void
- Add transparency to new image
Constants
IMG_TYPE_GIF
public
mixed
IMG_TYPE_GIF
= 1
IMG_TYPE_JPEG
public
mixed
IMG_TYPE_JPEG
= 2
IMG_TYPE_PNG
public
mixed
IMG_TYPE_PNG
= 3
IMG_TYPE_WEBP
public
mixed
IMG_TYPE_WEBP
= 18
Properties
$imageCheck
public
mixed
$imageCheck
= 1
$newImage
public
mixed
$newImage
$newImageFile
public
mixed
$newImageFile
$newImageHeight
public
mixed
$newImageHeight
$newImagePosX
public
mixed
$newImagePosX
$newImagePosY
public
mixed
$newImagePosY
$newImageQuality
public
mixed
$newImageQuality
$newImageType
public
mixed
$newImageType
$newImageWidth
public
mixed
$newImageWidth
$orgImage
public
mixed
$orgImage
$orgImageFile
public
mixed
$orgImageFile
$orgImageHeight
public
mixed
$orgImageHeight
$orgImageType
public
mixed
$orgImageType
$orgImageWidth
public
mixed
$orgImageWidth
$iccProfile
Will contain the ICC default color space profile
protected
static string
$iccProfile
Methods
__construct()
Constructor
public
__construct() : void
Tags
_checkTrailingSlash()
Add a trailing slash to the given path if there's none already
public
_checkTrailingSlash(string &$path) : void
The parameter is given by reference.
Parameters
- $path : string
-
The directory path
_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 ]) : 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
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
Return values
array<string|int, mixed> —The array as returned by @getimagesize($file) on success, false otherwise
_imageCreateFromFile()
Creates an image from an image file
public
_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
Return values
resource —The image on success, the empty string otherwise
_isImage()
Returns the image type as determined by getimagesize() for the given file.
public
static _isImage(string $file) : 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
int —The file type on success, false otherwise
_resetVariables()
Resets all object variables.
public
_resetVariables() : void
Tags
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
createNewImageForResize()
create new image for resize
public
createNewImageForResize(string $width, string $height, string $quality[, mixed $setWhiteBackground = false ]) : mixed
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
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
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
getImageDim()
Scale down the original image dimensions to a given maximum pixel size
public
getImageDim(string $path, string $webPath, string $fileName[, int $max = 60 ]) : array<string|int, mixed>
Proportionally scales the width and height, so that both fit within the maximum size. The array returned looks like array( 'style' => 'style="height: <scaled_height>px; width: <scaled_width>px;"', 'width' => original width plus one in pixels, 'height' => original height pluis one in pixels, )
Parameters
- $path : string
-
The absolute file path
- $webPath : string
-
The file path relative to the document root
- $fileName : string
-
The file name
- $max : int = 60
-
The maximum size for both width and height
Tags
Return values
array<string|int, mixed> —An array of image dimensions on success, null otherwise
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 serveral 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
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
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
boolrotateImage()
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
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
Return values
bool —True on success, false otherwise.
showNewImage()
Outputs the new image in the browser
public
showNewImage() : bool
Tags
Return values
bool —True on success, false otherwise
getIccProfile()
Get the ICC default coolor 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
setTransparency()
Add transparency to new image
private
setTransparency() : void
Define a color as transparent or add alpha channel, depending on the image file type.