You are here

public function ImageStylesProvider::buildDerivativeLink in Consumer Image Styles 4.x

Same name and namespace in other branches
  1. 8.3 src/ImageStylesProvider.php \Drupal\consumer_image_styles\ImageStylesProvider::buildDerivativeLink()

Builds a derivative link based on the image URI and the image style.

Parameters

string $uri: The file URI.

\Drupal\image\ImageStyleInterface $image_style: The image style to apply.

Return value

array A structured array that complies with the JSON:API spec for links.

Overrides ImageStylesProviderInterface::buildDerivativeLink

See also

https://jsonapi.org/format/#document-links

File

src/ImageStylesProvider.php, line 69

Class

ImageStylesProvider
Class ImageStylesProvider.

Namespace

Drupal\consumer_image_styles

Code

public function buildDerivativeLink($uri, ImageStyleInterface $image_style) {
  $image = $this->imageFactory
    ->get($uri);
  $dimensions = [
    'width' => $image
      ->getWidth(),
    'height' => $image
      ->getHeight(),
  ];
  $image_style
    ->transformDimensions($dimensions, $uri);
  return [
    'href' => file_create_url($image_style
      ->buildUrl($uri)),
    'title' => $this
      ->t('Image Style: @name', [
      '@name' => $image_style
        ->label(),
    ]),
    'type' => $image
      ->getMimeType(),
    'meta' => [
      'rel' => static::DERIVATIVE_LINK_REL,
    ] + $dimensions,
  ];
}