简体   繁体   English

在Drupal 8中的预处理函数中获取图像样式高度

[英]get image style height in pre process function in Drupal 8

I use Drupal 8 with the awesome inline responsive images module. 我使用Drupal 8和令人敬畏的内联响应图像模块。 I want to make changes to the img field (the fallback image) before the <picture> element is rendered, more specifically: I need to add the width and height parameters to the <img> field. 我想在呈现<picture>元素之前更改img字段(后备图像),更具体地说:我需要将widthheight参数添加到<img>字段。 I therefore use the preprocess_image hook. 因此我使用preprocess_image钩子。

This hook provides me with a bunch of variables, most notably $variables[attributes] . 这个钩子为我提供了一堆变量,最值得注意的是$variables[attributes] $variables[width] , $variables[height] and $variables[uri] are all empty strings for some reason. 由于某种原因, $variables[width]$variables[height]$variables[uri]都是空字符串。 Fortunately $variables[attributes] contains: $variables[attributes][data-entity-uuid] and $variables[attributes][srcset] so at least I have path to the styled image and the uuid to the original image. 幸运的是, $variables[attributes]包含: $variables[attributes][data-entity-uuid]$variables[attributes][srcset]所以至少我有到样式化图像的路径和原始图像的uuid。

I figured there are two ways to get to where I want to go (that is load the styled image and get height and width): 我想有两种方法可以到达我想去的地方(即加载样式图像并获得高度和宽度):

  1. convert the path to an uri (or is it path)? 将路径转换为uri(或路径)?
  2. Get the file id from the uuid and then somehow get the uri from the styled image (which seems like a detour to get what I want) 从uuid获取文件ID然后以某种方式从样式图像中获取uri(这似乎是绕道而行以获得我想要的东西)

I can't get option 1 to work. 我不能让选项1工作。 The path in srcset is like this: /sites/default/files/styles/image_lightbox/public/inline-images/erf-2.jpg?itok=4_EU9Ttx and I think I need to convert that to public://styles/image_lightbox/public/inline-images/erf-2.jpg but got stuck at something like: srcset中的路径是这样的: /sites/default/files/styles/image_lightbox/public/inline-images/erf-2.jpg?itok=4_EU9Ttx我认为我需要将其转换为public://styles/image_lightbox/public/inline-images/erf-2.jpg但是遇到了类似的问题:

$parsed_url = parse_url($variables['attributes']['srcset']);
$path = file_build_uri($parsed_url['path']);

but that still left the /sites/default/files part in there 但仍留有/ sites / default / files部分

I can't get option 2 to work. 我不能让选项2工作。 I'm stuck at: 我坚持:

$file_array = \Drupal::entityTypeManager()->getStorage('file')->loadByProperties(['uuid' => $img_uuid]);
$file_id = reset(array_keys($file_array));
$file = File::load($file_id);
$image_uri = ImageStyle::load('image-lightbox')->buildUrl($file->getFileUri());
$image = \Drupal::service('image.factory')->get($image_uri);

This fails at $file = File::load($file_id) for some reason 由于某种原因,这在$file = File::load($file_id)失败

After wasting 8 hours of my life in getting this solved I would be very grateful with any help 在浪费了8个小时的生命来解决这个问题后,我会非常感激任何帮助

The answer was much simpler than I thought. 答案比我想象的要简单得多。

Option 1 turned out to be very easy: Instead of converting /sites/default/files/styles/image_lightbox/public/inline-images/erf-2.jpg?itok=4_EU9Ttx (from $variables[attributes][srcset] ) to public://styles/image_lightbox/public/inline-images/erf-2.jpg . 选项1变得非常简单:而不是将/sites/default/files/styles/image_lightbox/public/inline-images/erf-2.jpg?itok=4_EU9Ttx (来自$variables[attributes][srcset] )转换为public://styles/image_lightbox/public/inline-images/erf-2.jpg I had to convert it to http://hostname/path-to-drupal-install/sites/default/files/styles/image_lightbox/public/inline-images/erf-2.jpg?itok=4_EU9Ttx which I did like this: 我不得不把它转换为http://hostname/path-to-drupal-install/sites/default/files/styles/image_lightbox/public/inline-images/erf-2.jpg?itok=4_EU9Ttx我这样做了:

global $base_url;
$image_uri = $base_url.$variables['attributes']['srcset'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM