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 | Type | Value | Description |
---|---|---|---|
CACHE_PREFIX
|
string |
"system.resizer."
|
string
"system.resizer."
The cache key prefix for resizer configs |
Properties
protected
static
$availableSources
:
array
= []
Available sources to get images from
protected
$fileModel
:
FileModel
= null
The instance of the FileModel for the source image
protected $height : int
Desired height
protected
$identifier
:
string
= null
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
Property | Type | Description |
---|---|---|
$image | mixed |
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 |
integer
Desired width of the resized image |
$height | integer |
integer
Desired height of the resized image |
$options | array |
array
Array of options to pass to the resizer |
public crop () : void
Process the crop request
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
Property | Type | Description |
---|---|---|
$image | mixed |
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 |
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
Property | Type | Description |
---|---|---|
$image | mixed |
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 |
int | string | bool | null
Desired width of the resized image |
$height | int | string | bool | null |
int | string | bool | null
Desired height of the resized image |
$options | array |
array
Array of options to pass to the resizer |
public static flushAvailableSources () : void
Flushes the local sources cache.
public static fromIdentifier (string $identifier) : self
Instantiate a resizer instance from the provided identifier
Property | Type | Description |
---|---|---|
$identifier | string |
string
The 40 character cache identifier for the desired resizer configuration |
public static getAvailableSources () : array
Get the available sources for processing image resize requests from
public getConfig () : array
Get the current config
public static getDefaultDisk () : Illuminate\Contracts\Filesystem\Filesystem
Get the default disk used to store processed images
public getDefaultOptions () : array
Get the default options for the resizer
public getDisk () : Illuminate\Contracts\Filesystem\Filesystem
Get the disk instance for image that is currently being processed
public getExtension () : string
Returns the file extension.
public getFileModel () : Winter\Storm\Database\Attach\File | null
Gets the current fileModel associated with the source image if one exists
public getIdentifier () : string
Gets the identifier for provided resizing configuration
40 character string used as a unique reference to the provided configuration
public getPathToResizedImage () : string
Get the path of the resized image
public getResizedUrl () : string
Get the URL to the resized image
public getResizerUrl () : string
Get the URL to the system resizer route for this instance's configuration
public getSourceFileContents ()
Get the contents of the image file to be resized
public getTempPath () : string
Get the internal temporary drirectory and ensure it exists
public getUrl () : string
Gets the current useful URL to the resized image (resizer if not resized, resized image directly if resized)
public static getValidResizedUrl (string $identifier, string $encodedUrl) : string | null
Check the provided encoded URL to verify its signature and return the decoded URL
Property | Type | Description |
---|---|---|
$identifier | string |
string
|
$encodedUrl | string |
string
|
Returns null if the provided value was invalid
public isResized () : bool
Get the reference to the resized image if the requested resize exists
public static isValidIdentifier (string $id) : bool
Check if the provided identifier looks like a valid identifier
Property | Type | Description |
---|---|---|
$id | string |
string
|
public static normalizeImage (mixed $image) : array
Normalize the provided input into information that the resizer can work with
Property | Type | Description |
---|---|---|
$image | mixed |
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 |
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
public storeConfig () : void
Stores the resizer configuration if the resizing hasn't been completed yet
protected
getLocalTempPath (string $path = null
)
: string
Stores the current source image in the temp directory and returns the path to it
Property | Type | Description |
---|---|---|
$path | string |
string
The path to suffix the temp directory path with, defaults to $identifier.$ext |
protected getTargetDetails () : array
Get the details for the target image
[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
Property | Type | Description |
---|---|---|
$path | string |
string
|