1

Is there a way to get all available image transforms in a template? I would like to generate the srcset for responsive images programmatically.

I thought of creating a macro for that but haven't found any information about how to loop over all the registered image transforms.

  • You might want to get out the ImageOptimize plugin; it has an OptimizedImages field that will pre-generate the srcset for you and create all of the images on Asset save time, so the frontend performance is optimal. – andrew.welch Apr 21 '18 at 21:28
  • Another approach is is the Picture plugin, where you define the transforms for a picture style in a config file. – Marion Newlevant Apr 24 '18 at 10:42
2

Craft 3 PHP

$transforms = Craft::$app->getAssetTransforms()->getAllTransforms();
foreach($allTransforms as $transform){
    echo $asset->getUrl($transform->handle);
}

Craft 3 Twig

{% set allTransforms = craft.app.getAssetTransforms.getAllTransforms() %}
{% for transform in allTransforms %}            
    {{ asset.getUrl(transform.handle) }}
{% endfor %}

Craft 2 PHP

$allTansforms = craft()->assetTransforms->getAllTransforms();

Craft 2 Twig

You'll need a plugin for this because you don't have access to that component

{% set allTransforms = craft.pluginHandle.getAllAssetTransforms %}

And in your PluginVariable

public function getAllAssetTransforms(){
    return craft()->assetTransforms->getAllTransforms();
}
| improve this answer | |
  • I think for Craft 3 Twig, it should be {{ asset.getUrl(transform.handle) }}. I tried to edit your post, but I need to edit at least 6 characters ... – obs Apr 20 '18 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.