SVG.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Olivier Paroz <github@oparoz.com>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OC\Preview;
  29. use OCP\Files\File;
  30. use OCP\IImage;
  31. use Psr\Log\LoggerInterface;
  32. class SVG extends ProviderV2 {
  33. /**
  34. * {@inheritDoc}
  35. */
  36. public function getMimeType(): string {
  37. return '/image\/svg\+xml/';
  38. }
  39. /**
  40. * {@inheritDoc}
  41. */
  42. public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
  43. try {
  44. $content = stream_get_contents($file->fopen('r'));
  45. if (substr($content, 0, 5) !== '<?xml') {
  46. $content = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . $content;
  47. }
  48. // Do not parse SVG files with references
  49. if (preg_match('/["\s](xlink:)?href\s*=/i', $content)) {
  50. return null;
  51. }
  52. $svg = new \Imagick();
  53. $svg->pingImageBlob($content);
  54. $mimeType = $svg->getImageMimeType();
  55. if (!preg_match($this->getMimeType(), $mimeType)) {
  56. throw new \Exception('File mime type does not match the preview provider: ' . $mimeType);
  57. }
  58. $svg->setBackgroundColor(new \ImagickPixel('transparent'));
  59. $svg->readImageBlob($content);
  60. $svg->setImageFormat('png32');
  61. } catch (\Exception $e) {
  62. \OC::$server->get(LoggerInterface::class)->error(
  63. 'File: ' . $file->getPath() . ' Imagick says:',
  64. [
  65. 'exception' => $e,
  66. 'app' => 'core',
  67. ]
  68. );
  69. return null;
  70. }
  71. //new image object
  72. $image = new \OCP\Image();
  73. $image->loadFromData((string) $svg);
  74. //check if image object is valid
  75. if ($image->valid()) {
  76. $image->scaleDownToFit($maxX, $maxY);
  77. return $image;
  78. }
  79. return null;
  80. }
  81. }