Skip to content

Commit

Permalink
[TASK] Enable rendering of original size as biggest size alexandersch…
Browse files Browse the repository at this point in the history
  • Loading branch information
twaurisch committed Dec 11, 2017
1 parent 6051215 commit 16ed430
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions Classes/Resource/Rendering/ImageRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ protected function processSourceCollection(File $originalFile, array $defaultPro
{
$configuration = $this->getConfiguration();

$ignoredWidths = [];

foreach ($configuration->getSourceCollection() as $sourceCollection) {
try {
if (!is_array($sourceCollection)) {
Expand All @@ -183,6 +185,10 @@ protected function processSourceCollection(File $originalFile, array $defaultPro
}

if ((int)$sourceCollection['width'] > (int)$this->defaultWidth) {
$width = (int)$sourceCollection['width'];
$ignoredWidths[$width]['dataKey'] = $sourceCollection['dataKey'];
$ignoredWidths[$width]['srcset'] = $sourceCollection['srcset'];

throw new \RuntimeException();
}

Expand All @@ -196,12 +202,22 @@ protected function processSourceCollection(File $originalFile, array $defaultPro

$url = $configuration->getAbsRefPrefix() . $processedFile->getPublicUrl();

$this->data['data-' . $sourceCollection['dataKey']] = $url;
$this->srcset[] = $url . rtrim(' ' . $sourceCollection['srcset'] ?: '');
$this->addItemForRendering($url, $sourceCollection['dataKey'], $sourceCollection['srcset']);
} catch (\Exception $ignoredException) {
continue;
}
}

if ($this->getMinKeyFromArray($ignoredWidths)) {
$width = $this->getMinKeyFromArray($ignoredWidths);

$url = $configuration->getAbsRefPrefix() . $originalFile->getPublicUrl();

$dataKey = $ignoredWidths[$width]['dataKey'];
$srcset = $ignoredWidths[$width]['srcset'];

$this->addItemForRendering($url, $dataKey, $srcset);
}
}

/**
Expand Down Expand Up @@ -288,4 +304,22 @@ protected function buildImageTag($src, FileInterface $file, $width, $height, arr

return $tagBuilder->render();
}

/**
* @param array $items
* @return mixed
*/
protected function getMinKeyFromArray(array $items) {
return min(array_keys($items));
}

/**
* @param string $url
* @param string $dataKey
* @param string $srcset
*/
protected function addItemForRendering($url, $dataKey, $srcset) {
$this->data['data-' . $dataKey] = $url;
$this->srcset[] = $url . rtrim(' ' . $srcset ?: '');
}
}

0 comments on commit 16ed430

Please sign in to comment.