Skip to content

AssetTransforms ​

Type
Class
Namespace
craft\services
Inherits
craft\services\AssetTransforms » yii\base\Component » yii\base\BaseObject
Implements
yii\base\Configurable
Since
3.0.0

Asset Transforms service.

An instance of the service is available via Craft::$app->assetTransforms.

View source

Public Properties ​

PropertyDescription
activeTransformIndexcraft\models\AssetTransformIndex, null
allTransformscraft\models\AssetTransform[]
behaviorsyii\base\Behavior – List of behaviors attached to this component.
cachedCloudImageSizeinteger
dbcraft\db\Connection, array, string – The database connection to use
pendingTransformIndexIdsarray

activeTransformIndex ​

Type
craft\models\AssetTransformIndex, null
Default value
null

View source

allTransforms ​

Type
craft\models\AssetTransform[]
Default value
null
Access
Read-only

View source

cachedCloudImageSize ​

Type
integer
Default value
null
Access
Read-only

View source

db ​

Type
craft\db\Connection, array, string
Default value
'db'
Since
3.5.4

The database connection to use

View source

pendingTransformIndexIds ​

Type
array
Default value
null
Access
Read-only

View source

Public Methods ​

MethodDescription
__call()Calls the named method which is not a class method.
__clone()This method is called after the object is created by cloning an existing one.
__construct()Constructor.
__get()Returns the value of a component property.
__isset()Checks if a property is set, i.e. defined and not null.
__serialize()Serializer
__set()Sets the value of a component property.
__unset()Sets a component property to be null.
attachBehavior()Attaches a behavior to this component.
attachBehaviors()Attaches a list of behaviors to the component.
behaviors()Returns a list of behaviors that this component should behave as.
canGetProperty()Returns a value indicating whether a property can be read.
canSetProperty()Returns a value indicating whether a property can be set.
className()Returns the fully qualified name of this class.
deleteAllTransformData()Deletes all transform data (including thumbs and sources) associated with an asset.
deleteCreatedTransformsForAsset()Deletes transforms for an asset.
deleteQueuedSourceFiles()Delete all image sources queued up for deletion.
deleteResizedAssetVersion()Deletes all the generated thumbnails for an asset.
deleteTransform()Deletes an asset transform.
deleteTransformById()Deletes an asset transform by its ID.
deleteTransformIndex()Delete a transform index by.
deleteTransformIndexDataByAssetId()Deletes transform records by an asset ID.
detachBehavior()Detaches a behavior from the component.
detachBehaviors()Detaches all behaviors from the component.
detectAutoTransformFormat()Detect the auto web-safe format for as asset. Returns null if the asset is not an image.
eagerLoadTransforms()Eager-loads transform indexes the given list of assets.
ensureBehaviors()Makes sure that the behaviors declared in behaviors() are attached to this component.
ensureTransformUrlByIndexModel()Get a transform URL by the transform index model.
extendTransform()Extend a transform by taking an existing transform and overriding its parameters.
getActiveTransformIndex()
getAllCreatedTransformsForAsset()Returns an array of craft\models\AssetTransformIndex objects for all created transforms for an asset.
getAllTransforms()Returns all named asset transforms.
getBehavior()Returns the named behavior object.
getBehaviors()Returns all behaviors attached to this component.
getCachedCloudImageSize()Get the size of max cached cloud images dimension.
getLocalImageSource()Get a local image source to use for transforms.
getPendingTransformIndexIds()Returns a list of pending transform index IDs.
getTransformByHandle()Returns an asset transform by its handle.
getTransformById()Returns an asset transform by its ID.
getTransformByUid()Returns an asset transform by its UID.
getTransformFilename()Returns the filename used by the transform index for an asset.
getTransformIndex()Get a transform index row. If it doesn't exist - create one.
getTransformIndexModelByAssetIdAndHandle()Returns a transform index model by an asset ID and transform handle.
getTransformIndexModelById()Returns a transform index model by its ID.
getTransformSubfolder()Returns a subfolder used by the transform index for an asset.
getTransformSubpath()Returns the path to a transform, relative to the asset's folder.
getTransformUri()Returns the URI for a transform, relative to the asset's folder.
getUrlForTransformByAssetAndTransformIndex()Get URL for Transform by the transform index model.
getUrlForTransformByIndexId()Get URL for Transform by TransformIndexId.
handleChangedTransform()Handle transform change.
handleDeletedTransform()Handle transform being deleted
hasEventHandlers()Returns a value indicating whether there is any handler attached to the named event.
hasMethod()Returns a value indicating whether a method is defined.
hasProperty()Returns a value indicating whether a property is defined for this component.
init()Initializes the object.
normalizeTransform()Normalize a transform from handle or a set of properties to an AssetTransform.
off()Detaches an existing event handler from this component.
on()Attaches an event handler to an event.
queueSourceForDeletingIfNecessary()Deletes an image local source if required by config.
saveTransform()Saves an asset transform.
setActiveTransformIndex()
storeLocalSource()Store a local image copy to a destination path.
storeTransformIndexData()Store a transform index data by it's model.
trigger()Triggers an event.
validateTransformIndexResult()Validates a transform index result to see if the index is still valid for a given asset.

__serialize() ​

Since
3.5.14

Serializer

View source

deleteAllTransformData() ​

Deletes all transform data (including thumbs and sources) associated with an asset.

View source

Arguments ​

deleteCreatedTransformsForAsset() ​

Deletes transforms for an asset.

View source

Arguments ​

Throws ​

deleteQueuedSourceFiles() ​

Delete all image sources queued up for deletion.

View source

deleteResizedAssetVersion() ​

Deletes all the generated thumbnails for an asset.

View source

Arguments ​

deleteTransform() ​

Deletes an asset transform.

Note that passing an ID to this function is now deprecated. Use deleteTransformById() instead.

View source

Arguments ​

Returns ​

boolean – Whether the transform was deleted

Throws ​

deleteTransformById() ​

Deletes an asset transform by its ID.

View source

Arguments ​

  • $transformId (integer) – The transform's ID

Returns ​

boolean – Whether the transform was deleted.

Throws ​

deleteTransformIndex() ​

Delete a transform index by.

View source

Arguments ​

deleteTransformIndexDataByAssetId() ​

Deletes transform records by an asset ID.

View source

Arguments ​

detectAutoTransformFormat() ​

Detect the auto web-safe format for as asset. Returns null if the asset is not an image.

View source

Arguments ​

Returns ​

mixed, string

Throws ​

eagerLoadTransforms() ​

Eager-loads transform indexes the given list of assets.

You can include srcset-style sizes (e.g. 100w or 2x) following a normal transform definition, for example:

::: code

twig
[{width: 1000, height: 600}, '1.5x', '2x', '3x']
php
[['width' => 1000, 'height' => 600], '1.5x', '2x', '3x']

:::

When a srcset-style size is encountered, the preceding normal transform definition will be used as a reference when determining the resulting transform dimensions.

View source

Arguments ​

  • $assets (craft\elements\Asset[], array) – The assets or asset data to eager-load transforms for
  • $transforms (array) – The transform definitions to eager-load

ensureTransformUrlByIndexModel() ​

Get a transform URL by the transform index model.

View source

Arguments ​

Returns ​

string

Throws ​

extendTransform() ​

Extend a transform by taking an existing transform and overriding its parameters.

View source

Arguments ​

Returns ​

craft\models\AssetTransform

getActiveTransformIndex() ​

View source

Returns ​

craft\models\AssetTransformIndex, null

getAllCreatedTransformsForAsset() ​

Returns an array of craft\models\AssetTransformIndex objects for all created transforms for an asset.

View source

Arguments ​

Returns ​

array

getAllTransforms() ​

Returns all named asset transforms.

View source

Returns ​

craft\models\AssetTransform[]

getCachedCloudImageSize() ​

Get the size of max cached cloud images dimension.

View source

Returns ​

integer

getLocalImageSource() ​

Get a local image source to use for transforms.

View source

Arguments ​

Returns ​

string

Throws ​

getPendingTransformIndexIds() ​

Returns a list of pending transform index IDs.

View source

Returns ​

array

getTransformByHandle() ​

Returns an asset transform by its handle.

View source

Arguments ​

Returns ​

craft\models\AssetTransform, null

getTransformById() ​

Returns an asset transform by its ID.

View source

Arguments ​

Returns ​

craft\models\AssetTransform, null

getTransformByUid() ​

Since
3.1.0

Returns an asset transform by its UID.

View source

Arguments ​

Returns ​

craft\models\AssetTransform, null

getTransformFilename() ​

Returns the filename used by the transform index for an asset.

View source

Arguments ​

Returns ​

string

getTransformIndex() ​

Get a transform index row. If it doesn't exist - create one.

View source

Arguments ​

Returns ​

craft\models\AssetTransformIndex

Throws ​

getTransformIndexModelByAssetIdAndHandle() ​

Returns a transform index model by an asset ID and transform handle.

View source

Arguments ​

Returns ​

craft\models\AssetTransformIndex, null

getTransformIndexModelById() ​

Returns a transform index model by its ID.

View source

Arguments ​

Returns ​

craft\models\AssetTransformIndex, null

getTransformSubfolder() ​

Returns a subfolder used by the transform index for an asset.

View source

Arguments ​

Returns ​

string

getTransformSubpath() ​

Returns the path to a transform, relative to the asset's folder.

View source

Arguments ​

Returns ​

string

getTransformUri() ​

Returns the URI for a transform, relative to the asset's folder.

View source

Arguments ​

Returns ​

string

getUrlForTransformByAssetAndTransformIndex() ​

Get URL for Transform by the transform index model.

View source

Arguments ​

Returns ​

string

getUrlForTransformByIndexId() ​

Get URL for Transform by TransformIndexId.

View source

Arguments ​

Returns ​

string

handleChangedTransform() ​

Handle transform change.

View source

Arguments ​

handleDeletedTransform() ​

Handle transform being deleted

View source

Arguments ​

init() ​

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the given configuration.

View source

normalizeTransform() ​

Normalize a transform from handle or a set of properties to an AssetTransform.

View source

Arguments ​

Returns ​

craft\models\AssetTransform, null

Throws ​

queueSourceForDeletingIfNecessary() ​

Deletes an image local source if required by config.

View source

Arguments ​

saveTransform() ​

Saves an asset transform.

View source

Arguments ​

Returns ​

boolean

Throws ​

setActiveTransformIndex() ​

View source

Arguments ​

storeLocalSource() ​

Store a local image copy to a destination path.

View source

Arguments ​

storeTransformIndexData() ​

Store a transform index data by it's model.

View source

Arguments ​

Returns ​

craft\models\AssetTransformIndex

validateTransformIndexResult() ​

Validates a transform index result to see if the index is still valid for a given asset.

View source

Arguments ​

Returns ​

boolean – Whether the index result is still valid

Constants ​

ConstantDescription
CONFIG_TRANSFORM_KEY

Events ​

EVENT_AFTER_DELETE_ASSET_TRANSFORM ​

Type
craft\events\AssetTransformEvent

The event that is triggered after an asset transform is deleted


EVENT_AFTER_DELETE_TRANSFORMS ​

Type
craft\events\AssetTransformImageEvent

The event that is triggered after deleting generated transforms.


EVENT_AFTER_SAVE_ASSET_TRANSFORM ​

Type
craft\events\AssetTransformEvent

The event that is triggered after an asset transform is saved


EVENT_BEFORE_APPLY_TRANSFORM_DELETE ​

Type
craft\events\AssetTransformEvent
Since
3.1.0

The event that is triggered before a transform delete is applied to the database.


EVENT_BEFORE_DELETE_ASSET_TRANSFORM ​

Type
craft\events\AssetTransformEvent

The event that is triggered before an asset transform is deleted


EVENT_BEFORE_DELETE_TRANSFORMS ​

Type
craft\events\AssetTransformImageEvent

The event that is triggered before deleting generated transforms.


EVENT_BEFORE_SAVE_ASSET_TRANSFORM ​

Type
craft\events\AssetTransformEvent

The event that is triggered before an asset transform is saved


EVENT_GENERATE_TRANSFORM ​

Type
craft\events\GenerateTransformEvent

The event that is triggered when a transform is being generated for an asset.