SVG.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Georg Ehrke <oc.list@georgehrke.com>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Olivier Paroz <github@oparoz.com>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\Preview;
  26. use OCP\ILogger;
  27. class SVG extends Provider {
  28. /**
  29. * {@inheritDoc}
  30. */
  31. public function getMimeType() {
  32. return '/image\/svg\+xml/';
  33. }
  34. /**
  35. * {@inheritDoc}
  36. */
  37. public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) {
  38. try {
  39. $svg = new \Imagick();
  40. $svg->setBackgroundColor(new \ImagickPixel('transparent'));
  41. $content = stream_get_contents($fileview->fopen($path, 'r'));
  42. if (substr($content, 0, 5) !== '<?xml') {
  43. $content = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . $content;
  44. }
  45. // Do not parse SVG files with references
  46. if (stripos($content, 'xlink:href') !== false) {
  47. return false;
  48. }
  49. $svg->readImageBlob($content);
  50. $svg->setImageFormat('png32');
  51. } catch (\Exception $e) {
  52. \OC::$server->getLogger()->logException($e, [
  53. 'level' => ILogger::ERROR,
  54. 'app' => 'core',
  55. ]);
  56. return false;
  57. }
  58. //new image object
  59. $image = new \OC_Image();
  60. $image->loadFromData($svg);
  61. //check if image object is valid
  62. if ($image->valid()) {
  63. $image->scaleDownToFit($maxX, $maxY);
  64. return $image;
  65. }
  66. return false;
  67. }
  68. }