Image
Image Asset class. Provides methods to manipulate images, such as resizing, cropping, converting, and generating data URLs. This class uses the Intervention Image library to handle image processing. It supports both GD and Imagick drivers, depending on the available PHP extensions.
Table of Contents
Methods
- buildHtmlSrcset() : string
- Alias of buildHtmlSrcsetW for backward compatibility.
- buildHtmlSrcsetW() : string
- Build the `srcset` HTML attribute for responsive images, based on widths.
- buildHtmlSrcsetX() : string
- Build the `srcset` HTML attribute for responsive images, based on pixel ratios.
- convert() : string
- Converts an image Asset to the target format.
- getBackgroundColor() : string
- Returns the background RGB color of an image asset.
- getDataUrl() : string
- Returns the Data URL (encoded in Base64).
- getDominantColor() : string
- Returns the dominant RGB color of an image asset.
- getHtmlSizes() : string
- Returns the value from the `$sizes` array if the class exists, otherwise returns the default size.
- getLqip() : string
- Returns a Low Quality Image Placeholder (LQIP) as data URL.
- getSvgAttributes() : SimpleXMLElement|false
- Returns SVG attributes.
- isAnimatedGif() : bool
- Checks if an asset is an animated GIF.
- isIco() : bool
- Returns true if asset is an ICO.
- isImage() : bool
- Asset is a valid image?
- isSVG() : bool
- Returns true if asset is a SVG.
- maskable() : string
- Makes an image Asset maskable, meaning it can be used as a PWA icon.
- resize() : string
- Resizes an image Asset to the given width or/and height.
- manager() : ImageManager
- Create new manager instance with available driver.
Methods
buildHtmlSrcset()
Alias of buildHtmlSrcsetW for backward compatibility.
public
static buildHtmlSrcset(Asset $asset, array<string|int, mixed> $widths[, mixed $notEmpty = false ]) : string
Parameters
- $asset : Asset
- $widths : array<string|int, mixed>
- $notEmpty : mixed = false
Return values
stringbuildHtmlSrcsetW()
Build the `srcset` HTML attribute for responsive images, based on widths.
public
static buildHtmlSrcsetW(Asset $asset, array<string|int, mixed> $widths[, bool $notEmpty = false ]) : string
e.g.: srcset="/img-480.jpg 480w, /img-800.jpg 800w".
Parameters
- $asset : Asset
- $widths : array<string|int, mixed>
-
An array of widths to include in the
srcset - $notEmpty : bool = false
-
If true the source image is always added to the
srcset
Tags
Return values
stringbuildHtmlSrcsetX()
Build the `srcset` HTML attribute for responsive images, based on pixel ratios.
public
static buildHtmlSrcsetX(Asset $asset, int $width1x, array<string|int, mixed> $ratios) : string
e.g.: srcset="/img-1x.jpg 1.0x, /img-2x.jpg 2.0x".
Parameters
- $asset : Asset
- $width1x : int
-
The width of the 1x image
- $ratios : array<string|int, mixed>
-
An array of pixel ratios to include in the
srcset
Tags
Return values
stringconvert()
Converts an image Asset to the target format.
public
static convert(Asset $asset, string $format, int $quality) : string
Parameters
- $asset : Asset
- $format : string
- $quality : int
Tags
Return values
stringgetBackgroundColor()
Returns the background RGB color of an image asset.
public
static getBackgroundColor(Asset $asset) : string
Parameters
- $asset : Asset
Tags
Return values
stringgetDataUrl()
Returns the Data URL (encoded in Base64).
public
static getDataUrl(Asset $asset, int $quality) : string
Parameters
- $asset : Asset
- $quality : int
Tags
Return values
stringgetDominantColor()
Returns the dominant RGB color of an image asset.
public
static getDominantColor(Asset $asset) : string
Parameters
- $asset : Asset
Tags
Return values
stringgetHtmlSizes()
Returns the value from the `$sizes` array if the class exists, otherwise returns the default size.
public
static getHtmlSizes(string $class[, array<string|int, mixed> $sizes = [] ]) : string
Parameters
- $class : string
- $sizes : array<string|int, mixed> = []
Return values
stringgetLqip()
Returns a Low Quality Image Placeholder (LQIP) as data URL.
public
static getLqip(Asset $asset) : string
Parameters
- $asset : Asset
Tags
Return values
stringgetSvgAttributes()
Returns SVG attributes.
public
static getSvgAttributes(Asset $asset) : SimpleXMLElement|false
Parameters
- $asset : Asset
Return values
SimpleXMLElement|falseisAnimatedGif()
Checks if an asset is an animated GIF.
public
static isAnimatedGif(Asset $asset) : bool
Parameters
- $asset : Asset
Return values
boolisIco()
Returns true if asset is an ICO.
public
static isIco(Asset $asset) : bool
Parameters
- $asset : Asset
Return values
boolisImage()
Asset is a valid image?
public
static isImage(Asset $asset) : bool
Parameters
- $asset : Asset
Return values
boolisSVG()
Returns true if asset is a SVG.
public
static isSVG(Asset $asset) : bool
Parameters
- $asset : Asset
Return values
boolmaskable()
Makes an image Asset maskable, meaning it can be used as a PWA icon.
public
static maskable(Asset $asset, int $quality, int $padding) : string
Parameters
- $asset : Asset
- $quality : int
- $padding : int
Tags
Return values
stringresize()
Resizes an image Asset to the given width or/and height.
public
static resize(Asset $asset[, int|null $width = null ][, int|null $height = null ][, int $quality = 75 ][, bool $rmAnimation = false ]) : string
If both width and height are provided, the image is cropped to fit the dimensions. If only one dimension is provided, the image is scaled proportionally. The $rmAnimation parameter can be set to true to remove animations from animated images (e.g., GIFs).
Parameters
- $asset : Asset
- $width : int|null = null
- $height : int|null = null
- $quality : int = 75
- $rmAnimation : bool = false
Tags
Return values
stringmanager()
Create new manager instance with available driver.
private
static manager() : ImageManager