2013-05-21 06:23:31 -04:00
|
|
|
<?php
|
2024-05-23 03:26:56 -04:00
|
|
|
|
2013-05-21 06:23:31 -04:00
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2013-05-21 06:23:31 -04:00
|
|
|
*/
|
2013-05-29 06:33:24 -04:00
|
|
|
namespace OC\Preview;
|
|
|
|
|
|
2019-11-22 14:52:10 -05:00
|
|
|
use OCP\Files\File;
|
2019-06-04 09:25:25 -04:00
|
|
|
use OCP\IImage;
|
2022-03-30 04:55:41 -04:00
|
|
|
use Psr\Log\LoggerInterface;
|
2018-04-25 09:22:28 -04:00
|
|
|
|
2019-06-04 09:25:25 -04:00
|
|
|
class SVG extends ProviderV2 {
|
2014-11-28 03:16:35 -05:00
|
|
|
/**
|
|
|
|
|
* {@inheritDoc}
|
|
|
|
|
*/
|
2019-06-04 09:25:25 -04:00
|
|
|
public function getMimeType(): string {
|
2014-11-28 03:16:35 -05:00
|
|
|
return '/image\/svg\+xml/';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* {@inheritDoc}
|
|
|
|
|
*/
|
2019-06-04 09:25:25 -04:00
|
|
|
public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
|
2015-06-06 10:21:36 -04:00
|
|
|
try {
|
2019-06-04 09:25:25 -04:00
|
|
|
$content = stream_get_contents($file->fopen('r'));
|
2015-06-06 10:21:36 -04:00
|
|
|
if (substr($content, 0, 5) !== '<?xml') {
|
2014-11-28 03:16:35 -05:00
|
|
|
$content = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . $content;
|
2013-05-29 06:33:24 -04:00
|
|
|
}
|
|
|
|
|
|
2014-11-28 03:16:35 -05:00
|
|
|
// Do not parse SVG files with references
|
2024-05-15 17:38:24 -04:00
|
|
|
if (preg_match('/["\s](xlink:)?href\s*=/i', $content)) {
|
2019-06-04 09:25:25 -04:00
|
|
|
return null;
|
2014-03-05 09:53:12 -05:00
|
|
|
}
|
2014-11-28 03:16:35 -05:00
|
|
|
|
2024-04-07 22:10:19 -04:00
|
|
|
$svg = new \Imagick();
|
|
|
|
|
|
|
|
|
|
$svg->pingImageBlob($content);
|
|
|
|
|
$mimeType = $svg->getImageMimeType();
|
|
|
|
|
if (!preg_match($this->getMimeType(), $mimeType)) {
|
|
|
|
|
throw new \Exception('File mime type does not match the preview provider: ' . $mimeType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$svg->setBackgroundColor(new \ImagickPixel('transparent'));
|
2014-11-28 03:16:35 -05:00
|
|
|
$svg->readImageBlob($content);
|
|
|
|
|
$svg->setImageFormat('png32');
|
|
|
|
|
} catch (\Exception $e) {
|
2024-04-07 22:10:19 -04:00
|
|
|
\OC::$server->get(LoggerInterface::class)->error(
|
|
|
|
|
'File: ' . $file->getPath() . ' Imagick says:',
|
|
|
|
|
[
|
|
|
|
|
'exception' => $e,
|
|
|
|
|
'app' => 'core',
|
|
|
|
|
]
|
|
|
|
|
);
|
2019-06-04 09:25:25 -04:00
|
|
|
return null;
|
2014-03-05 09:53:12 -05:00
|
|
|
}
|
2014-11-28 03:16:35 -05:00
|
|
|
|
|
|
|
|
//new image object
|
2022-06-01 20:37:36 -04:00
|
|
|
$image = new \OCP\Image();
|
2021-10-25 10:13:50 -04:00
|
|
|
$image->loadFromData((string)$svg);
|
2014-11-28 03:16:35 -05:00
|
|
|
//check if image object is valid
|
2015-06-06 10:21:36 -04:00
|
|
|
if ($image->valid()) {
|
|
|
|
|
$image->scaleDownToFit($maxX, $maxY);
|
|
|
|
|
|
|
|
|
|
return $image;
|
|
|
|
|
}
|
2019-06-04 09:25:25 -04:00
|
|
|
return null;
|
2014-11-28 03:16:35 -05:00
|
|
|
}
|
|
|
|
|
}
|