1
0

SVG.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. $svg = new \Imagick();
  45. $svg->setBackgroundColor(new \ImagickPixel('transparent'));
  46. $content = stream_get_contents($file->fopen('r'));
  47. if (substr($content, 0, 5) !== '<?xml') {
  48. $content = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . $content;
  49. }
  50. // Do not parse SVG files with references
  51. if (stripos($content, 'xlink:href') !== false) {
  52. return null;
  53. }
  54. $svg->readImageBlob($content);
  55. $svg->setImageFormat('png32');
  56. } catch (\Exception $e) {
  57. \OC::$server->get(LoggerInterface::class)->error($e->getMessage(), [
  58. 'exception' => $e,
  59. 'app' => 'core',
  60. ]);
  61. return null;
  62. }
  63. //new image object
  64. $image = new \OCP\Image();
  65. $image->loadFromData((string) $svg);
  66. //check if image object is valid
  67. if ($image->valid()) {
  68. $image->scaleDownToFit($maxX, $maxY);
  69. return $image;
  70. }
  71. return null;
  72. }
  73. }