I am trying to print an image using an image style that I've created in my drupal 8 theme.
I can print an image in a template by doing {{ content.banner }} but how do I apply the image style to that image? I tried looking for documentation but there doesn't seem to be any out there.
This code worked for me to apply the image style to the field named field_teaser_image: {# set image style #} {% set img = content. field_teaser_image. 0|merge({ '#image_style' : 'thumbnail'}) %} {# render image #} {{ img }} Thanks pavlovich / Megan!
Go to Configuration > Media > Image styles and define styles as needed.
Image styles are used to set presets for image processing. Using image styles, we can crop, desaturate, resize, rotate and scale images. We can also add various effects before an image is displayed. When an image is displayed with a style, a new image file is created and the original image is left unchanged.
Currently drupal 8 doesn't have special filter for applying image style. Instead you can set new attribute for your image like this:
{% set image = image|merge({'#image_style': 'thumbnail'}) %}
Then simply output your updated image:
{{ image }}
P.S . You can do {{ dump(image) }}
to see what attributes you can update
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With