Twig读取图像高度/宽度属性

Jan*_*ell 5 symfony twig symfony-2.2

我有这样的图像:

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

现在我想得到图像尺寸并添加宽度和高度属性

{% 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 %}
Run Code Online (Sandbox Code Playgroud)

这可能吗?

最好的祝福

小智 -2

在你的控制器中,执行

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

然后在你的树枝模板中

{% 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 %}
Run Code Online (Sandbox Code Playgroud)

这种方法的唯一限制/问题是,您需要在输出 URL 中包含动态变量(高度和宽度)。

请告诉我们进展如何

问候,