我正在尝试使用我在drupal 8主题中创建的图像样式打印图像。
我可以通过执行{{ content.banner }在模板中打印图像,但是如何将图像样式应用于该图像?我试着寻找文档,但似乎没有任何文档。
发布于 2015-11-12 10:56:17
目前,drupal 8没有特殊的过滤器应用图像样式。相反,您可以为您的图像设置如下新属性:
{% set image = image|merge({'#image_style': 'thumbnail'}) %}然后,只需输出更新的图像:
{{ image }}P.S .您可以通过{{ dump(image) }}查看可以更新哪些属性。
发布于 2016-07-28 08:23:22
我通过创建自己的Twig过滤器来解决这个问题。
您也可以创建自己的模块,公开这个过滤器。
你可以重复使用它。
码
namespace Drupal\twig_extender\TwigExtension;
use Drupal\node\Entity\Node;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\file\Entity\File;
use Drupal\image\Entity\ImageStyle;
class Images extends \Twig_Extension {
/**
* Generates a list of all Twig functions that this extension defines.
*/
public function getFunctions(){
return array(
new \Twig_SimpleFunction('image_style', array($this, 'imageStyle'), array('is_safe' => array('html'))),
);
}
/**
* Gets a unique identifier for this Twig extension.
*/
public function getName() {
return 'twig_extender.twig.images';
}
/**
* Generate images styles for given image
*/
public static function imageStyle($file_id, $styles) {
$file = File::load($file_id);
$transform = array();
if (isset($file->uri->value)) {
$transform['source'] = $file->url();
foreach ($styles as $style) {
$transform[$style] = ImageStyle::load($style)->buildUrl($file->uri->value);
}
}
return $transform;
}
}使用
{% set transform = image_style(image.entity.fid.value, ['thumbnail', 'large']) %}然后您就可以访问源映像和样式了。
{{ transform.source }}
{{ transform.thumbnail }}
{{ transform.large }}希望它能对你有帮助!
发布于 2019-11-08 14:19:24
如何做到这一点,而没有控制模块:使它成为您自己的渲染数组。
{% for item in content.field_images['#items'] %}
{% set image = {
'#theme': 'image_style',
'#style_name': 'medium',
'#uri': item.entity.uri.value,
'#alt': item.alt,
'#width': item.width,
'#height': item.height
} %}
{{ image }}
{% endfor %}https://stackoverflow.com/questions/33510051
复制相似问题