Asset implements ArrayAccess
Asset class. Represents an asset (file) in the Cecil project. Handles file locating, content reading, compiling, minifying, fingerprinting, resizing images, and more.
Table of Contents
Interfaces
- ArrayAccess
Constants
- IMAGE_THUMB = 'thumbnails'
Properties
- $builder : Builder
- $cacheTags : array<string|int, mixed>
- $config : Config
- $data : array<string|int, mixed>
- $cacheTags : array<string|int, mixed>
Methods
- __construct() : mixed
- Creates an Asset from a file path, an array of files path or an URL.
- __toString() : string
- Returns path.
- avif() : self
- Converts an image asset to AVIF format.
- buildPathFromUrl() : string
- Builds a relative path from a URL.
- compile() : self
- Compiles a SCSS + cache.
- convert() : self
- Converts an image asset to $format format.
- cover() : self
- Crops the image to the specified width and height, keeping the specified position.
- dataurl() : string
- Returns the Data URL (encoded in Base64).
- fingerprint() : self
- Add hash to the file name + cache.
- getAudio() : Mp3Info
- Returns MP3 file infos.
- getIntegrity() : string
- Hashing content of an asset with the specified algo, sha384 by default.
- getVideo() : array<string|int, mixed>
- Returns MP4 file infos.
- isImageInCdn() : bool
- Is the asset an image and is it in CDN?
- minify() : self
- Minifying a CSS or a JS.
- offsetExists() : bool
- Implements \ArrayAccess.
- offsetGet() : mixed
- Implements \ArrayAccess.
- offsetSet() : void
- Implements \ArrayAccess.
- offsetUnset() : void
- Implements \ArrayAccess.
- resize() : self
- Scales down an image to a new $width.
- sanitize() : string
- Replaces some characters by '_'.
- save() : void
- Adds asset path to the list of assets to save.
- webp() : self
- Converts an image asset to WebP format.
- doCompile() : self
- Compiles a SCSS.
- doFingerprint() : self
- Add hash to the file name.
- doMinify() : self
- Minifying a CSS or a JS + cache.
- buildImageCdnUrl() : string
- Builds CDN image URL.
- checkImage() : void
- Checks if the asset is not missing and is typed as an image.
- deduplicateThumbPath() : string
- Remove redondant '/thumbnails/<width>/' in the path.
- getHeight() : int
- Returns the height of an image/SVG.
- getImageSize() : array<string|int, mixed>|false
- Returns image size informations.
- getRemoteFileContent() : string
- Try to get remote file content.
- getWidth() : int
- Returns the width of an image/SVG.
- locateFile() : array<string|int, mixed>
- Returns local file path and updated path, or throw an exception.
- optimize() : int
- Optimizing $filepath image.
Constants
IMAGE_THUMB
public
mixed
IMAGE_THUMB
= 'thumbnails'
Properties
$builder
protected
Builder
$builder
$cacheTags
protected
array<string|int, mixed>
$cacheTags
= []
Cache tags
$config
protected
Config
$config
$data
protected
array<string|int, mixed>
$data
= []
Methods
__construct()
Creates an Asset from a file path, an array of files path or an URL.
public
__construct(Builder $builder, string|array<string|int, mixed> $paths[, array<string|int, mixed>|null $options = null ]) : mixed
Options:
[
'filename' =>
Parameters
- $builder : Builder
- $paths : string|array<string|int, mixed>
- $options : array<string|int, mixed>|null = null
Tags
__toString()
Returns path.
public
__toString() : string
Return values
stringavif()
Converts an image asset to AVIF format.
public
avif([int|null $quality = null ]) : self
Parameters
- $quality : int|null = null
Tags
Return values
selfbuildPathFromUrl()
Builds a relative path from a URL.
public
static buildPathFromUrl(string $url) : string
Used for remote files.
Parameters
- $url : string
Return values
stringcompile()
Compiles a SCSS + cache.
public
compile() : self
Tags
Return values
selfconvert()
Converts an image asset to $format format.
public
convert(string $format[, int|null $quality = null ]) : self
Parameters
- $format : string
- $quality : int|null = null
Tags
Return values
selfcover()
Crops the image to the specified width and height, keeping the specified position.
public
cover(int $width, int $height[, string $position = 'center' ]) : self
Parameters
- $width : int
- $height : int
- $position : string = 'center'
Tags
Return values
selfdataurl()
Returns the Data URL (encoded in Base64).
public
dataurl() : string
Tags
Return values
stringfingerprint()
Add hash to the file name + cache.
public
fingerprint() : self
Return values
selfgetAudio()
Returns MP3 file infos.
public
getAudio() : Mp3Info
Tags
Return values
Mp3InfogetIntegrity()
Hashing content of an asset with the specified algo, sha384 by default.
public
getIntegrity([string $algo = 'sha384' ]) : string
Used for SRI (Subresource Integrity).
Parameters
- $algo : string = 'sha384'
Tags
Return values
stringgetVideo()
Returns MP4 file infos.
public
getVideo() : array<string|int, mixed>
Tags
Return values
array<string|int, mixed>isImageInCdn()
Is the asset an image and is it in CDN?
public
isImageInCdn() : bool
Return values
boolminify()
Minifying a CSS or a JS.
public
minify() : self
Return values
selfoffsetExists()
Implements \ArrayAccess.
public
offsetExists(mixed $offset) : bool
Parameters
- $offset : mixed
Attributes
- #[ReturnTypeWillChange]
Return values
booloffsetGet()
Implements \ArrayAccess.
public
offsetGet(mixed $offset) : mixed
Parameters
- $offset : mixed
Attributes
- #[ReturnTypeWillChange]
offsetSet()
Implements \ArrayAccess.
public
offsetSet(mixed $offset, mixed $value) : void
Parameters
- $offset : mixed
- $value : mixed
Attributes
- #[ReturnTypeWillChange]
offsetUnset()
Implements \ArrayAccess.
public
offsetUnset(mixed $offset) : void
Parameters
- $offset : mixed
Attributes
- #[ReturnTypeWillChange]
resize()
Scales down an image to a new $width.
public
resize(int $width) : self
Parameters
- $width : int
Tags
Return values
selfsanitize()
Replaces some characters by '_'.
public
static sanitize(string $string) : string
Parameters
- $string : string
Return values
stringsave()
Adds asset path to the list of assets to save.
public
save() : void
Tags
webp()
Converts an image asset to WebP format.
public
webp([int|null $quality = null ]) : self
Parameters
- $quality : int|null = null
Tags
Return values
selfdoCompile()
Compiles a SCSS.
protected
doCompile() : self
Tags
Return values
selfdoFingerprint()
Add hash to the file name.
protected
doFingerprint() : self
Return values
selfdoMinify()
Minifying a CSS or a JS + cache.
protected
doMinify() : self
Tags
Return values
selfbuildImageCdnUrl()
Builds CDN image URL.
private
buildImageCdnUrl() : string
Return values
stringcheckImage()
Checks if the asset is not missing and is typed as an image.
private
checkImage() : void
Tags
deduplicateThumbPath()
Remove redondant '/thumbnails/<width>/' in the path.
private
deduplicateThumbPath(string $path) : string
Parameters
- $path : string
Return values
stringgetHeight()
Returns the height of an image/SVG.
private
getHeight() : int
Tags
Return values
intgetImageSize()
Returns image size informations.
private
getImageSize() : array<string|int, mixed>|false
Tags
Return values
array<string|int, mixed>|falsegetRemoteFileContent()
Try to get remote file content.
private
getRemoteFileContent(string $path[, string|null $userAgent = null ]) : string
Returns file content or throw an exception.
Parameters
- $path : string
- $userAgent : string|null = null
Tags
Return values
stringgetWidth()
Returns the width of an image/SVG.
private
getWidth() : int
Tags
Return values
intlocateFile()
Returns local file path and updated path, or throw an exception.
private
locateFile(string $path[, string|null $fallback = null ][, string|null $userAgent = null ]) : array<string|int, mixed>
If $fallback path is set, it will be used if the remote file is not found.
Try to locate the file in: (1. remote file)
- assets
- themes/
/assets - static
- themes/
/static
Parameters
- $path : string
- $fallback : string|null = null
- $userAgent : string|null = null
Tags
Return values
array<string|int, mixed>optimize()
Optimizing $filepath image.
private
optimize(string $filepath, string $path, int $quality) : int
Returns the new file size.
Parameters
- $filepath : string
- $path : string
- $quality : int