aelvan / Imager-Craft

This plugin has been DEPRECATED. Check out Imager X instead.
MIT License
342 stars 69 forks source link

How to set parameters from fields? #262

Closed MrGlasspoole closed 5 years ago

MrGlasspoole commented 5 years ago

How can i set parameters like width, height, format and so on from a field? Everything i tried did not work.

Like:

      {% set transformedImages = craft.imager.transformImage(image,
        [
          { width: {{ myWidthField }}, jpegQuality: 90 }
        ],
          {
            format: '{{ myFormatField }}',
          }
      ) %}

In Expression Engine i was using CE Image and did things like that: <a href="{produkte_ma_text_bild}">{exp:ce_img:make src="{produkte_ma_text_bild}" max_height="{produkte_ma_text_bild_height}"}<img class="proimg" src="{made}" alt="{title}" title="{title}" width="{width}" height="{height}" />{/exp:ce_img:make}</a>

If you write an article with images they are all of different sizes...

MrGlasspoole commented 5 years ago

Ok i got it. I can do:

{% set imageSizes = [{ width: 800}, { width: 640}, { width: 480}, { width: 320}, { width: 160}] %}
{% set transformedImages = craft.imager.transformImage(image, imageSizes %}
aelvan commented 5 years ago

Inside twig tags, you always just use variables in the twig context directly, you never use twig in twig, so:

{% set transformedImages = craft.imager.transformImage(image,
        [
          { width: myWidth, jpegQuality: 90 }
        ],
          {
            format: myFormat,
          }
      ) %}

If these are fields on an entry variable, you'd do entry.myWidthField.

Refer to the Twig and Craft documentation for more information on this.