Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor autogenerate feature v3 #622

Draft
wants to merge 3 commits into
base: 3.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 47 additions & 37 deletions Classes/DataProcessing/FilesProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,51 +175,61 @@ protected function processFiles(array $properties = []): ?array
{
$data = [];
$cropVariant = $this->processorConfiguration['processingConfiguration.']['cropVariant'] ?? 'default';
$formats = $this->processorConfiguration['processingConfiguration.']['formats.'] ?? [];

// Legacy workaround
$autogenerateConfig = $this->processorConfiguration['processingConfiguration.']['autogenerate.'] ?? null;
if ($autogenerateConfig) {
if (($autogenerateConfig['retina2x'] ?? 0) == 1) {
$formats['urlRetina'] = [
'factor' => FileUtility::RETINA_RATIO,
];
}
if (($autogenerateConfig['lqip'] ?? 0) == 1) {
$formats['urlLqip'] = [
'factor' => FileUtility::LQIP_RATIO,
];
}
}

foreach ($this->fileObjects as $key => $fileObject) {
if (isset($this->processorConfiguration['processingConfiguration.']['autogenerate.'])) {
if ($formats) {
$delayProcessing = (int)($this->processorConfiguration['processingConfiguration.']['delayProcessing'] ?? 0) === 1;

// 1. render image as usual
$file = $this->getFileUtility()->processFile(
$fileObject,
$properties,
$cropVariant,
(int)($this->processorConfiguration['processingConfiguration.']['delayProcessing'] ?? 0) === 1
$delayProcessing
);

$targetWidth = (int)($properties['width'] ?: $file['properties']['dimensions']['width']);
$targetHeight = (int)($properties['height'] ?: $file['properties']['dimensions']['height']);

if (isset($this->processorConfiguration['processingConfiguration.']['autogenerate.']['retina2x']) &&
(int)$this->processorConfiguration['processingConfiguration.']['autogenerate.']['retina2x'] === 1 &&
($targetWidth || $targetHeight)) {
$file['urlRetina'] = $this->getFileUtility()->processFile(
$fileObject,
array_merge(
$properties,
[
'width' => $targetWidth * FileUtility::RETINA_RATIO,
'height' => $targetHeight * FileUtility::RETINA_RATIO,
]
),
$cropVariant,
(int)($this->processorConfiguration['processingConfiguration.']['delayProcessing'] ?? 0) === 1
)['publicUrl'];
}

if (isset($this->processorConfiguration['processingConfiguration.']['autogenerate.']['lqip']) &&
(int)$this->processorConfiguration['processingConfiguration.']['autogenerate.']['lqip'] === 1 &&
($targetWidth || $targetHeight)) {
$file['urlLqip'] = $this->getFileUtility()->processFile(
$fileObject,
array_merge(
$properties,
[
'width' => $targetWidth * FileUtility::LQIP_RATIO,
'height' => $targetHeight * FileUtility::LQIP_RATIO,
]
),
$cropVariant,
(int)($this->processorConfiguration['processingConfiguration.']['delayProcessing'] ?? 0) === 1
)['publicUrl'];
// 2. render additional formats
$originalWidth = $fileObject->getProperty('width');
$originalHeight = $fileObject->getProperty('height');
$targetWidth = (int)($properties['width'] ?? $file['properties']['dimensions']['width']);
$targetHeight = (int)($properties['height'] ?? $file['properties']['dimensions']['height']);
if ($targetWidth || $targetHeight) {
foreach ($formats as $formatKey => $formatConf) {
$formatKey = rtrim($formatKey, '.');
$factor = (float)($formatConf['factor'] ?? 1.0);

$file[$formatKey] = $this->getFileUtility()->processFile(
$fileObject,
array_merge(
$properties,
[
'fileExtension' => $formatConf['fileExtension'] ?? null,
// multiply width/height by factor,
// but don't stretch image beyond its original dimensions!
'width' => min($targetWidth * $factor, $originalWidth),
'height' => min($targetHeight * $factor, $originalHeight),
]
),
$cropVariant,
$delayProcessing
)['publicUrl'];
}
}

$data[] = $file;
Expand Down
60 changes: 41 additions & 19 deletions Classes/DataProcessing/GalleryProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,23 @@ private function getCroppedDimensionalPropertyFromProcessedFile(array $processed
*/
protected function prepareGalleryData()
{
$formats = $this->processorConfiguration['formats.'] ?? [];

// Legacy workaround
$autogenerateConfig = $this->processorConfiguration['autogenerate.'] ?? null;
if ($autogenerateConfig) {
if (($autogenerateConfig['retina2x'] ?? 0) == 1) {
$formats['urlRetina'] = [
'factor' => FileUtility::RETINA_RATIO,
];
}
if (($autogenerateConfig['lqip'] ?? 0) == 1) {
$formats['urlLqip'] = [
'factor' => FileUtility::LQIP_RATIO,
];
}
}

for ($row = 1; $row <= $this->galleryData['count']['rows']; $row++) {
for ($column = 1; $column <= $this->galleryData['count']['columns']; $column++) {
$fileKey = (($row - 1) * $this->galleryData['count']['columns']) + $column - 1;
Expand All @@ -186,31 +203,36 @@ protected function prepareGalleryData()
if ($fileObj) {
if ($fileObj['properties']['type'] === 'image') {
$image = $this->getImageService()->getImage((string)$fileObj['properties']['fileReferenceUid'], null, true);
$fileObj = $this->getFileUtility()->processFile($image, $this->mediaDimensions[$fileKey] ?? []);

if (isset($this->processorConfiguration['autogenerate.']['retina2x'],
$fileObj['properties']['dimensions']['width']) &&
(int)$this->processorConfiguration['autogenerate.']['retina2x'] === 1) {
$fileObj['urlRetina'] = $this->getFileUtility()->processFile(
// 1. render image as usual
$fileObj = $this->getFileUtility()->processFile(
$image,
array_merge(
['fileExtension' => $this->processorConfiguration['fileExtension'] ?? null],
$this->mediaDimensions[$fileKey] ?? []
)
);

// 2. render additional formats
$originalWidth = $image->getProperty('width');
$originalHeight = $image->getProperty('height');
$targetWidth = $fileObj['properties']['dimensions']['width'];
$targetHeight = $fileObj['properties']['dimensions']['height'];
foreach ($formats ?? [] as $formatKey => $formatConf) {
$formatKey = rtrim($formatKey, '.');
$factor = (float)($formatConf['factor'] ?? 1.0);

$fileObj[$formatKey] = $this->getFileUtility()->processFile(
$image,
[
'width' => $fileObj['properties']['dimensions']['width'] * FileUtility::RETINA_RATIO,
'height' => $fileObj['properties']['dimensions']['height'] * FileUtility::RETINA_RATIO,
'fileExtension' => $formatConf['fileExtension'] ?? null,
// multiply width/height by factor,
// but don't stretch image beyond its original dimensions!
'width' => min($targetWidth * $factor, $originalWidth),
'height' => min($targetHeight * $factor, $originalHeight),
]
)['publicUrl'];
}

if (isset($this->processorConfiguration['autogenerate.']['lqip'],
$fileObj['properties']['dimensions']['width']) &&
(int)$this->processorConfiguration['autogenerate.']['lqip'] === 1) {
$fileObj['urlLqip'] = $this->getFileUtility()->processFile(
$image,
[
'width' => $fileObj['properties']['dimensions']['width'] * FileUtility::LQIP_RATIO,
'height' => $fileObj['properties']['dimensions']['height'] * FileUtility::LQIP_RATIO,
]
)['publicUrl'];
}
}

$this->galleryData['rows'][$row]['columns'][$column] = $fileObj;
Expand Down