Twig reads image height / width attributes

I have an image like this:

{% image output='/images/foo.jpg' '@MyBundle/Resources/public/images/bar.jpg' %} <img src="{{ asset(asset_url) }}" alt="Something"> {% endimage %} 

now I would like to get the dimensions of the image and add width and height attributes like

 {% image output='/images/foo.jpg' '@MyBundle/Resources/public/images/bar.jpg' %} <img src="{{ asset(asset_url) }}" alt="Something" width="{{ asset(asset_width) }}" height="{{ asset(asset_height) }}"> {% endimage %} 

Is it possible?

Best wishes

+4
source share
1 answer

In your controller do

 return $this->render( 'assetic' => array( 'vars' => array( 'height' => '300px', 'width' => '400px' ) ); 

Then in your template branch

 {% image vars=['height', 'width'] output='/images/foo.jpg?{height}x{width}' '@MyBundle/Resources/public/images/bar.jpg' %} <img src="{{ asset(asset_url) }}" alt="Something" height="{{assetic.vars.height}}" width="{{assetic.vars.width}}"> {% endimage %} 

The only limitation / problem with this approach is that you need to include dynamic variables (height and width) in the output URL.

Let us know how it happened.

Hello,

-2
source

Source: https://habr.com/ru/post/1492048/


All Articles