AbstractImage
extends AbstractProperties
in package
implements
ImageInterface
uses
CalculationTrait, ImageTrait
Abstract class of Image class.
Tags
Table of Contents
Interfaces
- ImageInterface
- Interface for image manipulation drivers
Constants
- RDIERROR_CALCULATEFAILED_USE_RESIZENORATIO = 4
- Unable to calculated, please try to calculate on your own and use `resizeNoRatio()` instead.
- RDIERROR_CROP_UNKNOWNIMG = 5
- Unable to crop this kind of image.
- RDIERROR_FLIP_NOTSUPPORTED = 9
- Not support flip for current PHP version. (PHP 5.5 or newer is required to use GD flip.)
- RDIERROR_FLIP_UNKNOWIMG = 8
- Unable to flip this kind of image.
- RDIERROR_IMAGEMAGICK_NOTMEETREQUIREMENT = 21
- ImageMagick version does not meet requirement.
- RDIERROR_IMAGICK_NOTLOAD = 18
- Imagick extension was not loaded.
- RDIERROR_IMAGICK_NOTMEETREQUIREMENT = 19
- Imagick version does not meet requirement.
- RDIERROR_IMAGICK_VERSIONUNKNOW = 20
- Could not verify Imagick version.
- RDIERROR_RESIZE_UNKNOWIMG = 6
- Unable to resize this kind of image.
- RDIERROR_ROTATE_UNKNOWIMG = 7
- Unable to rotate this kind of image.
- RDIERROR_SAVE_FAILED = 13
- Failed to save an image.
- RDIERROR_SAVE_UNSUPPORT = 12
- Unable to save to this extension. or target file extension is not supported.
- RDIERROR_SHOW_FAILED = 15
- Failed to show an image to browser.
- RDIERROR_SHOW_UNSUPPORT = 14
- Unable to show image in this extension. File extension is not supported.
- RDIERROR_SRC_NOTEXISTS = 3
- Source file is not exists.
- RDIERROR_SRC_NOTIMAGE = 2
- Source file is not image.
- RDIERROR_SRC_UNKNOWN = 16
- Unable to set source from this kind of image.
- RDIERROR_SRC_WEBP_ALPHA_NOTSUPPORTED = 1
- Not support alpha transparency WebP for current PHP version.
- RDIERROR_SRC_WEBP_ANIMATED_NOTSUPPORTED = 22
- Not support animated WebP for current PHP version (or current ImageMatick version).
- RDIERROR_WMI_NOTEXISTS = 11
- Watermark image not exists.
- RDIERROR_WMI_UNKNOWIMG = 10
- Unable to set watermark from this kind of image.
- RDIERROR_WMT_FONT_NOTEXISTS = 17
- Watermark text font file is not exists.
Properties
- $allow_resize_larger : bool
- Allow to set resize larger than source image.
- $destination_image_height : int
- $destination_image_width : int
- $jpg_quality : int
- JPEG & WebP quality.
- $last_modified_image_height : int
- $last_modified_image_width : int
- $master_dim : string
- Master dimension.
- $png_quality : int
- PNG quality.
- $source_image_data : array<string|int, mixed>
- $source_image_ext : string
- $source_image_height : int
- $source_image_mime : string
- $source_image_path : string
- $source_image_type : string
- $source_image_width : int
- $status : bool
- Contain status of action methods.
- $status_msg : string
- Contain status error message of action methods.
- $statusCode : int
- Contain status code refer from class constants.
- $watermark_image_height : int
- $watermark_image_type : string
- $watermark_image_width : int
- $wmTextBottomPadding : int
- Add bottom padding to watermark text to let characters that long to the bottom can be displayed. Example p, g, ฤ, ฎ, etc.<br> This bottom padding should be different for each font size.
- $wmTextBoundingBoxYPadding : int
- Watermark text bounding box vertical (Y) padding.
- $destination_image_height : int
- $destination_image_width : int
- $last_modified_image_height : int
- $last_modified_image_width : int
- $source_image_ext : string
- $source_image_height : int
- $source_image_mime : string
- $source_image_path : string
- $source_image_type : string
- $source_image_width : int
- $watermark_image_height : int
- $watermark_image_type : string
- $watermark_image_width : int
Methods
- __construct() : mixed
- Class constructor.
- __destruct() : mixed
- Class de-constructor.
- __get() : mixed
- Magic get
- __set() : mixed
- Magic set
- clear() : bool
- Clear and reset processed results to make it ready for new process while keep the original source.
- getImageFileData() : array<string|int, mixed>|false
- Get image file data such as width, height, mime type, extension.
- getImageSize() : array<string|int, mixed>
- Get source image size
- resize() : bool
- Resize the image.
- buildSourceImageData() : bool
- Build source image data
- calculateCounterClockwise() : int
- Calculate counter clockwise degree.
- calculateStartXOfCenter() : int
- Calculate startX position of center.
- calculateWatermarkImageStartXY() : array<string|int, mixed>
- Calculate watermark start X and Y from specified text position (left, right, bottom, top, middle, center).
- convertAlpha127ToRgba() : string
- Convert alpha number (0 - 127) to rgba value (1.00 - 0.00).
- isClassSetup() : bool
- Verify that is class setup properly.
- isPreviousError() : bool
- Check is previous operation contain error?
- normalizeDegree() : int|string
- Normalize degree.
- normalizeWidthHeight() : array<string|int, mixed>
- Convert width, height to integer and check if it is less than 0 then set to minimum value.
- setErrorMessage() : mixed
- Set error message and code. This will be set statusXxx properties.
- setStatusSuccess() : mixed
- Set status as success and remove error message, error code.
- verifyQualityValue() : mixed
- Verify image save quality value.
- normalizeWatermarkOptions() : void
- Normalize watermark options.
Constants
RDIERROR_CALCULATEFAILED_USE_RESIZENORATIO
Unable to calculated, please try to calculate on your own and use `resizeNoRatio()` instead.
public
mixed
RDIERROR_CALCULATEFAILED_USE_RESIZENORATIO
= 4
RDIERROR_CROP_UNKNOWNIMG
Unable to crop this kind of image.
since 3.1.4 There is no need to use this anymore.
public
mixed
RDIERROR_CROP_UNKNOWNIMG
= 5
RDIERROR_FLIP_NOTSUPPORTED
Not support flip for current PHP version. (PHP 5.5 or newer is required to use GD flip.)
public
mixed
RDIERROR_FLIP_NOTSUPPORTED
= 9
RDIERROR_FLIP_UNKNOWIMG
Unable to flip this kind of image.
since 3.1.4 There is no need to use this anymore.
public
mixed
RDIERROR_FLIP_UNKNOWIMG
= 8
RDIERROR_IMAGEMAGICK_NOTMEETREQUIREMENT
ImageMagick version does not meet requirement.
public
mixed
RDIERROR_IMAGEMAGICK_NOTMEETREQUIREMENT
= 21
RDIERROR_IMAGICK_NOTLOAD
Imagick extension was not loaded.
public
mixed
RDIERROR_IMAGICK_NOTLOAD
= 18
RDIERROR_IMAGICK_NOTMEETREQUIREMENT
Imagick version does not meet requirement.
public
mixed
RDIERROR_IMAGICK_NOTMEETREQUIREMENT
= 19
RDIERROR_IMAGICK_VERSIONUNKNOW
Could not verify Imagick version.
public
mixed
RDIERROR_IMAGICK_VERSIONUNKNOW
= 20
RDIERROR_RESIZE_UNKNOWIMG
Unable to resize this kind of image.
since 3.1.4 There is no need to use this anymore.
public
mixed
RDIERROR_RESIZE_UNKNOWIMG
= 6
RDIERROR_ROTATE_UNKNOWIMG
Unable to rotate this kind of image.
since 3.1.4 There is no need to use this anymore.
public
mixed
RDIERROR_ROTATE_UNKNOWIMG
= 7
RDIERROR_SAVE_FAILED
Failed to save an image.
public
mixed
RDIERROR_SAVE_FAILED
= 13
RDIERROR_SAVE_UNSUPPORT
Unable to save to this extension. or target file extension is not supported.
public
mixed
RDIERROR_SAVE_UNSUPPORT
= 12
RDIERROR_SHOW_FAILED
Failed to show an image to browser.
public
mixed
RDIERROR_SHOW_FAILED
= 15
RDIERROR_SHOW_UNSUPPORT
Unable to show image in this extension. File extension is not supported.
public
mixed
RDIERROR_SHOW_UNSUPPORT
= 14
RDIERROR_SRC_NOTEXISTS
Source file is not exists.
public
mixed
RDIERROR_SRC_NOTEXISTS
= 3
RDIERROR_SRC_NOTIMAGE
Source file is not image.
public
mixed
RDIERROR_SRC_NOTIMAGE
= 2
RDIERROR_SRC_UNKNOWN
Unable to set source from this kind of image.
public
mixed
RDIERROR_SRC_UNKNOWN
= 16
RDIERROR_SRC_WEBP_ALPHA_NOTSUPPORTED
Not support alpha transparency WebP for current PHP version.
public
mixed
RDIERROR_SRC_WEBP_ALPHA_NOTSUPPORTED
= 1
RDIERROR_SRC_WEBP_ANIMATED_NOTSUPPORTED
Not support animated WebP for current PHP version (or current ImageMatick version).
public
mixed
RDIERROR_SRC_WEBP_ANIMATED_NOTSUPPORTED
= 22
Tags
RDIERROR_WMI_NOTEXISTS
Watermark image not exists.
public
mixed
RDIERROR_WMI_NOTEXISTS
= 11
RDIERROR_WMI_UNKNOWIMG
Unable to set watermark from this kind of image.
public
mixed
RDIERROR_WMI_UNKNOWIMG
= 10
RDIERROR_WMT_FONT_NOTEXISTS
Watermark text font file is not exists.
public
mixed
RDIERROR_WMT_FONT_NOTEXISTS
= 17
Properties
$allow_resize_larger
Allow to set resize larger than source image.
public
bool
$allow_resize_larger
= false
Set to true to allow, false to disallow. Default is false.
$destination_image_height write-only
public
int
$destination_image_height
Destination image height. For internal use only.
$destination_image_width write-only
public
int
$destination_image_width
Destination image width. For internal use only.
$jpg_quality
JPEG & WebP quality.
public
int
$jpg_quality
= 100
Quality from 0 (worst quality, smallest file) to 100 (best quality, biggest file). Default is 100.
$last_modified_image_height write-only
public
int
$last_modified_image_height
Last modified image height. For internal use only.
$last_modified_image_width write-only
public
int
$last_modified_image_width
Last modified image width. For internal use only.
$master_dim
Master dimension.
public
string
$master_dim
= 'auto'
Master dimension value is 'auto', 'width', or 'height'. Default is 'auto'.
$png_quality
PNG quality.
public
int
$png_quality
= 0
Compression level from 0 (no compression) to 9. Default is 0.
$source_image_data
public
array<string|int, mixed>
$source_image_data
Source image data.
Tags
$source_image_ext write-only
public
string
$source_image_ext
Source image file extension. For internal use only.
$source_image_height write-only
public
int
$source_image_height
Source image height. For internal use only.
$source_image_mime write-only
public
string
$source_image_mime
Source image mime type. Example image/jpeg. For internal use only.
$source_image_path write-only
public
string
$source_image_path
Real full path to source image file. For internal use only.
$source_image_type write-only
public
string
$source_image_type
Source image type. Use with PHP predefined constants IMAGETYPE_XXX only. For internal use only.
$source_image_width write-only
public
int
$source_image_width
Source image width. For internal use only.
$status
Contain status of action methods.
public
bool
$status
= false
Return false if there is something error.
$status_msg
Contain status error message of action methods.
public
string
$status_msg
= null
Return error message. Default is null.
$statusCode
Contain status code refer from class constants.
public
int
$statusCode
= null
Return status code. See class constants. Default is null.
$watermark_image_height write-only
public
int
$watermark_image_height
Watermark image height. For internal use only.
$watermark_image_type write-only
public
string
$watermark_image_type
Watermark image type. Use with PHP predefined constants IMAGETYPE_XXX only. For internal use only.
$watermark_image_width write-only
public
int
$watermark_image_width
Watermark image width. For internal use only.
$wmTextBottomPadding
Add bottom padding to watermark text to let characters that long to the bottom can be displayed. Example p, g, ฤ, ฎ, etc.<br> This bottom padding should be different for each font size.
public
int
$wmTextBottomPadding
= 5
Set the number of pixels to add to watermark text at the bottom. Default is 5, previous version was 5.
$wmTextBoundingBoxYPadding
Watermark text bounding box vertical (Y) padding.
public
int
$wmTextBoundingBoxYPadding
= 0
Set the number of watermark text bounding box padding. Default is 0.
$destination_image_height
protected
int
$destination_image_height
Destination image height.
$destination_image_width
protected
int
$destination_image_width
Destination image width.
$last_modified_image_height
protected
int
$last_modified_image_height
Last modified image height.
$last_modified_image_width
protected
int
$last_modified_image_width
Last modified image width.
$source_image_ext
protected
string
$source_image_ext
Source image file extension.
$source_image_height
protected
int
$source_image_height
Source image height.
$source_image_mime
protected
string
$source_image_mime
Source image mime type. Example image/jpeg.
$source_image_path
protected
string
$source_image_path
= ''
Real full path to source image file.
$source_image_type
protected
string
$source_image_type
Source image type. Use with PHP predefined constants IMAGETYPE_XXX only.
Tags
$source_image_width
protected
int
$source_image_width
Source image width.
$watermark_image_height
protected
int
$watermark_image_height
Watermark image height.
$watermark_image_type
protected
string
$watermark_image_type
Watermark image type. Use with PHP predefined constants IMAGETYPE_XXX only.
Tags
$watermark_image_width
protected
int
$watermark_image_width
Watermark image width.
Methods
__construct()
Class constructor.
public
__construct(string $source_image_path) : mixed
Parameters
- $source_image_path : string
-
Path to source image file.
__destruct()
Class de-constructor.
public
__destruct() : mixed
__get()
Magic get
public
__get(string $name) : mixed
Parameters
- $name : string
__set()
Magic set
public
__set(string $name, mixed $value) : mixed
Parameters
- $name : string
- $value : mixed
clear()
Clear and reset processed results to make it ready for new process while keep the original source.
public
clear() : bool
This method will be reset all properties that is commonly use but not reset specific properties for GD, Imagick.
The properties that will be clear is based on ImageInterface interface that was described.
Return values
bool —Return true on successfully cleared.
getImageFileData()
Get image file data such as width, height, mime type, extension.
public
getImageFileData(string $imagePath) : array<string|int, mixed>|false
This will be use getimagesize() function if supported, something else as backup.
Parameters
- $imagePath : string
-
Full path to image file.
Tags
Return values
array<string|int, mixed>|false —Return array:
index 0 Image width.
index 1 Image height.
index 2 Image type constant. See more at https://www.php.net/manual/en/image.constants.php
mime key is mime type.
ext key is file extension with dot (.ext).
Other array keys are optional, it can be omit.
Return false on failure.
getImageSize()
Get source image size
public
getImageSize() : array<string|int, mixed>
Return values
array<string|int, mixed> —Return array of image size with 'width' and 'height' in array key.
resize()
Resize the image.
public
resize(mixed $width, mixed $height) : bool
Parameters
- $width : mixed
-
Image width that image will be resize to.
- $height : mixed
-
Image height that image will be resize to.
Return values
bool —Return true on success, false on failed. Call to status_msg property to see the details on failure.
buildSourceImageData()
Build source image data
protected
buildSourceImageData(string $source_image_path) : bool
Parameters
- $source_image_path : string
-
Path to source image file.
Return values
bool —Return true on success, false on failure. Call to statusCode or status_msg property to see the details on failure.
calculateCounterClockwise()
Calculate counter clockwise degree.
protected
calculateCounterClockwise(int $value) : int
Calculate input degree for use with Imagick.
In GD 90 degrees is 270 in Imagick. This function help to make it working together very well.
So it will be...
input = output
0 = 0
1 = 359
10 = 350
90 = 270
180 = 180
270 = 90
360 = 0
Parameters
- $value : int
-
Input degrees.
Return values
int —Return opposite degrees for use with Imagick.
calculateStartXOfCenter()
Calculate startX position of center.
protected
calculateStartXOfCenter(int $objWidth, int $canvasWidth) : int
With this method, you can calculate the start x position of center (horizontal) or middle (vertical).
The purpose of this method is for internal use, do not call this method from image's driver class directly due to it can be changed in the future.
Formular: round((half of canvas) - (half of object))
Parameters
- $objWidth : int
-
Destination image object size.
- $canvasWidth : int
-
Canvas size.
Return values
int —Calculated size.
calculateWatermarkImageStartXY()
Calculate watermark start X and Y from specified text position (left, right, bottom, top, middle, center).
protected
calculateWatermarkImageStartXY(string $wmStartX, string $wmStartY, int $imgWidth, int $imgHeight, int $wmWidth, int $wmHeight[, array<string|int, mixed> $options = [] ]) : array<string|int, mixed>
The purpose of this method is for internal use, do not call this method from image's driver class directly due to it can be changed in the future.
Parameters
- $wmStartX : string
-
Position to begin in x axis. The value is 'left', 'center', 'right'.
- $wmStartY : string
-
Position to begin in y axis. The value is 'top', 'middle', 'bottom'.
- $imgWidth : int
-
Latest image width. Get the dimension from latest modified image only.
- $imgHeight : int
-
Latest image height. Get the dimension from latest modified image only.
- $wmWidth : int
-
Latest watermark image width. Get the dimension from latest modified image only.
- $wmHeight : int
-
Latest watermark image height. Get the dimension from latest modified image only.
- $options : array<string|int, mixed> = []
-
The watermark options. This argument must be normalized before calling this method. Value is associative array.
padding(int) Padding around watermark object. Use with left, right, bottom, top but not middle, center.
Tags
Return values
array<string|int, mixed> —Return array with width as index 0, height as index 1.
convertAlpha127ToRgba()
Convert alpha number (0 - 127) to rgba value (1.00 - 0.00).
protected
convertAlpha127ToRgba(int $number) : string
The purpose of this method is for internal use, do not call this method from image's driver class directly due to it can be changed in the future.
Parameters
- $number : int
-
Alpha number (0 to 127). 127 is completely transparent.
Return values
string —Return RGBA value (1.00 to 0.00). 0.00 is completely transparent.
isClassSetup()
Verify that is class setup properly.
protected
isClassSetup() : bool
If image source was not found then it will not setup properly.
Return values
bool —Return true on success, return false on failed.
isPreviousError()
Check is previous operation contain error?
protected
isPreviousError() : bool
Return values
bool —Return true if there is some error, false if there is not.
normalizeDegree()
Normalize degree.
protected
normalizeDegree(int|string $degree) : int|string
If degree is allowed string then it will be return as-is. If degree is number then it will be convert to integer.
Parameters
- $degree : int|string
-
The degree.
Return values
int|string —Return converted degree.
normalizeWidthHeight()
Convert width, height to integer and check if it is less than 0 then set to minimum value.
protected
normalizeWidthHeight(int $width, int $height[, int|false $minWidth = 100 ][, int|false $minHeight = 100 ]) : array<string|int, mixed>
Parameters
- $width : int
-
Width
- $height : int
-
Height
- $minWidth : int|false = 100
-
Minimum that width must not less than 0. Set to
falseto not check it. Default is 100. - $minHeight : int|false = 100
-
Minimum that height must not less than 0. Set to
falseto not check it. Default is 100.
Return values
array<string|int, mixed> —Return 2D array where first index is width, second index is height.
setErrorMessage()
Set error message and code. This will be set statusXxx properties.
protected
setErrorMessage(string $errorMessage, int $errorCode) : mixed
Parameters
- $errorMessage : string
-
The error message.
- $errorCode : int
-
The error code, usually it is class's constant.
Tags
setStatusSuccess()
Set status as success and remove error message, error code.
protected
setStatusSuccess() : mixed
Tags
verifyQualityValue()
Verify image save quality value.
protected
verifyQualityValue() : mixed
Tags
normalizeWatermarkOptions()
Normalize watermark options.
private
normalizeWatermarkOptions(array<string|int, mixed> &$options) : void
Parameters
- $options : array<string|int, mixed>
-
The watermark options to be altered.