1<?php
2
3declare(strict_types=1);
4
5/*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18namespace TYPO3\CMS\Core\Resource\Processing;
19
20use TYPO3\CMS\Core\Imaging\ImageDimension;
21
22/**
23 * Processes (scales) SVG Images files, when no cropping is defined
24 */
25class SvgImageProcessor implements ProcessorInterface
26{
27    public function canProcessTask(TaskInterface $task): bool
28    {
29        return $task->getType() === 'Image'
30            && in_array($task->getName(), ['Preview', 'CropScaleMask'], true)
31            && empty($task->getConfiguration()['crop'])
32            && $task->getTargetFileExtension() === 'svg';
33    }
34
35    /**
36     * Processes the given task.
37     *
38     * @param TaskInterface $task
39     * @throws \InvalidArgumentException
40     */
41    public function processTask(TaskInterface $task): void
42    {
43        $task->setExecuted(true);
44        $task->getTargetFile()->setUsesOriginalFile();
45        try {
46            $imageDimension = ImageDimension::fromProcessingTask($task);
47        } catch (\Throwable $e) {
48            // To not fail image processing, we just assume an SVG image dimension here
49            $imageDimension = new ImageDimension(64, 64);
50        }
51        $task->getTargetFile()->updateProperties(
52            [
53                'width' => $imageDimension->getWidth(),
54                'height' => $imageDimension->getHeight(),
55                'size' => $task->getSourceFile()->getSize(),
56                'checksum' => $task->getConfigurationChecksum(),
57            ]
58        );
59    }
60}
61