public function Render::renderImageStyle in Bamboo Twig 8.2
Same name and namespace in other branches
- 8.5 bamboo_twig_loader/src/TwigExtension/Render.php \Drupal\bamboo_twig_loader\TwigExtension\Render::renderImageStyle()
- 8.3 bamboo_twig_loader/src/TwigExtension/Render.php \Drupal\bamboo_twig_loader\TwigExtension\Render::renderImageStyle()
- 8.4 bamboo_twig_loader/src/TwigExtension/Render.php \Drupal\bamboo_twig_loader\TwigExtension\Render::renderImageStyle()
Returns the URL of this image derivative for an original image path or URI.
Parameters
string $path: The path or URI to the original image.
string $style: The image style.
bool $preprocess: Bypass the standard Drupal process to pre-generate the image style.
Return value
string|null The absolute URL where a style image can be downloaded, suitable for use in an <img> tag. Requesting the URL will cause the image to be created. Exceptend when preprocess is enabled, the image will already be available on the fso.
File
- bamboo_twig_loader/
src/ TwigExtension/ Render.php, line 142
Class
- Render
- Provides some renderer as Twig Extensions.
Namespace
Drupal\bamboo_twig_loader\TwigExtensionCode
public function renderImageStyle($path, $style, $preprocess = FALSE) {
$image_style = $this
->getImageStyleStorage()
->load($style);
// Assert the requested style exist, otherwise return null.
if (!$image_style) {
return NULL;
}
// From an uri or path retrieve an image object.
$image = $this
->getImageFactory()
->get($path);
// Assert the image exist, otherwise return null.
if (empty($image)) {
return NULL;
}
// Lazy load the fso.
$fso = $this
->getFileSystemObject();
// Assert the image exist on the file system.
$image_path = $fso
->realpath($image
->getSource());
if (!is_file($image_path)) {
return NULL;
}
// When user want to preprocess the derivated instead of waiting first
// HTTP call.
if ($preprocess) {
$image_style_uri = $image_style
->buildUri($path);
// Assert the image style doesn't already exist.
$image_style_path = $fso
->realpath($image_style_uri);
if (!is_file($image_style_path)) {
// createDerivative need an URI so transform none uri.
if (file_valid_uri($path)) {
$image_uri = $path;
}
else {
$image_uri = file_build_uri($path);
}
// Create the new image derivative.
$image_style
->createDerivative($image_uri, $image_style_uri);
return file_create_url($image_style_uri);
}
}
return $image_style
->buildUrl($path);
}