Skip to content

Commit

Permalink
feat: add fileExt in process instruction (TYPO3-Headless#475)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kubilay Melnikov authored and Kubilay Melnikov committed Oct 21, 2022
1 parent 719182c commit 8d48954
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 27 deletions.
33 changes: 14 additions & 19 deletions Classes/DataProcessing/FilesProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,12 @@ public function process(
return $processedData;
}

$dimensions = [];
$properties = [];

if (isset($processorConfiguration['processingConfiguration.'])) {
$dimensions = [
'width' => $cObj->stdWrapValue('width', $processorConfiguration['processingConfiguration.'] ?? [], null),
'height' => $cObj->stdWrapValue('height', $processorConfiguration['processingConfiguration.'] ?? [], null),
'minWidth' => $cObj->stdWrapValue('minWidth', $processorConfiguration['processingConfiguration.'] ?? [], null),
'minHeight' => $cObj->stdWrapValue('minHeight', $processorConfiguration['processingConfiguration.'] ?? [], null),
'maxWidth' => $cObj->stdWrapValue('maxWidth', $processorConfiguration['processingConfiguration.'] ?? [], null),
'maxHeight' => $cObj->stdWrapValue('maxHeight', $processorConfiguration['processingConfiguration.'] ?? [], null),
];
foreach (array_keys((array)$processorConfiguration['processingConfiguration.']) as $key) {
$properties[$key] = $cObj->stdWrapValue($key, $processorConfiguration['processingConfiguration.'], null);
}
}

$this->contentObjectRenderer = $cObj;
Expand All @@ -98,7 +93,7 @@ public function process(
);

$this->fileObjects = $this->fetchData();
$processedData[$targetFieldName] = $this->processFiles($dimensions);
$processedData[$targetFieldName] = $this->processFiles($properties);

return $this->removeDataIfnotAppendInConfiguration($processorConfiguration, $processedData);
}
Expand Down Expand Up @@ -169,27 +164,27 @@ protected function fetchData(): array
}

/**
* @param array $dimensions
* @param array $properties
* @return array|null
*/
protected function processFiles(array $dimensions = []): ?array
protected function processFiles(array $properties = []): ?array
{
$data = [];
$cropVariant = $this->processorConfiguration['processingConfiguration.']['cropVariant'] ?? 'default';

foreach ($this->fileObjects as $key => $fileObject) {
if (isset($this->processorConfiguration['processingConfiguration.']['autogenerate.'])) {
$file = $this->getFileUtility()->processFile($fileObject, $dimensions, $cropVariant);
$targetWidth = (int)($dimensions['width'] ?: $file['properties']['dimensions']['width']);
$targetHeight = (int)($dimensions['height'] ?: $file['properties']['dimensions']['height']);
$file = $this->getFileUtility()->processFile($fileObject, $properties, $cropVariant);
$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(
$dimensions,
$properties,
[
'width' => $targetWidth * FileUtility::RETINA_RATIO,
'height' => $targetHeight * FileUtility::RETINA_RATIO,
Expand All @@ -205,7 +200,7 @@ protected function processFiles(array $dimensions = []): ?array
$file['urlLqip'] = $this->getFileUtility()->processFile(
$fileObject,
array_merge(
$dimensions,
$properties,
[
'width' => $targetWidth * FileUtility::LQIP_RATIO,
'height' => $targetHeight * FileUtility::LQIP_RATIO,
Expand All @@ -217,7 +212,7 @@ protected function processFiles(array $dimensions = []): ?array

$data[] = $file;
} else {
$data[$key] = $this->getFileUtility()->processFile($fileObject, $dimensions, $cropVariant);
$data[$key] = $this->getFileUtility()->processFile($fileObject, $properties, $cropVariant);

$crop = $fileObject->getProperty('crop');

Expand All @@ -226,7 +221,7 @@ protected function processFiles(array $dimensions = []): ?array

if (is_array($cropVariants) && count($cropVariants) > 1) {
foreach (array_keys($cropVariants) as $cropVariantName) {
$file = $this->getFileUtility()->processFile($fileObject, $dimensions, $cropVariantName);
$file = $this->getFileUtility()->processFile($fileObject, $properties, $cropVariantName);
$data[$key]['cropVariants'][$cropVariantName] = $file;
}
}
Expand Down
19 changes: 11 additions & 8 deletions Classes/Utility/FileUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,11 @@ public function processFile(FileInterface $fileReference, array $dimensions = []

/**
* @param FileInterface $fileReference
* @param array $dimensions
* @param array $arguments
* @param string $cropVariant
* @return ProcessedFile
*/
public function processImageFile(FileInterface $image, array $dimensions = [], string $cropVariant = 'default'): ProcessedFile
public function processImageFile(FileInterface $image, array $arguments = [], string $cropVariant = 'default'): ProcessedFile
{
try {
$properties = $image->getProperties();
Expand All @@ -164,14 +164,17 @@ public function processImageFile(FileInterface $image, array $dimensions = [], s
$cropVariant = $cropVariant ?: 'default';
$cropArea = $cropVariantCollection->getCropArea($cropVariant);
$processingInstructions = [
'width' => $dimensions['width'] ?? null,
'height' => $dimensions['height'] ?? null,
'minWidth' => $dimensions['minWidth'] ?? $properties['minWidth'] ?? 0,
'minHeight' => $dimensions['minHeight'] ?? $properties['minHeight'] ?? 0,
'maxWidth' => $dimensions['maxWidth'] ?? $properties['maxWidth'] ?? 0,
'maxHeight' => $dimensions['maxHeight'] ?? $properties['maxHeight'] ?? 0,
'width' => $arguments['width'] ?? null,
'height' => $arguments['height'] ?? null,
'minWidth' => $arguments['minWidth'] ?? $properties['minWidth'] ?? 0,
'minHeight' => $arguments['minHeight'] ?? $properties['minHeight'] ?? 0,
'maxWidth' => $arguments['maxWidth'] ?? $properties['maxWidth'] ?? 0,
'maxHeight' => $arguments['maxHeight'] ?? $properties['maxHeight'] ?? 0,
'crop' => $cropArea->isEmpty() ? null : $cropArea->makeAbsoluteBasedOnFile($image),
];
if (!empty($arguments['fileExtension'])) {
$processingInstructions['fileExtension'] = $arguments['fileExtension'];
}
return $this->imageService->applyProcessingInstructions($image, $processingInstructions);
} catch (\UnexpectedValueException|\RuntimeException|\InvalidArgumentException $e) {
$type = lcfirst(get_class($image));
Expand Down

0 comments on commit 8d48954

Please sign in to comment.