我正在处理复杂的图像样式,我希望能够快速加载所有不同的衍生产品,以检查它们是否被正确处理。
但我看不出怎样才能轻易地得到链接。我能想到的最好的方法就是在JSONAPI中打开节点,然后用https://www.drupal.org/project/drupal/issues/2825812的补丁从它获得衍生产品的URL。
有更简单的东西吗?
回到Drupal 6天中,Image模块显示到图像节点本身上所有大小的链接。
发布于 2021-08-18 14:56:41
您可以使用以下代码获得该信息:
$original_uri = 'public://images/image.jpg';
$styles = \Drupal::entityTypeManager()->getStorage('image_style')->loadMultiple();
$urls = array_map(function($style) use ($original_uri) {
return $style->buildUri($original_uri);
}, $styles);$urls现在应该包含一个基于原始URL的URL列表,一个用于系统中的每个图像样式。
发布于 2021-08-18 22:31:26
要获取应用特定图像样式的图像的图像派生URI,可以使用以下代码。
// Load the ImageStyle instance in $image_style, for example with
// $image_style = ImageStyle::load($id).
// $image_uri is the path/URI for the image to which the style is applied.
if ($image_style->supportsUri($image_uri)) {
$derivative_uri = $style->buildUri($image_uri);
}请记住,只有当从$image_style->buildURL($image_uri)返回的URL被请求时(从浏览器)才会创建图像派生。构建URI (或URL)不会生成派生映像;它会返回不存在的图像的URI (URL)。
为了检查可以创建的图像导数,应该使用类似于从ImageStyleDownloadController::deliver()中使用的代码。
if ($image_style->supportsUri($image_uri)) {
$derivative_uri = $style->buildUri($image_uri);
if (!file_exists($image_uri)) {
$path_info = pathinfo($image_uri);
$converted_image_uri = $path_info['dirname'] . DIRECTORY_SEPARATOR . $path_info['filename'];
if (file_exists($converted_image_uri)) {
$image_uri = $converted_image_uri;
}
}
$success = file_exists($derivative_uri) || $image_style->createDerivative($image_uri, $derivative_uri);
if ($success) {
$image = Drupal::service('image.factory')->get($derivative_uri);
$uri = $image->getSource();
}
}https://drupal.stackexchange.com/questions/305714
复制相似问题