<p>I don't think that Craft can do this natively, and would guess that it needs an AssetModel in order to determine where to create the image transforms, etc. Your best bet would be to add the images to Craft.</p>

<p>Assuming the images are stored locally, this is pretty easy:</p>

<ul>
<li>create an asset source that points to the folder where your images are stored</li>
<li>go to settings and update asset indexes for the new asset source</li>
</ul>

<p>If your filenames are all unique then you can reference the image by filename in your template:</p>

<pre><code>{% set asset = craft.assets.filename('filename.jpg').first() %}
{% if asset is defined %}
    &lt;img src="{{ asset.getUrl('medium') }}"&gt;
{% endif %}
</code></pre>

<p>Otherwise, you will need to add <a href="https://craftcms.com/docs/templating/craft.assets#folderId" rel="nofollow"><code>folderId</code></a> to the criteria, or add the asset to the current entry via an assetField and reference through the assetField.</p>

<p>(Another option, although not recommended, would be to add the images to an asset source as above; update indexes; then create a template that just renders all images in the asset source (using <a href="https://craftcms.com/docs/templating/craft.assets#source" rel="nofollow"><code>source('assetSource')</code></a> criteria), rendered with the given transform. You can then just hard code the path to the image using the image transform folder ('myimages/_medium' folder). But you could just as easily use photoshop for this.)</p>

CC BY-SA 3.0
2016-03-23T04:26:40.550Z
It's good to have you back around, Douglas. :)
Brad Bell
CC BY-SA 3.0
2016-03-23T04:34:43.400Z
This solution works wonderfully! I forgot to mention that the image was already in the Craft assets index - so adding the `craft.assets.filename()` variable did the trick perfectly. Thanks a ton!
simonswiss
CC BY-SA 3.0
2016-03-23T04:39:21.603Z
Great! Glad it worked. Just watch out for unique filenames or you may inadvertently render the wrong image :)
Douglas McDonald
CC BY-SA 3.0
2016-09-20T09:52:17.477Z
Nice answer, it is exactly what I was searching for. Thanks!
Masiorama
CC BY-SA 3.0
2016-03-23T04:15:33.717Z
2016-03-23T04:26:06.457Z
2016-03-23T04:26:06.457Z
Douglas McDonald
<p>I am migrating a static HTML site to Craft, and trying to apply image transforms to specific images without running them through a Craft field.</p>

<p>In other words, I have the image path hardcoded in the template, and would like to apply an existing transform to it.</p>

<p>I have tried to set my image path as a Twig variable and then call <code>getUrl('medium')</code> on that - but i get the error because I am passing a <code>String</code> value.</p>

<p>I also tried to store the image path in an array variable, but it's not working either.</p>

<p>Is there anyway to achieve this and pass a static path to <code>getUrl()</code>?</p>

<image-transforms>
Pass hardcoded path to getUrl()
# Pass hardcoded path to getUrl()

(Asked by: username_0 on 2016-03-22)

I am migrating a static HTML site to Craft, and trying to apply image transforms to specific images without running them through a Craft field.

In other words, I have the image path hardcoded in the template, and would like to apply an existing transform to it.

I have tried to set my image path as a Twig variable and then call `getUrl('medium')` on that - but i get the error because I am passing a `String` value.

I also tried to store the image path in an array variable, but it's not working either.

Is there anyway to achieve this and pass a static path to `getUrl()`?

**Tags:** image-transforms


---

## 1 answers

### Answer by username_1 on 2016-03-23. Score: 5 | Accepted answer

I don't think that Craft can do this natively, and would guess that it needs an AssetModel in order to determine where to create the image transforms, etc. Your best bet would be to add the images to Craft.
Assuming the images are stored locally, this is pretty easy:

create an asset source that points to the folder where your images are stored
go to settings and update asset indexes for the new asset source

If your filenames are all unique then you can reference the image by filename in your template:
{% set asset = craft.assets.filename('filename.jpg').first() %}
{% if asset is defined %}
    
{% endif %}

Otherwise, you will need to add folderId to the criteria, or add the asset to the current entry via an assetField and reference through the assetField.
(Another option, although not recommended, would be to add the images to an asset source as above; update indexes; then create a template that just renders all images in the asset source (using source('assetSource') criteria), rendered with the given transform. You can then just hard code the path to the image using the image transform folder ('myimages/_medium' folder). But you could just as easily use photoshop for this.)

> **Comments:**
> **username_2**: It's good to have you back around, Douglas. :)  
> **username_0**: This solution works wonderfully! I forgot to mention that the image was already in the Craft assets index - so adding the `craft.assets.filename()` variable did the trick perfectly. Thanks a ton!  
> **username_1**: Great! Glad it worked. Just watch out for unique filenames or you may inadvertently render the wrong image :)  
> **username_3**: Nice answer, it is exactly what I was searching for. Thanks!  

---

