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
.
Public Properties ​
Property | Description |
---|---|
activeTransformIndex | craft\models\AssetTransformIndex, null |
allTransforms | craft\models\AssetTransform[] |
behaviors | yii\base\Behavior – List of behaviors attached to this component. |
cachedCloudImageSize | integer |
db | craft\db\Connection, array, string – The database connection to use |
pendingTransformIndexIds | array |
activeTransformIndex
​
- Type
- craft\models\AssetTransformIndex, null
- Default value
null
allTransforms
​
- Type
- craft\models\AssetTransform[]
- Default value
null
- Access
- Read-only
cachedCloudImageSize
​
- Type
- integer
- Default value
null
- Access
- Read-only
db
​
- Type
- craft\db\Connection, array, string
- Default value
'db'
- Since
- 3.5.4
The database connection to use
pendingTransformIndexIds
​
- Type
- array
- Default value
null
- Access
- Read-only
Public Methods ​
Method | Description |
---|---|
__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
deleteAllTransformData()
​
Deletes all transform data (including thumbs and sources) associated with an asset.
Arguments ​
$asset
(craft\elements\Asset)
deleteCreatedTransformsForAsset()
​
Deletes transforms for an asset.
Arguments ​
$asset
(craft\elements\Asset)
Throws ​
- craft\errors\VolumeException
if something went very wrong when deleting a transform
deleteQueuedSourceFiles()
​
Delete all image sources queued up for deletion.
deleteResizedAssetVersion()
​
Deletes all the generated thumbnails for an asset.
Arguments ​
$asset
(craft\elements\Asset)
deleteTransform()
​
Deletes an asset transform.
Note that passing an ID to this function is now deprecated. Use deleteTransformById() instead.
Arguments ​
$transform
(integer, craft\models\AssetTransform) – The transform
Returns ​
boolean – Whether the transform was deleted
Throws ​
- yii\db\Exception
on DB error
deleteTransformById()
​
Deletes an asset transform by its ID.
Arguments ​
$transformId
(integer) – The transform's ID
Returns ​
boolean – Whether the transform was deleted.
Throws ​
- yii\db\Exception
on DB error
deleteTransformIndex()
​
Delete a transform index by.
Arguments ​
$indexId
(integer)
deleteTransformIndexDataByAssetId()
​
Deletes transform records by an asset ID.
Arguments ​
$assetId
(integer)
detectAutoTransformFormat()
​
Detect the auto web-safe format for as asset. Returns null
if the asset is not an image.
Arguments ​
$asset
(craft\elements\Asset)
Returns ​
mixed
, string
Throws ​
- craft\errors\AssetLogicException
If attempting to detect an image format for a non-image.
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
[{width: 1000, height: 600}, '1.5x', '2x', '3x']
[['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.
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.
Arguments ​
$index
(craft\models\AssetTransformIndex)
Returns ​
Throws ​
- craft\errors\AssetTransformException
If there was an error generating the transform.
extendTransform()
​
Extend a transform by taking an existing transform and overriding its parameters.
Arguments ​
$transform
(craft\models\AssetTransform)$parameters
(array)
Returns ​
getActiveTransformIndex()
​
Returns ​
craft\models\AssetTransformIndex, null
getAllCreatedTransformsForAsset()
​
Returns an array of craft\models\AssetTransformIndex objects for all created transforms for an asset.
Arguments ​
$asset
(craft\elements\Asset)
Returns ​
getAllTransforms()
​
Returns all named asset transforms.
Returns ​
getCachedCloudImageSize()
​
Get the size of max cached cloud images dimension.
Returns ​
getLocalImageSource()
​
Get a local image source to use for transforms.
Arguments ​
$asset
(craft\elements\Asset)
Returns ​
Throws ​
- craft\errors\VolumeException
If there was an error downloading the remote file. - craft\errors\VolumeObjectNotFoundException
If the file cannot be found.
getPendingTransformIndexIds()
​
Returns a list of pending transform index IDs.
Returns ​
getTransformByHandle()
​
Returns an asset transform by its handle.
Arguments ​
$handle
(string)
Returns ​
craft\models\AssetTransform, null
getTransformById()
​
Returns an asset transform by its ID.
Arguments ​
$id
(integer)
Returns ​
craft\models\AssetTransform, null
getTransformByUid()
​
- Since
- 3.1.0
Returns an asset transform by its UID.
Arguments ​
$uid
(string)
Returns ​
craft\models\AssetTransform, null
getTransformFilename()
​
Returns the filename used by the transform index for an asset.
Arguments ​
$asset
(craft\elements\Asset)$index
(craft\models\AssetTransformIndex)
Returns ​
getTransformIndex()
​
Get a transform index row. If it doesn't exist - create one.
Arguments ​
$asset
(craft\elements\Asset)$transform
(craft\models\AssetTransform, string, array, null)
Returns ​
craft\models\AssetTransformIndex
Throws ​
- craft\errors\AssetTransformException
if the transform cannot be found by the handle
getTransformIndexModelByAssetIdAndHandle()
​
Returns a transform index model by an asset ID and transform handle.
Arguments ​
Returns ​
craft\models\AssetTransformIndex, null
getTransformIndexModelById()
​
Returns a transform index model by its ID.
Arguments ​
$transformId
(integer)
Returns ​
craft\models\AssetTransformIndex, null
getTransformSubfolder()
​
Returns a subfolder used by the transform index for an asset.
Arguments ​
$asset
(craft\elements\Asset)$index
(craft\models\AssetTransformIndex)
Returns ​
getTransformSubpath()
​
Returns the path to a transform, relative to the asset's folder.
Arguments ​
$asset
(craft\elements\Asset)$index
(craft\models\AssetTransformIndex)
Returns ​
getTransformUri()
​
Returns the URI for a transform, relative to the asset's folder.
Arguments ​
$asset
(craft\elements\Asset)$index
(craft\models\AssetTransformIndex)
Returns ​
getUrlForTransformByAssetAndTransformIndex()
​
Get URL for Transform by the transform index model.
Arguments ​
$asset
(craft\elements\Asset)$transformIndexModel
(craft\models\AssetTransformIndex)
Returns ​
getUrlForTransformByIndexId()
​
Get URL for Transform by TransformIndexId.
Arguments ​
$transformId
(integer)
Returns ​
handleChangedTransform()
​
Handle transform change.
Arguments ​
$event
(craft\events\ConfigEvent)
handleDeletedTransform()
​
Handle transform being deleted
Arguments ​
$event
(craft\events\ConfigEvent)
init()
​
Initializes the object.
This method is invoked at the end of the constructor after the object is initialized with the given configuration.
normalizeTransform()
​
Normalize a transform from handle or a set of properties to an AssetTransform.
Arguments ​
$transform
(craft\models\AssetTransform, string, array, null)
Returns ​
craft\models\AssetTransform, null
Throws ​
- craft\errors\AssetTransformException
if $transform is an invalid transform handle
queueSourceForDeletingIfNecessary()
​
Deletes an image local source if required by config.
Arguments ​
$imageSource
(string)
saveTransform()
​
Saves an asset transform.
Arguments ​
$transform
(craft\models\AssetTransform) – The transform to be saved$runValidation
(boolean) – Whether the transform should be validated
Returns ​
Throws ​
- craft\errors\AssetTransformException
If attempting to update a non-existing transform.
setActiveTransformIndex()
​
Arguments ​
$index
(craft\models\AssetTransformIndex)
storeLocalSource()
​
Store a local image copy to a destination path.
Arguments ​
storeTransformIndexData()
​
Store a transform index data by it's model.
Arguments ​
$index
(craft\models\AssetTransformIndex)
Returns ​
craft\models\AssetTransformIndex
validateTransformIndexResult()
​
Validates a transform index result to see if the index is still valid for a given asset.
Arguments ​
$result
(array)$transform
(craft\models\AssetTransform)$asset
(craft\elements\Asset, array) – The asset object or a raw database result
Returns ​
boolean – Whether the index result is still valid
Constants ​
Constant | Description |
---|---|
CONFIG_TRANSFORM_KEY |
Events ​
EVENT_AFTER_DELETE_ASSET_TRANSFORM ​
The event that is triggered after an asset transform is deleted
EVENT_AFTER_DELETE_TRANSFORMS ​
The event that is triggered after deleting generated transforms.
EVENT_AFTER_SAVE_ASSET_TRANSFORM ​
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 ​
The event that is triggered before an asset transform is deleted
EVENT_BEFORE_DELETE_TRANSFORMS ​
The event that is triggered before deleting generated transforms.
EVENT_BEFORE_SAVE_ASSET_TRANSFORM ​
The event that is triggered before an asset transform is saved
EVENT_GENERATE_TRANSFORM ​
The event that is triggered when a transform is being generated for an asset.