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
- Build the `srcset` HTML attribute for responsive images.
- convert() : string
- Converts an image Asset to the target format.
- cover() : string
- Crops an image Asset to the given width and height, keeping the aspect ratio.
- 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
- Scales down an image Asset to the given width, keeping the aspect ratio.
- manager() : ImageManager
- Create new manager instance with available driver.
Methods
buildHtmlSrcset()
Build the `srcset` HTML attribute for responsive images.
public
static buildHtmlSrcset(Asset $asset, array<string|int, mixed> $widths) : string
e.g.: srcset="/img-480.jpg 480w, /img-800.jpg 800w"
.
Parameters
- $asset : Asset
- $widths : array<string|int, mixed>
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
stringcover()
Crops an image Asset to the given width and height, keeping the aspect ratio.
public
static cover(Asset $asset, int $width, int $height, int $quality) : string
Parameters
- $asset : Asset
- $width : int
- $height : int
- $quality : int
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()
Scales down an image Asset to the given width, keeping the aspect ratio.
public
static resize(Asset $asset, int $width, int $quality) : string
Parameters
- $asset : Asset
- $width : int
- $quality : int
Tags
Return values
stringmanager()
Create new manager instance with available driver.
private
static manager() : ImageManager