ImageResizer

Image Resizing class used for resizing any image resources accessible to the application.

 class System\Classes\ImageResizer

This works by accepting a variety of image sources and normalizing the pipeline for storing the desired resizing configuration and then deferring the actual resizing of the images until requested by the browser.

When the resizer route is hit, the configuration is retrieved from the cache and used to generate the desired image and then redirect to the generated images static path to minimize the load on the server. Future loads of the image are automatically pointed to the static URL of the resized image without even hitting the resizer route.

The functionality of this class is controlled by these config items:

  • cms.storage.resized.disk - The disk to store resized images on
  • cms.storage.resized.folder - The folder on the disk to store resized images in
  • cms.storage.resized.path - The public path to the resized images as returned by the storage disk's URL method, used to identify already resized images

Constants

Constant Description
CACHE_PREFIX
string "system.resizer."

The cache key prefix for resizer configs

Properties

protected static $availableSources : array

Available sources to get images from

protected $fileModel : FileModel

The instance of the FileModel for the source image

protected $height : int

Desired height

protected $identifier : string

Unique identifier for the current configuration

protected $image : array

Image source data ['disk' => string, 'path' => string, 'source' => string]

protected $options : array

Image resizing configuration data

protected $width : int

Desired width

Methods

public __construct (mixed $image, integer $width, integer $height, array $options = [])

Prepare the resizer instance

Parameters
Property Description
$image
mixed

Supported values below: ['disk' => FilesystemAdapter, 'path' => string, 'source' => string, 'fileModel' => FileModel|void], instance of Winter\Storm\Database\Attach\File, string containing URL or path accessible to the application's filesystem manager

$width
integer

Desired width of the resized image

$height
integer

Desired height of the resized image

$options
array

Array of options to pass to the resizer

Returns
mixed

public crop () : void

Process the crop request

Returns
void

public static filterGetDimensions (mixed $image) : array

Gets the dimensions of the provided image file NOTE: Doesn't currently support being passed a FileModel image that has already been resized

Parameters
Property Description
$image
mixed

Supported values below: ['disk' => FilesystemAdapter, 'path' => string, 'source' => string, 'fileModel' => FileModel|void], instance of Winter\Storm\Database\Attach\File, string containing URL or path accessible to the application's filesystem manager

Returns
array

public static filterGetUrl (mixed $image, int | string | bool | null $width = null, int | string | bool | null $height = null, array $options = []) : string

Converts supplied input into a URL that will return the desired resized image

Parameters
Property Description
$image
mixed

Supported values below: ['disk' => FilesystemAdapter, 'path' => string, 'source' => string, 'fileModel' => FileModel|void], instance of Winter\Storm\Database\Attach\File, string containing URL or path accessible to the application's filesystem manager

$width
int | string | bool | null

Desired width of the resized image

$height
int | string | bool | null

Desired height of the resized image

$options
array

Array of options to pass to the resizer

Returns
string

public static flushAvailableSources () : void

Flushes the local sources cache.

Returns
void

public static fromIdentifier (string $identifier) : self

Instantiate a resizer instance from the provided identifier

Parameters
Property Description
$identifier
string

The 40 character cache identifier for the desired resizer configuration

Returns
self

public static getAvailableSources () : array

Get the available sources for processing image resize requests from

Returns
array

public getConfig () : array

Get the current config

Returns
array

public static getDefaultDisk () : Illuminate\Contracts\Filesystem\Filesystem

Get the default disk used to store processed images

Returns
Illuminate\Contracts\Filesystem\Filesystem

public getDefaultOptions () : array

Get the default options for the resizer

Returns
array

public getDisk () : Illuminate\Contracts\Filesystem\Filesystem

Get the disk instance for image that is currently being processed

Returns
Illuminate\Contracts\Filesystem\Filesystem

public getExtension () : string

Returns the file extension.

Returns
string

public getFileModel () : Winter\Storm\Database\Attach\File | null

Gets the current fileModel associated with the source image if one exists

Returns
Winter\Storm\Database\Attach\File | null

public getIdentifier () : string

Gets the identifier for provided resizing configuration

Returns
string

40 character string used as a unique reference to the provided configuration

public getPathToResizedImage () : string

Get the path of the resized image

Returns
string

public getResizedUrl () : string

Get the URL to the resized image

Returns
string

public getResizerUrl () : string

Get the URL to the system resizer route for this instance's configuration

Returns
string

public getSourceFileContents ()

Get the contents of the image file to be resized

Returns
mixed

public getTempPath () : string

Get the internal temporary drirectory and ensure it exists

Returns
string

public getUrl () : string

Gets the current useful URL to the resized image (resizer if not resized, resized image directly if resized)

Returns
string

public static getValidResizedUrl (string $identifier, string $encodedUrl) : string | null

Check the provided encoded URL to verify its signature and return the decoded URL

Parameters
Property Description
$identifier
string
$encodedUrl
string
Returns
string | null

Returns null if the provided value was invalid

public isResized () : bool

Get the reference to the resized image if the requested resize exists

Returns
bool

public static isValidIdentifier (string $id) : bool

Check if the provided identifier looks like a valid identifier

Parameters
Property Description
$id
string
Returns
bool

public static normalizeImage (mixed $image) : array

Normalize the provided input into information that the resizer can work with

Parameters
Property Description
$image
mixed

Supported values below: ['disk' => FilesystemAdapter, 'path' => string, 'source' => string, 'fileModel' => FileModel|void], instance of Winter\Storm\Database\Attach\File, string containing URL or path accessible to the application's filesystem manager

Returns
array

Array containing the disk, path, source, and fileModel if applicable ['disk' => FilesystemAdapter, 'path' => string, 'source' => string, 'fileModel' => FileModel|void]

public resize () : void

Process the resize request

Returns
void

public storeConfig () : void

Stores the resizer configuration if the resizing hasn't been completed yet

Returns
void

protected getLocalTempPath (string $path = null) : string

Stores the current source image in the temp directory and returns the path to it

Parameters
Property Description
$path
string

The path to suffix the temp directory path with, defaults to $identifier.$ext

Returns
string

protected getTargetDetails () : array

Get the details for the target image

Returns
array

[FilesystemAdapter $disk, (string) $path]

protected static normalizePath (string $path) : string

Normalize the provided path to Unix style directory seperators to ensure that path manipulation operations succeed regardless of environment

NOTE: Can't use Winter\Storm\FileSystem\PathResolver because it prepends the current working directory to relative paths

Parameters
Property Description
$path
string
Returns
string
Copyright © 2024 Winter CMS