[英]Drupal: where to find the image style applied to on an image field? Efficiently find the URL of styled images?
[英]Drupal 8 images with image style
在 drupal 7 中,我使用函數image_style_url('style', uri)
生成具有樣式的新圖像並返回圖像的路徑。 那么在drupal 8 中會是什么呢? 謝謝
根據更改記錄:
use Drupal\image\Entity\ImageStyle;
$path = 'public://images/image.jpg';
$url = ImageStyle::load('style_name')->buildUrl($path);
您應該盡可能嘗試使用新的 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());
根據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);
在您的控制器和 Drupal 的其他 OOP 部分中,您可以使用:
use Drupal\image\Entity\ImageStyle;
$path = 'public://images/image.jpg';
$url = ImageStyle::load('style_name')->buildUrl($path);
在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);
另一種方法是提供一個可渲染數組並讓 drupal 渲染引擎渲染它。
$render = [
'#theme' => 'image_style',
'#style_name' => 'thumbnail',
'#uri' => $path,
// optional parameters
];
我發現我經常想要預處理圖像以將圖像樣式應用於節點或段落類型上的圖像。 在許多情況下,我創建了一個段落,允許用戶以百分比形式選擇圖像的寬度。 在預處理中,我會檢查寬度的值並應用正確的圖像樣式。
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;
}
在此示例中,我正在預處理名為“basic_content”的特定段落類型,但您可以使用節點預處理執行相同的操作。 繼續我的示例,我將有一個名為paragraph--basic_content.html.twig 的twig模板來處理該段落類型的顯示。
在樹枝文件中顯示圖像將是這樣的。
<img class="img-responsive" src="{{basic_image_display['uri']}}" alt="{{ basic_image_display['alt'] }}" title="{{ basic_image_display['title'] }}"/>
從 .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);
這就是您從媒體圖像實體獲取圖像樣式的方式。
我在 Drupal 8 中使用了這段代碼。 它工作正常。
$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.