\Field\UtilityImageToolbox

ImageToolbox class for handling image related tasks.

Allows to define image preview modes, render a field instance or create image thumbnails among other things.

NOTE: Thumbnails files are always stored in .tmb directory.

Summary

Methods
Properties
Constants
formatter()
thumbnail()
previewsOptions()
getPreviews()
addPreview()
delete()
deleteThumbnails()
bytesToSize()
fileIcon()
fileIconMap()
ext()
removeExt()
No public properties found
No constants found
_initPreviews()
$_previews
N/A
No private methods found
No private properties found
N/A

Properties

$_previews

$_previews : array

List of preview modes. More modes can be defined using "addPreview()" method.

Type

array

Methods

formatter()

formatter(\Cake\View\View $view, \Field\Model\Entity\Field $field) : string

Renders the given custom field.

Parameters

\Cake\View\View $view

Instance of view class

\Field\Model\Entity\Field $field

The field to be rendered

Returns

string —

HTML code

thumbnail()

thumbnail(string $filePath, string $previewSize) : false|string

Creates a thumbnail for the given image.

Parameters

string $filePath

Full path to original image file

string $previewSize

A valid preview preset

Returns

false|string —

Full path to thumbnail file on success, false otherwise

previewsOptions()

previewsOptions() : array

Returns an array list of available previews modes suitable for select boxes.

Returns

array

getPreviews()

getPreviews(string $slug) : array

Gets all defined previews, or information for an specific preview.

Parameters

string $slug

Slug of the preview for which get its info, set to null will retrieve info for all registered previews

Returns

array

addPreview()

addPreview(string $slug, string $label, integer $width, integer $height) : void

Defines a new preview configuration or overwrite if exists.

Parameters

string $slug

Unique machine-name. e.g.: my-preview-mode

string $label

Human-readable name. e.g.: My preview mode

integer $width

Width for images that would use this preview mode

integer $height

Height for images that would use this preview mode

delete()

delete(string $imagePath) : void

Deletes the given image and all its thumbnails.

Parameters

string $imagePath

Full path to image file

deleteThumbnails()

deleteThumbnails(string $imagePath) : void

Delete image's thumbnails if exists.

Parameters

string $imagePath

Full path to original image file

bytesToSize()

bytesToSize(integer $bytes, integer $precision) : string

Gets a translated string representation of the size.

Parameters

integer $bytes

Size to convert given in bytes units

integer $precision

Decimal precision

Returns

string —

Human-readable size, e.g. 1 KB, 36.8 MB, etc

fileIcon()

fileIcon(string $mime, mixed $iconsDirectory) : mixed

Creates a path to the icon for a file mime.

Parameters

string $mime

File mime type

mixed $iconsDirectory

A path to a directory of icons to be used for files. Defaults to built-in icons directory (Field/webroot/img/file-icons/)

Returns

mixed —

A string for the icon file name, or false if an appropriate icon could not be found

fileIconMap()

fileIconMap(string $mime) : string

Determine the generic icon MIME package based on a file's MIME type.

Parameters

string $mime

File mime type

Returns

string —

The generic icon MIME package expected for this file

ext()

ext(string $fileName) : string

Get file extension.

Parameters

string $fileName

File name, including its extension. e.g.: my-file.docx

Returns

string —

File extension without the ending DOT and lowercased, e.g. pdf, jpg, png, etc. If no extension is found an empty string will be returned

removeExt()

removeExt(string $fileName) : string

Remove file extension.

Parameters

string $fileName

File name, including its extension. e.g. my-file.docx, myFile.DoCX, etc

Returns

string —

File name without extension, e.g. my-file, myFile, etc

_initPreviews()

_initPreviews() : void

Initializes defaults built-in preview modes.