Watermark
extends AbstractGdCommand
in package
uses
CalculationTrait, GdTrait, ImageTrait
Watermark image and text.
Tags
Table of Contents
Properties
Methods
- __construct() : mixed
- applyImage() : bool
- Apply watermark image to source image.
- applyText() : bool
- Apply watermark text to source image.
- getImageFileData() : array<string|int, mixed>|false
- Get image file data such as width, height, mime type, extension.
- 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).
- fillTransparentOnObject() : mixed
- Fill transparent-white on selected image object.
- fillWhiteBgOnDestination() : mixed
- Fill white background on destination image object.
- isResourceOrGDObject() : bool
- Check if image variable is resource of GD or is object of `\GdImage` (PHP 8.0) or not.
- 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.
- setupSourceFromFile() : GdImage|resource|false
- Setup source image object (GD) from file.
- verifyQualityValue() : mixed
- Verify image save quality value.
- createWatermarkTextObject() : array<string|int, mixed>
- Create watermark text object.
- getImageColorAlpha() : mixed
- Get image color with alpha value (transparency).
- imagecopymergeAlphaPreserve() : mixed
- Merge a source image onto the destination with a forced opacity level, preserving the alpha channel of both images.
- normalizeStartPosition() : array<string|int, mixed>
- Normalize start X and Y position.
- normalizeWatermarkOptions() : void
- Normalize watermark options.
- setupWatermarkImageObject() : bool
- Setup watermark image object.
Properties
$Gd
protected
Gd
$Gd
Methods
__construct()
public
__construct(Gd $Gd) : mixed
Parameters
- $Gd : Gd
applyImage()
Apply watermark image to source image.
public
applyImage(string $wm_img_path[, int|string $wm_img_start_x = 0 ][, int|string $wm_img_start_y = 0 ][, array<string|int, mixed> $options = [] ]) : bool
Parameters
- $wm_img_path : string
-
Full path of watermark image file
- $wm_img_start_x : int|string = 0
-
Position to begin in x axis. The value is integer or 'left', 'center', 'right'.
- $wm_img_start_y : int|string = 0
-
Position to begin in y axis. The value is integer or 'top', 'middle', 'bottom'.
- $options : array<string|int, mixed> = []
-
The watermark options. (Since v3.1.3)
padding(int) Padding around watermark object. Use with left, right, bottom, top but not middle, center. See\Rundiz\Image\Traits\CalculationTrait::calculateWatermarkImageStartXY().
opacity(int) The image opacity value from 0 (full transparent) to 100 (no transparent).
Tags
Return values
bool —Return true on success, false on failure. Call to status_msg property to see the details on failure.
applyText()
Apply watermark text to source image.
public
applyText(string $wm_txt_text, string $wm_txt_font_path[, int|string $wm_txt_start_x = 0 ][, int|string $wm_txt_start_y = 0 ][, int $wm_txt_font_size = 10 ][, string $wm_txt_font_color = 'white' ][, int $wm_txt_font_alpha = 60 ][, array<string|int, mixed> $options = [] ]) : bool
Parameters
- $wm_txt_text : string
-
Watermark text
- $wm_txt_font_path : string
-
'True Type Font' path
- $wm_txt_start_x : int|string = 0
-
Position to begin in x axis. The value is integer or 'left', 'center', 'right'.
- $wm_txt_start_y : int|string = 0
-
Position to begin in x axis. The value is integer or 'top', 'middle', 'bottom'.
- $wm_txt_font_size : int = 10
-
Font size
- $wm_txt_font_color : string = 'white'
-
Font color. ('black', 'white', 'red', 'green', 'blue', 'yellow', 'cyan', 'magenta')
- $wm_txt_font_alpha : int = 60
-
Text transparency value from 0 (no transparent) to 127 (full transparent).
- $options : array<string|int, mixed> = []
-
The watermark text options. (Since v.3.1.0)
fillBackground(bool) Set totrueto fill background color for text bounding box. Default isfalseto use transparent.
backgroundColor(string) The background color to fill for text bounding box. Available values are 'black', 'white', 'red', 'green', 'blue', 'yellow', 'cyan', 'magenta', 'debug'.
padding(int) (Since v3.1.3) Padding around watermark text. Use with left, right, bottom, top but not middle, center.
Tags
Return values
bool —Return true on success, false on failed. Call to status_msg property to see the details on failure.
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.
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.
fillTransparentOnObject()
Fill transparent-white on selected image object.
protected
fillTransparentOnObject(GdImage|resource $image) : mixed
Parameters
- $image : GdImage|resource
-
Image object to fill color.
Tags
fillWhiteBgOnDestination()
Fill white background on destination image object.
protected
fillWhiteBgOnDestination() : mixed
Create temporary canvas, fill white background, copy image from "destination image object" to temp canvas, then mark this canvas as "destination image object".
isResourceOrGDObject()
Check if image variable is resource of GD or is object of `\GdImage` (PHP 8.0) or not.
protected
isResourceOrGDObject(mixed $image) : bool
Parameters
- $image : mixed
-
The
\GdImagevariable to check.
Tags
Return values
bool —Return true if it is resource or instance of \GdImage, return false if it 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
setupSourceFromFile()
Setup source image object (GD) from file.
protected
setupSourceFromFile(string $file, int $imageType) : GdImage|resource|false
Parameters
- $file : string
-
Source image file path.
- $imageType : int
-
Image type as defined in
IMAGETYPE_XXXby PHP. This value may have got fromgetImageFileData()method.
Tags
Return values
GdImage|resource|false —Return resource or \GdImage (for PHP 8.0+) on success, false on failure.
verifyQualityValue()
Verify image save quality value.
protected
verifyQualityValue() : mixed
Tags
createWatermarkTextObject()
Create watermark text object.
private
createWatermarkTextObject(int $wm_txt_width, int $wm_txt_height, string $wm_txt_font_color, int $wm_txt_font_alpha[, array<string|int, mixed> $options = [] ]) : array<string|int, mixed>
Parameters
- $wm_txt_width : int
- $wm_txt_height : int
- $wm_txt_font_color : string
- $wm_txt_font_alpha : int
- $options : array<string|int, mixed> = []
Tags
Return values
array<string|int, mixed> —Return indexed array:
index 0 (resource|\GdImage|false) Resource or object of text canvas.
index 1 (object) Color identifier.
getImageColorAlpha()
Get image color with alpha value (transparency).
private
getImageColorAlpha(string $colorName, int $alpha, GdImage|resource $image) : mixed
Parameters
- $colorName : string
-
The color name. Supported 'black', 'white', 'red', 'green', 'blue', 'yellow', 'cyan', 'magenta'.
- $alpha : int
-
The alpha value from 0 (no transparent) to 127 (full transparent).
- $image : GdImage|resource
-
The GD image object or resource.
Tags
imagecopymergeAlphaPreserve()
Merge a source image onto the destination with a forced opacity level, preserving the alpha channel of both images.
private
imagecopymergeAlphaPreserve(GdImage|resource $dst, GdImage|resource $src, int $dstX, int $dstY, int $srcX, int $srcY, int $width, int $height[, int $opacity = 100 ]) : mixed
Drop-in replacement for imagecopymerge(), which ignores alpha and corrupts transparency when the destination is not fully opaque. Each source pixel's own alpha is multiplied by the forced opacity, then composited over the destination using source-over blending, so transparent areas of the destination stay transparent.
Both images are converted to truecolor in place if they are palette based. The destination's alpha blending is disabled and its alpha channel is flagged to be saved.
Parameters
- $dst : GdImage|resource
-
Destination image, modified in place.
- $src : GdImage|resource
-
Source image to merge onto the destination.
- $dstX : int
-
X coordinate in the destination to start merging at.
- $dstY : int
-
Y coordinate in the destination to start merging at.
- $srcX : int
-
X coordinate in the source to start reading from.
- $srcY : int
-
Y coordinate in the source to start reading from.
- $width : int
-
Width of the region to merge, in pixels.
- $height : int
-
Height of the region to merge, in pixels.
- $opacity : int = 100
-
Forced opacity of the source, 0 (full transparent) to 100 (no transparent).
Tags
normalizeStartPosition()
Normalize start X and Y position.
private
normalizeStartPosition(int|string $wm_img_start_x, int|string $wm_img_start_y[, int|null $watermark_width = null ][, int|null $watermark_height = null ][, array<string|int, mixed> $options = [] ]) : array<string|int, mixed>
Parameters
- $wm_img_start_x : int|string
- $wm_img_start_y : int|string
- $watermark_width : int|null = null
-
Watermark width. Set to
nullto automatically get it from image object. - $watermark_height : int|null = null
-
Watermark height. Set to
nullto automatically get it from image object. - $options : array<string|int, mixed> = []
Tags
Return values
array<string|int, mixed> —Return indexed array where first array is start X, second is start Y.
normalizeWatermarkOptions()
Normalize watermark options.
private
normalizeWatermarkOptions(array<string|int, mixed> &$options) : void
Parameters
- $options : array<string|int, mixed>
-
The watermark options to be altered.
Tags
setupWatermarkImageObject()
Setup watermark image object.
private
setupWatermarkImageObject(string $wm_img_path) : bool
Parameters
- $wm_img_path : string
-
Path to watermark image.
Return values
bool —Return true on success, false on failed. Call to status_msg property to see the details on failure.