use*_*914 22 drupal image drupal-8
在drupal 7中,我使用函数image_style_url('style', uri)生成带有样式的新图像并返回图像的路径.那么在drupal 8中会有什么而不是它呢?谢谢
Cli*_*ive 54
根据变更记录:
use Drupal\image\Entity\ImageStyle;
$path = 'public://images/image.jpg';
$url = ImageStyle::load('style_name')->buildUrl($path);
Run Code Online (Sandbox Code Playgroud)
sag*_*ons 14
您应尽可能尝试使用新的Drupal功能.
相反,使用:
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());
Run Code Online (Sandbox Code Playgroud)
根据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);
Run Code Online (Sandbox Code Playgroud)
在您的控制器和Drupal的其他OOP部分中,您可以使用:
use Drupal\image\Entity\ImageStyle;
$path = 'public://images/image.jpg';
$url = ImageStyle::load('style_name')->buildUrl($path);
Run Code Online (Sandbox Code Playgroud)
在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);
Run Code Online (Sandbox Code Playgroud)
另一种方法是提供一个可渲染的数组,让drupal Render引擎渲染它.
$render = [
'#theme' => 'image_style',
'#style_name' => 'thumbnail',
'#uri' => $path,
// optional parameters
];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22123 次 |
| 最近记录: |