Documentation

ImageTransformer
in package

FinalYes

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

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
self

addWatermark()

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
self

applyFilter()

public applyFilter(string $filter[, array<string|int, mixed> $options = [] ]) : self
Parameters
$filter : string
$options : array<string|int, mixed> = []
Return values
self

compress()

public compress([int $quality = 75 ][, string $format = 'webp' ]) : self
Parameters
$quality : int = 75
$format : string = 'webp'
Return values
self

convert()

public convert([string $format = 'webp' ][, int $quality = 80 ]) : self
Parameters
$format : string = 'webp'
$quality : int = 80
Return values
self

crop()

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
self

getBlob()

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
string

getDominantColor()

public getDominantColor([int $paletteSize = 8 ]) : string
Parameters
$paletteSize : int = 8
Return values
string

loadFromFile()

Load image from file path

public loadFromFile(string $path) : self
Parameters
$path : string
Return values
self

loadFromString()

Load image from binary string

public loadFromString(string $blob) : self
Parameters
$blob : string
Return values
self

resize()

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
self

rotate()

public rotate(float $degrees[, string $bgcolor = 'transparent' ]) : self
Parameters
$degrees : float
$bgcolor : string = 'transparent'
Return values
self

roundCorners()

public roundCorners(int $radius) : self
Parameters
$radius : int
Return values
self

save()

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
bool

toBase64()

public toBase64([string $format = 'webp' ][, int $quality = 80 ]) : string
Parameters
$format : string = 'webp'
$quality : int = 80
Return values
string

calculateContain()

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>
Return values
array<string|int, mixed>
On this page

Search results