You are here

public function TwigExtension::imageStyle in Twig Tweak 8.2

Same name and namespace in other branches
  1. 8 src/TwigExtension.php \Drupal\twig_tweak\TwigExtension::imageStyle()

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.

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.

File

src/TwigExtension.php, line 1065

Class

TwigExtension
Twig extension with some useful functions and filters.

Namespace

Drupal\twig_tweak

Code

public function imageStyle($path, $style) {

  // @phpcs:ignore DrupalPractice.Objects.GlobalClass.GlobalClass
  if (!($image_style = ImageStyle::load($style))) {
    trigger_error(sprintf('Could not load image style %s.', $style));
    return;
  }
  if (!$image_style
    ->supportsUri($path)) {
    trigger_error(sprintf('Could not apply image style %s.', $style));
    return;
  }
  return file_url_transform_relative($image_style
    ->buildUrl($path));
}