ImageTransformer
in package
ImageTransformer
- Uses Imagick if available, otherwise falls back to GD.
- Preserves transparency where possible.
- Default export format: webp (if supported by runtime).
Methods return $this for chaining where relevant.
Table of Contents
Properties
- $gd : mixed
- $height : int
- $imagick : Imagick|null
- $loadedFormat : string|null
- $logger : LoggerInterface|null
- $path : string
- $useImagick : bool
- $width : int
Methods
- __construct() : mixed
- addText() : self
- Add text onto image.
- addWatermark() : self
- Add watermark image path or binary.
- applyFilter() : self
- clear() : void
- Clear resources
- compress() : self
- convert() : self
- crop() : self
- Crop to exact size from gravity: 'center'|'top'|'bottom'|'left'|'right'
- getAvgColor() : string
- getBlob() : string
- Convert to target format and return binary.
- getDominantColor() : string
- getHeight() : int
- getWidth() : int
- isUsingImagick() : bool
- loadFromFile() : self
- Load image from file path
- loadFromString() : self
- Load image from binary string
- resize() : self
- Resize with optional preserving aspect.
- rotate() : self
- roundCorners() : self
- save() : bool
- Save to file
- toBase64() : string
- calculateContain() : array<string|int, mixed>
- Calculate contain size
- colorAlloc() : mixed
- createTrueColor() : mixed
- gdImageCopyMergeAlpha() : mixed
- gravityToXY() : array<string|int, mixed>
- hexColorAllocate() : mixed
- positionCoords() : array<string|int, mixed>
- roundRect() : mixed
- textPositionFromOptions() : array<string|int, mixed>
Properties
$gd
private
mixed
$gd
$height
private
int
$height
= 0
$imagick
private
Imagick|null
$imagick
= null
$loadedFormat
private
string|null
$loadedFormat
= null
$logger
private
LoggerInterface|null
$logger
$path
private
string
$path
= 'unknown'
$useImagick
private
bool
$useImagick
= false
$width
private
int
$width
= 0
Methods
__construct()
public
__construct([LoggerInterface|null $logger = null ]) : mixed
Parameters
- $logger : LoggerInterface|null = null
addText()
Add text onto image.
public
addText(string $text[, array<string|int, mixed> $options = [] ]) : self
$options: [ fontFile => '/path/to.ttf', size => 16, color => '#ffffff', opacity => 1.0 (0..1), angle => 0, x => int|null, y => int|null, align => 'left'|'center'|'right', valign => 'top'|'middle'|'bottom' ]
Parameters
- $text : string
- $options : array<string|int, mixed> = []
Return values
selfaddWatermark()
Add watermark image path or binary.
public
addWatermark(string $wmPathOrBlob[, array<string|int, mixed> $options = [] ]) : self
$options: scale (0..1 relative to base), x/y, opacity (0..1), position: 'center'|'top-left'|...
Parameters
- $wmPathOrBlob : string
- $options : array<string|int, mixed> = []
Return values
selfapplyFilter()
public
applyFilter(string $filter[, array<string|int, mixed> $options = [] ]) : self
Parameters
- $filter : string
- $options : array<string|int, mixed> = []
Return values
selfclear()
Clear resources
public
clear() : void
compress()
public
compress([int $quality = 75 ][, string $format = 'webp' ]) : self
Parameters
- $quality : int = 75
- $format : string = 'webp'
Return values
selfconvert()
public
convert([string $format = 'webp' ][, int $quality = 80 ]) : self
Parameters
- $format : string = 'webp'
- $quality : int = 80
Return values
selfcrop()
Crop to exact size from gravity: 'center'|'top'|'bottom'|'left'|'right'
public
crop(int $w, int $h[, string $gravity = 'center' ]) : self
Parameters
- $w : int
- $h : int
- $gravity : string = 'center'
Return values
selfgetAvgColor()
public
getAvgColor() : string
Return values
stringgetBlob()
Convert to target format and return binary.
public
getBlob([string $format = 'webp' ][, int $quality = 80 ]) : string
$format: 'webp','png','jpeg','jpg','gif','avif'
Parameters
- $format : string = 'webp'
- $quality : int = 80
Return values
stringgetDominantColor()
public
getDominantColor([int $paletteSize = 8 ]) : string
Parameters
- $paletteSize : int = 8
Return values
stringgetHeight()
public
getHeight() : int
Return values
intgetWidth()
public
getWidth() : int
Return values
intisUsingImagick()
public
isUsingImagick() : bool
Return values
boolloadFromFile()
Load image from file path
public
loadFromFile(string $path) : self
Parameters
- $path : string
Return values
selfloadFromString()
Load image from binary string
public
loadFromString(string $blob) : self
Parameters
- $blob : string
Return values
selfresize()
Resize with optional preserving aspect.
public
resize(int $targetW, int $targetH[, string $mode = 'fit' ][, bool $allowUpscale = true ]) : self
$mode: 'fit' (contain), 'cover' (crop to fill), 'stretch' (ignore aspect)
Parameters
- $targetW : int
- $targetH : int
- $mode : string = 'fit'
- $allowUpscale : bool = true
Return values
selfrotate()
public
rotate(float $degrees[, string $bgcolor = 'transparent' ]) : self
Parameters
- $degrees : float
- $bgcolor : string = 'transparent'
Return values
selfroundCorners()
public
roundCorners(int $radius) : self
Parameters
- $radius : int
Return values
selfsave()
Save to file
public
save(string $path[, string $format = 'webp' ][, int $quality = 80 ]) : bool
Parameters
- $path : string
- $format : string = 'webp'
- $quality : int = 80
Return values
booltoBase64()
public
toBase64([string $format = 'webp' ][, int $quality = 80 ]) : string
Parameters
- $format : string = 'webp'
- $quality : int = 80
Return values
stringcalculateContain()
Calculate contain size
private
calculateContain(int $origW, int $origH, int $targetW, int $targetH, bool $allowUpscale) : array<string|int, mixed>
Parameters
- $origW : int
- $origH : int
- $targetW : int
- $targetH : int
- $allowUpscale : bool
Return values
array<string|int, mixed>colorAlloc()
private
colorAlloc(mixed $gd, string $hex) : mixed
Parameters
- $gd : mixed
- $hex : string
createTrueColor()
private
createTrueColor(int $w, int $h[, bool $preserveAlpha = false ]) : mixed
Parameters
- $w : int
- $h : int
- $preserveAlpha : bool = false
gdImageCopyMergeAlpha()
private
gdImageCopyMergeAlpha(mixed $dstImg, mixed $srcImg, mixed $dstX, mixed $dstY, mixed $srcX, mixed $srcY, mixed $srcW, mixed $srcH, mixed $pct) : mixed
Parameters
- $dstImg : mixed
- $srcImg : mixed
- $dstX : mixed
- $dstY : mixed
- $srcX : mixed
- $srcY : mixed
- $srcW : mixed
- $srcH : mixed
- $pct : mixed
gravityToXY()
private
gravityToXY(int $origW, int $origH, int $w, int $h, string $gravity) : array<string|int, mixed>
Parameters
- $origW : int
- $origH : int
- $w : int
- $h : int
- $gravity : string
Return values
array<string|int, mixed>hexColorAllocate()
private
hexColorAllocate(mixed $gd, string $hex[, float $opacity = 1.0 ]) : mixed
Parameters
- $gd : mixed
- $hex : string
- $opacity : float = 1.0
positionCoords()
private
positionCoords(int $wOverlay, int $hOverlay, string $position[, int|null $x = null ][, int|null $y = null ]) : array<string|int, mixed>
Parameters
- $wOverlay : int
- $hOverlay : int
- $position : string
- $x : int|null = null
- $y : int|null = null
Return values
array<string|int, mixed>roundRect()
private
roundRect(mixed $img, mixed $x1, mixed $y1, mixed $x2, mixed $y2, mixed $r, mixed $col) : mixed
Parameters
- $img : mixed
- $x1 : mixed
- $y1 : mixed
- $x2 : mixed
- $y2 : mixed
- $r : mixed
- $col : mixed
textPositionFromOptions()
private
textPositionFromOptions(int $textW, int $textH, array<string|int, mixed> $options) : array<string|int, mixed>
Parameters
- $textW : int
- $textH : int
- $options : array<string|int, mixed>