SVG.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Georg Ehrke <georg@owncloud.com>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Olivier Paroz <github@oparoz.com>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  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. class SVG extends Provider {
  27. /**
  28. * {@inheritDoc}
  29. */
  30. public function getMimeType() {
  31. return '/image\/svg\+xml/';
  32. }
  33. /**
  34. * {@inheritDoc}
  35. */
  36. public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) {
  37. try {
  38. $svg = new \Imagick();
  39. $svg->setBackgroundColor(new \ImagickPixel('transparent'));
  40. $content = stream_get_contents($fileview->fopen($path, 'r'));
  41. if (substr($content, 0, 5) !== '<?xml') {
  42. $content = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . $content;
  43. }
  44. // Do not parse SVG files with references
  45. if (stripos($content, 'xlink:href') !== false) {
  46. return false;
  47. }
  48. $svg->readImageBlob($content);
  49. $svg->setImageFormat('png32');
  50. } catch (\Exception $e) {
  51. \OCP\Util::writeLog('core', $e->getmessage(), \OCP\Util::ERROR);
  52. return false;
  53. }
  54. //new image object
  55. $image = new \OC_Image();
  56. $image->loadFromData($svg);
  57. //check if image object is valid
  58. if ($image->valid()) {
  59. $image->scaleDownToFit($maxX, $maxY);
  60. return $image;
  61. }
  62. return false;
  63. }
  64. }