1

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) %}

<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?

2 Answers 2

2

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.

0
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 plugin to do it for you.

1
  • 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
    Commented Oct 20, 2017 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.