【问题标题】:How can I load an image by an uri in drupal 8?如何在 drupal 8 中通过 uri 加载图像?
【发布时间】:2019-03-19 19:41:12
【问题描述】:

我用 customtemplate_preprocess_image 函数编写了一个自定义模板。

在这个函数中,我只能使用 $variables['uri'] 来加载相同的图像,但图像样式不同。 uri 的格式如下: '/sites/default/files/styles/image_xs/public/2019-02/IMG_20181219_135712499_0.jpg?itok=1faUCioy'

如果我现在这样做

$style = ImageStyle::load('image_lightbox');
$image = \Drupal::service('image.factory')->get($variables['uri']);
if ($image->isValid()) {
    print("Image url: ".$variables['uri'].'<br>');
}
else {
    print("Image invalid: ".$variables['uri'].'<br>');
}

它呈现“图像无效:/sites/default/files/styles/image_xs/public/2019-02/IMG_20181219_135712499_0.jpg?itok=1faUCioy

为什么?只有这个 uri 我该如何解决这个问题?

【问题讨论】:

标签: php drupal drupal-8


【解决方案1】:

您可以在 drupal 8 的 twig 文件中使用文件 uri。

{{ file_url(node.field_image.entity.fileuri) }}

【讨论】:

  • 如何访问 image_preprocess 函数中的 $entity 变量? $variables['entity'] 不存在。
  • 尝试通过这种方式获取_preprocess_image中的图像$styled_image_url = ImageStyle::load('image_lightbox')-&gt;buildUrl($node-&gt;field_image-&gt;entity-&gt;getFileUri());
  • 谢谢,但 $node 变量在预处理图像中不可访问
猜你喜欢
  • 1970-01-01
  • 2016-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-11
  • 2014-04-28
  • 1970-01-01
相关资源
最近更新 更多