<p>You'd do it from PHP nearly idential to how you'd do it from Twig... by passing in the Asset Transform's handle to <a href="https://craftcms.com/classreference/models/AssetFileModel" rel="nofollow">AssetFileModel's</a> <a href="https://craftcms.com/classreference/models/AssetFileModel#getUrl-detail" rel="nofollow"><code>getUrl()</code></a> method.</p>

<p>Twig:</p>

<pre><code>{% set asset = craft.assets.id(1).first() %}

{% if asset %}
    {{ asset.getUrl('myTransform') }}
{% endif %}
</code></pre>

<p>PHP:</p>

<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Asset);
$criteria-&gt;id = 1;
$asset = $criteria-&gt;first();

if ($asset)
{
    $url = $asset-&gt;getUrl('myTransform');
}
</code></pre>

CC BY-SA 3.0
2016-09-27T07:46:02.770Z
yep thats the way, thank you very much :)
nickGR
CC BY-SA 4.0
2019-04-25T13:59:03.837Z
How to do the same in Craftcms 3.
inrsaurabh
CC BY-SA 4.0
2019-04-29T08:13:00.370Z
https://craftcms.stackexchange.com/questions/30264/get-transformed-asset-url-in-controller-of-module-while-using-eager-loading
inrsaurabh
CC BY-SA 3.0
2016-09-26T19:04:21.557Z
2016-09-26T19:04:21.557Z
Brad Bell
<p>I want to return JSON with some asset URLs, but i want these URLs to be transformed asset URLs.</p>

<p>Is there some way for me to get the asset transformed URL in the controller? Will the assets be automatically transformed or i need to somehow do it manually ?</p>

<image-transforms>
Get transformed asset URL in controller
# Get transformed asset URL in controller

(Asked by: username_0 on 2016-09-26)

I want to return JSON with some asset URLs, but i want these URLs to be transformed asset URLs.

Is there some way for me to get the asset transformed URL in the controller? Will the assets be automatically transformed or i need to somehow do it manually ?

**Tags:** image-transforms

---

## 1 answers

### Answer by username_1 on 2016-09-26. Score: 2 | Accepted answer

You'd do it from PHP nearly idential to how you'd do it from Twig... by passing in the Asset Transform's handle to AssetFileModel's getUrl() method.
Twig:
{% set asset = craft.assets.id(1).first() %}

{% if asset %}
    {{ asset.getUrl('myTransform') }}
{% endif %}

PHP:
$criteria = craft()->elements->getCriteria(ElementType::Asset);
$criteria->id = 1;
$asset = $criteria->first();

if ($asset)
{
    $url = $asset->getUrl('myTransform');
}

> **Comments:**
> **username_0**: yep thats the way, thank you very much :)  
> **username_2**: How to do the same in Craftcms 3.  
> **username_2**: https://craftcms.stackexchange.com/questions/30264/get-transformed-asset-url-in-controller-of-module-while-using-eager-loading  

---

