[英]Drupal 8 images with image style
In drupal 7, i use function image_style_url('style', uri)
to generate new image with style and return image's path.在 drupal 7 中,我使用函数image_style_url('style', uri)
生成具有样式的新图像并返回图像的路径。 so what will be instead of it in drupal 8?那么在drupal 8 中会是什么呢? thanks谢谢
Per the change records :根据更改记录:
use Drupal\image\Entity\ImageStyle;
$path = 'public://images/image.jpg';
$url = ImageStyle::load('style_name')->buildUrl($path);
You should try to use the new Drupal functions wherever possible.您应该尽可能尝试使用新的 Drupal 函数。
Instead, use:相反,使用:
use Drupal\file\Entity\File;
use Drupal\image\Entity\ImageStyle;
$fid = 123;
$file = File::load($fid);
$image_uri = ImageStyle::load('your_style-name')->buildUrl($file->getFileUri());
Edited as per https://www.drupal.org/node/2050669 :根据https://www.drupal.org/node/2050669编辑:
$original_image = 'public://images/image.jpg';
// Load the image style configuration entity
use Drupal\image\Entity\ImageStyle;
$style = ImageStyle::load('thumbnail');
$uri = $style->buildUri($original_image);
$url = $style->buildUrl($original_image);
In your Controllers and other OOP part of Drupal you can use :在您的控制器和 Drupal 的其他 OOP 部分中,您可以使用:
use Drupal\image\Entity\ImageStyle;
$path = 'public://images/image.jpg';
$url = ImageStyle::load('style_name')->buildUrl($path);
And in YOUR_THEME.theme
file while Error: Class 'ImageStyle' not found in YOURTHEMENAME_preprocess_node
you can do it with the follwing在YOUR_THEME.theme
文件中,而Error: Class 'ImageStyle' not found in YOURTHEMENAME_preprocess_node
您可以使用以下方法进行操作
$path = 'public://images/image.jpg';
$style = \Drupal::entityTypeManager()->getStorage('image_style')->load('thumbnail');
$url = $style->buildUrl($path);
Another method is provide a renderable array and let the drupal Render engine render it.另一种方法是提供一个可渲染数组并让 drupal 渲染引擎渲染它。
$render = [
'#theme' => 'image_style',
'#style_name' => 'thumbnail',
'#uri' => $path,
// optional parameters
];
I have found that I often want to preprocess the image to apply an image style to an image on a node or a paragraph type.我发现我经常想要预处理图像以将图像样式应用于节点或段落类型上的图像。 In many cases I have created a paragraph that allows the user to choose the width of the image as a percentage.在许多情况下,我创建了一个段落,允许用户以百分比形式选择图像的宽度。 In the preprocess I would check the value of the width and apply the correct image style.在预处理中,我会检查宽度的值并应用正确的图像样式。
use Drupal\image\Entity\ImageStyle;
function THEME_preprocess_paragraph__basic_content(&$vars) {
//get the paragraph
$paragraph = $vars['paragraph'];
//get the image
$images = $paragraph->get('field_para_image');
//get the images value, in my case I only have one required image, but if you have unlimited image, you could loop thru $images
$uri = $images[0]->entity->uri->value;
//This is my field that determines the width the user wants for the image and is used to determine the image style
$preset = $paragraph->get('field_column_width')->value;
$properties = array();
$properties['title'] = $images[0]->getValue()['title'];
$properties['alt'] = $images[0]->getValue()['alt'];
//this is where the Image style is applied
switch($preset) {
case 'image-20':
$properties['uri'] = ImageStyle::load('width_20_percent')->buildUrl($uri);
break;
case 'image-45':
$properties['uri'] = ImageStyle::load('width_45_percent')->buildUrl($uri);
break;
case 'image-55':
$properties['uri'] = ImageStyle::load('width_55_percent')->buildUrl($uri);
break;
case 'image-100':
$properties['uri'] = ImageStyle::load('width_100_percent')->buildUrl($uri);
break;
}
//assign to a variable that the twig template can use
$vars['basic_image_display'] = $properties;
}
In this example, I am preprocessing a specific paragraph type named "basic_content" but you can do the same thing with a node preprocess.在此示例中,我正在预处理名为“basic_content”的特定段落类型,但您可以使用节点预处理执行相同的操作。 Continuing my example, I would have a twig template named paragraph--basic_content.html.twig to handle the display of that paragraph type.继续我的示例,我将有一个名为paragraph--basic_content.html.twig 的twig模板来处理该段落类型的显示。
Displaying the image would be something like this in the twig file.在树枝文件中显示图像将是这样的。
<img class="img-responsive" src="{{basic_image_display['uri']}}" alt="{{ basic_image_display['alt'] }}" title="{{ basic_image_display['title'] }}"/>
Works for me from a classic Drupal database Query in .module file :从 .module 文件中的经典 Drupal 数据库查询对我有用:
$query = \Drupal::database()->select('file_managed', 'f' );
$query->addField('f', 'uri');
$pictures = $query->execute()->fetchAll();
foreach ($pictures as $key => $picture) {
$largePictureUri = entity_load('image_style', 'large')->buildUrl($picture->uri);
}
$view_mode = $variables['content']['field_media_image']['0']['#view_mode'];
$display_content = \Drupal::service('entity_display.repository')
->getViewDisplay('media', 'image', $view_mode)->build($media_entity);
$style = ImageStyle::load($display_content['image'][0]['#image_style']); $original_image = $media_entity->get('image')->entity->getFileUri();
$destination = $style->buildUri($original_image);
This is how you get image style from a media image entity.这就是您从媒体图像实体获取图像样式的方式。
I used in Drupal 8 this code.我在 Drupal 8 中使用了这段代码。 It's working fine.它工作正常。
$fid = 374; //get your file id, this value just for example
$fname = db_select('file_managed', 'f')->fields('f', array('filename'))->condition('f.fid', $fid)->execute()->fetchField();
$url = entity_load('image_style', 'YOUR_STYLE_NAME')->buildUrl($fname);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.