Cecil

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>

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.
dataurl()  : string
Returns the Data URL (encoded in Base64).
fingerprint()  : self
Add hash to the file name + cache.
getAudio()  : array<string|int, mixed>
Returns audio file infos: - duration (in seconds.microseconds) - bitrate (in bps) - channel ('stereo', 'dual_mono', 'joint_stereo' or 'mono')
getHeight()  : int|null
Returns the height of an image/SVG or a video.
getVideo()  : array<string|int, mixed>
Returns video file infos: - duration (in seconds) - width (in pixels) - height (in pixels)
getWidth()  : int|null
Returns the width of an image/SVG or a video.
integrity()  : string
Hashing content of an asset with the specified algo, sha384 by default.
isImageInCdn()  : bool
Is the asset an image and is it in CDN?
isMissing()  : bool
Checks if the asset is missing.
maskable()  : self
Creates a maskable image (with a padding = 20%).
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
Resizes an image to the given width or/and height.
sanitize()  : string
Replaces some characters by '_'.
save()  : void
Saves the asset by adding its path to the build assets list.
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(xheight)>/' in the path.
getImageSize()  : array<string|int, mixed>|false
Returns image size informations.
getRemoteFileContent()  : string
Try to get remote file content.
locateFile()  : array<string|int, mixed>
Returns local file path and updated path, or throw an exception.
optimizeImage()  : int
Optimizing $filepath image.

Constants

IMAGE_THUMB

public mixed IMAGE_THUMB = 'thumbnails'

Properties

$cacheTags

protected array<string|int, mixed> $cacheTags = []

Cache tags

$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' => , 'ignore_missing' => , 'fingerprint' => , 'minify' => , 'optimize' => , 'fallback' => , 'useragent' => , ]

Parameters
$builder : Builder
$paths : string|array<string|int, mixed>
$options : array<string|int, mixed>|null = null
Tags
throws
RuntimeException

__toString()

Returns path.

public __toString() : string
Return values
string

avif()

Converts an image asset to AVIF format.

public avif([int|null $quality = null ]) : self
Parameters
$quality : int|null = null
Tags
throws
RuntimeException
Return values
self

buildPathFromUrl()

Builds a relative path from a URL.

public static buildPathFromUrl(string $url) : string

Used for remote files.

Parameters
$url : string
Return values
string

convert()

Converts an image asset to $format format.

public convert(string $format[, int|null $quality = null ]) : self
Parameters
$format : string
$quality : int|null = null
Tags
throws
RuntimeException
Return values
self

dataurl()

Returns the Data URL (encoded in Base64).

public dataurl() : string
Tags
throws
RuntimeException
Return values
string

fingerprint()

Add hash to the file name + cache.

public fingerprint() : self
Return values
self

getAudio()

Returns audio file infos: - duration (in seconds.microseconds) - bitrate (in bps) - channel ('stereo', 'dual_mono', 'joint_stereo' or 'mono')

public getAudio() : array<string|int, mixed>
Tags
see
https://github.com/wapmorgan/Mp3Info
Return values
array<string|int, mixed>

getHeight()

Returns the height of an image/SVG or a video.

public getHeight() : int|null
Tags
throws
RuntimeException
Return values
int|null

getVideo()

Returns video file infos: - duration (in seconds) - width (in pixels) - height (in pixels)

public getVideo() : array<string|int, mixed>
Tags
see
https://github.com/JamesHeinrich/getID3
Return values
array<string|int, mixed>

getWidth()

Returns the width of an image/SVG or a video.

public getWidth() : int|null
Tags
throws
RuntimeException
Return values
int|null

isImageInCdn()

Is the asset an image and is it in CDN?

public isImageInCdn() : bool
Return values
bool

isMissing()

Checks if the asset is missing.

public isMissing() : bool
Return values
bool

maskable()

Creates a maskable image (with a padding = 20%).

public maskable([int|null $padding = null ]) : self
Parameters
$padding : int|null = null
Tags
throws
RuntimeException
Return values
self

minify()

Minifying a CSS or a JS.

public minify() : self
Return values
self

offsetExists()

Implements \ArrayAccess.

public offsetExists(mixed $offset) : bool
Parameters
$offset : mixed
Attributes
#[ReturnTypeWillChange]
Return values
bool

offsetGet()

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()

Resizes an image to the given width or/and height.

public resize([int|null $width = null ][, int|null $height = null ][, bool $rmAnimation = false ]) : self
  • If only the width is specified, the height is calculated to preserve the aspect ratio
  • If only the height is specified, the width is calculated to preserve the aspect ratio
  • If both width and height are specified, the image is resized to fit within the given dimensions, image is cropped and centered if necessary
  • If rmAnimation is true, any animation in the image (e.g., GIF) will be removed.
Parameters
$width : int|null = null
$height : int|null = null
$rmAnimation : bool = false
Tags
throws
RuntimeException
Return values
self

sanitize()

Replaces some characters by '_'.

public static sanitize(string $string) : string
Parameters
$string : string
Return values
string

save()

Saves the asset by adding its path to the build assets list.

public save() : void

Skips assets marked as missing and validates that the asset file exists in cache before adding it.

Tags
throws
RuntimeException

webp()

Converts an image asset to WebP format.

public webp([int|null $quality = null ]) : self
Parameters
$quality : int|null = null
Tags
throws
RuntimeException
Return values
self

doFingerprint()

Add hash to the file name.

protected doFingerprint() : self
Return values
self

doMinify()

Minifying a CSS or a JS + cache.

protected doMinify() : self
Tags
throws
RuntimeException
Return values
self

buildImageCdnUrl()

Builds CDN image URL.

private buildImageCdnUrl() : string
Return values
string

checkImage()

Checks if the asset is not missing and is typed as an image.

private checkImage() : void
Tags
throws
RuntimeException

deduplicateThumbPath()

Remove redondant '/thumbnails/<width(xheight)>/' in the path.

private deduplicateThumbPath(string $path) : string
Parameters
$path : string
Return values
string

getRemoteFileContent()

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
throws
RuntimeException
Return values
string

locateFile()

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)

  1. assets
  2. themes//assets
  3. static
  4. themes//static
Parameters
$path : string
$fallback : string|null = null
$userAgent : string|null = null
Tags
throws
RuntimeException
Return values
array<string|int, mixed>

optimizeImage()

Optimizing $filepath image.

private optimizeImage(string $filepath, string $path, int $quality) : int

Returns the new file size.

Parameters
$filepath : string
$path : string
$quality : int
Return values
int
 
On this page

Search results