Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-10-20T22:02:47.310Z
Answers.LastActivityDate: 2017-10-20T22:02:47.310Z
Answers.UserDisplayName: Erik Weber
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-10-20T18:36:17.433Z
Answers.Text: 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.
Answers.UserDisplayName: Sandwich
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-10-20T15:30:27.727Z
Answers.LastActivityDate: 2017-10-20T15:30:27.727Z
Answers.UserDisplayName: andrew.welch
Body: <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>
  
Tags: <assets><image-transforms><assetfilemodel>
Title: How can I base64-encode an Image Transform?
ThreadText: # How can I base64-encode an Image Transform?
  
  (Asked by: username_0 on 2017-10-20)
  
  I'm trying to use [this plugin](https://github.com/radarseven/Craft-Twig-ImageBase64), 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) %}
  
  <div style="background: url({{ imageTiny }});">...</div>
  
  ```
  
  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.  
  
  ---
  
  