You are here

public static function TwigTweakExtension::imageStyleFilter in Twig Tweak 3.1.x

Same name and namespace in other branches
  1. 3.x src/TwigTweakExtension.php \Drupal\twig_tweak\TwigTweakExtension::imageStyleFilter()

Returns the URL of this image derivative for an original image path or URI.

Parameters

string|null $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/TwigTweakExtension.php, line 483

Class

TwigTweakExtension
Twig extension with some useful functions and filters.

Namespace

Drupal\twig_tweak

Code

public static function imageStyleFilter(?string $path, string $style) : ?string {
  if (!$path) {
    trigger_error('Image path is empty.');
    return NULL;
  }
  if (!($image_style = ImageStyle::load($style))) {
    trigger_error(sprintf('Could not load image style %s.', $style));
    return NULL;
  }
  if (!$image_style
    ->supportsUri($path)) {
    trigger_error(sprintf('Could not apply image style %s.', $style));
    return NULL;
  }
  return file_url_transform_relative($image_style
    ->buildUrl($path));
}