Skip to content
Snippets Groups Projects
Commit c8376c38 authored by Lars Schröter's avatar Lars Schröter Committed by Jakob P
Browse files

Issue #3301224 by osopolar: Follow-up: Very slow JSON:API responses when...

Issue #3301224 by osopolar: Follow-up: Very slow JSON:API responses when images are stored on AWS bucket
parent 57c138a3
No related branches found
No related tags found
1 merge request!10#3301224 Build links using ImageStylesProvider::buildDerivativeLink()
Pipeline #306371 passed with warnings
......@@ -10,6 +10,7 @@ use Drupal\consumer_image_styles\ImageStylesProvider;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\File\FileUrlGeneratorInterface;
use Drupal\Core\Image\ImageFactory;
use Drupal\Core\Url;
use Drupal\image\ImageStyleInterface;
use Drupal\jsonapi\JsonApiResource\Link;
use Drupal\jsonapi\JsonApiResource\LinkCollection;
......@@ -120,15 +121,17 @@ class LinkCollectionNormalizer implements NormalizerInterface {
// Generate derivatives only for the found ones.
$image_styles = $this->imageStylesProvider->loadStyles($consumer);
return array_reduce($image_styles, function (LinkCollection $decorated, ImageStyleInterface $image_style) use ($uri) {
$image = $this->imageFactory->get($uri);
$dimensions = [
'width' => $image->getWidth(),
'height' => $image->getHeight(),
];
$image_style->transformDimensions($dimensions, $uri);
$link = $this->imageStylesProvider->buildDerivativeLink($uri, $image_style);
$dimensions = [];
if (isset($link['meta']['width'])) {
$dimensions['width'] = $link['meta']['width'];
}
if (isset($link['meta']['height'])) {
$dimensions['width'] = $link['meta']['height'];
}
$variant_link = new Link(
CacheableMetadata::createFromObject($image_style),
$this->fileUrlGenerator->generate($image_style->buildUrl($uri)),
Url::fromUri($link['href']),
ImageStylesProvider::DERIVATIVE_LINK_REL,
// Target attributes can only be strings, but dimensions are links.
array_map(function (?int $dimension): string {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment