<p>You can check for the existence of a transform within your controller (for example), as follows:</p>

<pre><code> if (craft()-&gt;assetTransforms-&gt;getTransformByHandle('jimbob')) {
      // Transform exists.
 } else {
      // Transform does not exist (null is returned).
 }
</code></pre>

<p>Unfortunately, there doesn't seem to be an equivalent service (such as <code>craft.assetTransforms</code>) for use within templates.</p>

<p>Stephen</p>

CC BY-SA 3.0
2014-08-11T18:37:04.117Z
2014-08-11T18:37:04.117Z
Stephen Lewis
<p>I'm using asset transforms in various places to serve appropriately sized images via a Twig macro that I feed an entry and the name of a transform into (the idea is to have the same bit of HTML, but with differently sized images depending on the context). The way I get the image URL is as follows:</p>

<p><code>entry.mainImage.first().getUrl(transform)</code></p>

<p>However, at the moment if the named transform is not found (ie. if the value of <code>transform</code> doesn't correspond to the name of an existing transform) I get a Craft error. For my purposes a better failure mode would be to just serve the untransformed, full-size image. I'm wondering if there's a way of checking whether a named transform exists? That way I could just set <code>transform</code> to <code>null</code> if it's not found and it should fail gracefully.</p>

CC BY-SA 3.0
2014-08-11T17:21:42.743Z
Are there other possibilities that would make your macro break besides "if the value of transform doesn't correspond to the name of an existing transform"?
carlcs
CC BY-SA 3.0
2014-08-11T17:29:36.320Z
No, not that I know of. The reason I'm asking is that the macro is being used across lots of templates and I've tweaked the functionality slightly, which means that in some places the macro receives an argument that doesn't correspond to an existing transform. This breaks all my templates, and it would be nice to catch that possibility in the macro so that if there's still a template or partial somewhere that I've missed that's calling the macro in the old way, I can be sure it won't break the page.
Nick F
CC BY-SA 3.0
2014-08-11T17:43:17.373Z
What about using cmd+shift+f in Sublime Text to search in all your template files for that old macro syntax? Work with regex too!
carlcs
CC BY-SA 3.0
2014-08-11T18:13:49.970Z
Ha, thanks. I actually already did that, as it happens. But if someone, say, deletes or renames an asset transform, the site will still break. I'd ideally like to be able to write my templates to protect against that in principle.
Nick F
<assets><image-transforms>
How to check whether a named Asset Transform exists?
# How to check whether a named Asset Transform exists?

(Asked by: username_0 on 2014-08-11)

I'm using asset transforms in various places to serve appropriately sized images via a Twig macro that I feed an entry and the name of a transform into (the idea is to have the same bit of HTML, but with differently sized images depending on the context). The way I get the image URL is as follows:

`entry.mainImage.first().getUrl(transform)`

However, at the moment if the named transform is not found (ie. if the value of `transform` doesn't correspond to the name of an existing transform) I get a Craft error. For my purposes a better failure mode would be to just serve the untransformed, full-size image. I'm wondering if there's a way of checking whether a named transform exists? That way I could just set `transform` to `null` if it's not found and it should fail gracefully.

**Tags:** assets, image-transforms
> **Comments:**
> **username_1**: Are there other possibilities that would make your macro break besides "if the value of transform doesn't correspond to the name of an existing transform"?  
> **username_0**: No, not that I know of. The reason I'm asking is that the macro is being used across lots of templates and I've tweaked the functionality slightly, which means that in some places the macro receives an argument that doesn't correspond to an existing transform. This breaks all my templates, and it would be nice to catch that possibility in the macro so that if there's still a template or partial somewhere that I've missed that's calling the macro in the old way, I can be sure it won't break the page.  
> **username_1**: What about using cmd+shift+f in Sublime Text to search in all your template files for that old macro syntax? Work with regex too!  
> **username_0**: Ha, thanks. I actually already did that, as it happens. But if someone, say, deletes or renames an asset transform, the site will still break. I'd ideally like to be able to write my templates to protect against that in principle.  

---

## 1 answers

### Answer by username_2 on 2014-08-11. Score: 4 | Accepted answer

You can check for the existence of a transform within your controller (for example), as follows:
 if (craft()->assetTransforms->getTransformByHandle('jimbob')) {
      // Transform exists.
 } else {
      // Transform does not exist (null is returned).
 }

Unfortunately, there doesn't seem to be an equivalent service (such as craft.assetTransforms) for use within templates.
Stephen


---

