Skip to content

Commit

Permalink
[BUGFIX] Add option to process SVG files (#783)
Browse files Browse the repository at this point in the history
Resolves: #782
  • Loading branch information
twoldanski authored Oct 30, 2024
1 parent 21cdcd4 commit 291ebb0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
16 changes: 11 additions & 5 deletions Classes/DataProcessing/FilesProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,13 @@ protected function processFiles(array $properties = []): ?array
$data = [];
$cropVariant = $this->processorConfiguration['processingConfiguration.']['cropVariant'] ?? 'default';

$fileUtility = $this->getFileUtility();

$fileUtility->setAllowSvgProcessing((int)($this->processorConfiguration['processingConfiguration.']['processSvg'] ?? 0) === 1);

foreach ($this->fileObjects as $key => $fileObject) {
if (isset($this->processorConfiguration['processingConfiguration.']['autogenerate.'])) {
$file = $this->getFileUtility()->processFile(
$file = $fileUtility->processFile(
$fileObject,
$properties,
$cropVariant,
Expand All @@ -191,7 +195,7 @@ protected function processFiles(array $properties = []): ?array
if (isset($this->processorConfiguration['processingConfiguration.']['autogenerate.']['retina2x']) &&
(int)$this->processorConfiguration['processingConfiguration.']['autogenerate.']['retina2x'] === 1 &&
($targetWidth || $targetHeight)) {
$file['urlRetina'] = $this->getFileUtility()->processFile(
$file['urlRetina'] = $fileUtility->processFile(
$fileObject,
array_merge(
$properties,
Expand All @@ -208,7 +212,7 @@ protected function processFiles(array $properties = []): ?array
if (isset($this->processorConfiguration['processingConfiguration.']['autogenerate.']['lqip']) &&
(int)$this->processorConfiguration['processingConfiguration.']['autogenerate.']['lqip'] === 1 &&
($targetWidth || $targetHeight)) {
$file['urlLqip'] = $this->getFileUtility()->processFile(
$file['urlLqip'] = $fileUtility->processFile(
$fileObject,
array_merge(
$properties,
Expand All @@ -224,7 +228,7 @@ protected function processFiles(array $properties = []): ?array

$data[] = $file;
} else {
$data[$key] = $this->getFileUtility()->processFile(
$data[$key] = $fileUtility->processFile(
$fileObject,
$properties,
$cropVariant,
Expand All @@ -238,14 +242,16 @@ protected function processFiles(array $properties = []): ?array

if (is_array($cropVariants) && count($cropVariants) > 1) {
foreach (array_keys($cropVariants) as $cropVariantName) {
$file = $this->getFileUtility()->processFile($fileObject, $properties, $cropVariantName);
$file = $fileUtility->processFile($fileObject, $properties, $cropVariantName);
$data[$key]['cropVariants'][$cropVariantName] = $file;
}
}
}
}
}

$fileUtility->setAllowSvgProcessing(false);

if (isset($this->processorConfiguration['processingConfiguration.']['returnFlattenObject']) &&
(int)$this->processorConfiguration['processingConfiguration.']['returnFlattenObject'] === 1) {
return $data[0] ?? null;
Expand Down
12 changes: 9 additions & 3 deletions Classes/DataProcessing/GalleryProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ private function getCroppedDimensionalPropertyFromProcessedFile(array $processed
*/
protected function prepareGalleryData()
{
$fileUtility = $this->getFileUtility();

$fileUtility->setAllowSvgProcessing((int)($this->processorConfiguration['processingConfiguration.']['processSvg'] ?? 0) === 1);

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,12 +190,12 @@ 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] ?? []);
$fileObj = $fileUtility->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(
$fileObj['urlRetina'] = $fileUtility->processFile(
$image,
[
'width' => $fileObj['properties']['dimensions']['width'] * FileUtility::RETINA_RATIO,
Expand All @@ -203,7 +207,7 @@ protected function prepareGalleryData()
if (isset($this->processorConfiguration['autogenerate.']['lqip'],
$fileObj['properties']['dimensions']['width']) &&
(int)$this->processorConfiguration['autogenerate.']['lqip'] === 1) {
$fileObj['urlLqip'] = $this->getFileUtility()->processFile(
$fileObj['urlLqip'] = $fileUtility->processFile(
$image,
[
'width' => $fileObj['properties']['dimensions']['width'] * FileUtility::LQIP_RATIO,
Expand All @@ -218,6 +222,8 @@ protected function prepareGalleryData()
}
}

$fileUtility->setAllowSvgProcessing(false);

$this->galleryData['columnSpacing'] = $this->columnSpacing;
$this->galleryData['border']['enabled'] = $this->borderEnabled;
$this->galleryData['border']['width'] = $this->borderWidth;
Expand Down
8 changes: 7 additions & 1 deletion Classes/Utility/FileUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class FileUtility
*/
protected array $errors = [];
protected Features $features;
protected bool $allowSvgProcessing = false;

public function __construct(
?ContentObjectRenderer $contentObjectRenderer = null,
Expand All @@ -61,6 +62,11 @@ public function __construct(
$this->features = $features ?? GeneralUtility::makeInstance(Features::class);
}

public function setAllowSvgProcessing(bool $allowSvgProcessing): void
{
$this->allowSvgProcessing = $allowSvgProcessing;
}

/**
* @param array<string,mixed> $dimensions
* @return array<string, mixed>
Expand Down Expand Up @@ -94,7 +100,7 @@ public function processFile(
];

if ($fileRenderer === null && $fileReference->getType() === AbstractFile::FILETYPE_IMAGE) {
if (!$delayProcessing && $fileReference->getMimeType() !== 'image/svg+xml') {
if (!$delayProcessing && ($this->allowSvgProcessing || $fileReference->getMimeType() !== 'image/svg+xml')) {
$fileReference = $this->processImageFile($fileReference, $dimensions, $cropVariant);
}
$publicUrl = $this->imageService->getImageUri($fileReference, true);
Expand Down

0 comments on commit 291ebb0

Please sign in to comment.