gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 18:05:55 25 4
gpt4 key购买 nike

我有一个像这样的图像:

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

现在我想获取图像尺寸并添加宽度和高度属性,例如
{% 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 %}

这可能吗?

此致

最佳答案

在你的 Controller 中,做

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

然后在你的 Twig 模板中
{% 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 %}

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

请告诉我们进展如何

问候,

关于symfony - Twig 读取图像高度/宽度属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17720069/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com