<p>Try this:</p>

<pre><code>{% set image = assetField.first().setTransform({width: 40}) %}
</code></pre>

<p>That should return an <code>AssetFileModel</code>, allowing you to do <code>{{ image64(image) }}</code></p>

<p>If you need to reuse the image with other transforms later, you might have to do some magic / use different variables.</p>

CC BY-SA 3.0
2017-10-20T22:02:47.310Z
2017-10-20T22:02:47.310Z
Erik Weber
<p>It's not going to work with this particular plugin:</p>

<p><code>
        // Require an instance of `AssetFileModel`
        if (! $asset instanceof AssetFileModel)
        {
            // Die quietly.
            return false;
        }
</code></p>

<p>It specifically checks the parameter that you pass in, and expects it to be an <code>AssetFileModel</code>. It wouldn't be too hard to modify the plugin to work for your use-case, though.</p>

<p>I see what you're trying to do; if you're using Craft 3, you could use the <a href="https://github.com/nystudio107/craft3-imageoptimize" rel="nofollow noreferrer">ImageOptimize</a> plugin to do it for you.</p>

CC BY-SA 3.0
2017-10-20T18:36:17.433Z
Yeah, that's what I was afraid of. So there's no way for Craft to return an `AssetFileModel` for an Image Transform, eh? I think there's a fork of the plug-in that might accept other parameter formats; I'll have to check that out.
Sandwich
CC BY-SA 3.0
2017-10-20T15:30:27.727Z
2017-10-20T15:30:27.727Z
andrew.welch
<p>I'm trying to use <a href="https://github.com/radarseven/Craft-Twig-ImageBase64" rel="nofollow noreferrer">this plugin</a>, but it requires an <code>AssetFileModel</code> as input. I can pass it the asset in question, no problem, but I'm wanting to pass it a transformed version of the asset. However, it seems like there's no way in Craft to pass a transformed image as an <code>AssetFileModel</code>.</p>

<p>Here's my code:</p>

<pre><code>{% set image = assetField.first() %}

{# Doesn't work; `image.url({width: 40})` outputs a URL as a string, not an AssetFileModel #}
{% set imageTiny = image.url({ width: 40 }) | image64(true) %}

&lt;div style="background: url({{ imageTiny }});"&gt;...&lt;/div&gt;
</code></pre>

<p>The desired result is for a base64-encoded string of the tiny, 40px-wide image to be output in the HTML.</p>

<p>Any ideas?</p>

<assets><image-transforms><assetfilemodel>
How can I base64-encode an Image Transform?
# How can I base64-encode an Image Transform?

(Asked by: username_0 on 2017-10-20)

I'm trying to use this plugin, but it requires an AssetFileModel as input. I can pass it the asset in question, no problem, but I'm wanting to pass it a transformed version of the asset. However, it seems like there's no way in Craft to pass a transformed image as an AssetFileModel.
Here's my code:
{% set image = assetField.first() %}

{# Doesn't work; `image.url({width: 40})` outputs a URL as a string, not an AssetFileModel #}
{% set imageTiny = image.url({ width: 40 }) | image64(true) %}

...

The desired result is for a base64-encoded string of the tiny, 40px-wide image to be output in the HTML.
Any ideas?

**Tags:** assets, image-transforms, assetfilemodel


---

## 2 answers

### Answer by username_1 on 2017-10-20. Score: 2 | Accepted answer

Try this:
{% set image = assetField.first().setTransform({width: 40}) %}

That should return an AssetFileModel, allowing you to do {{ image64(image) }}
If you need to reuse the image with other transforms later, you might have to do some magic / use different variables.


---

### Answer by username_2 on 2017-10-20. Score: 2

It's not going to work with this particular plugin:

`// Require an instance of `AssetFileModel`
if (! $asset instanceof AssetFileModel)
{
// Die quietly.
return false;
}`

It specifically checks the parameter that you pass in, and expects it to be an `AssetFileModel`. It wouldn't be too hard to modify the plugin to work for your use-case, though.

I see what you're trying to do; if you're using Craft 3, you could use the [ImageOptimize](https://github.com/nystudio107/craft3-imageoptimize) plugin to do it for you.

> **Comments:**
> **username_0**: Yeah, that's what I was afraid of. So there's no way for Craft to return an `AssetFileModel` for an Image Transform, eh? I think there's a fork of the plug-in that might accept other parameter formats; I'll have to check that out.  

---

