Bitmap.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  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 Imagick;
  27. use OCP\ILogger;
  28. /**
  29. * Creates a PNG preview using ImageMagick via the PECL extension
  30. *
  31. * @package OC\Preview
  32. */
  33. abstract class Bitmap extends Provider {
  34. /**
  35. * {@inheritDoc}
  36. */
  37. public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) {
  38. $tmpPath = $fileview->toTmpFile($path);
  39. if (!$tmpPath) {
  40. return false;
  41. }
  42. // Creates \Imagick object from bitmap or vector file
  43. try {
  44. $bp = $this->getResizedPreview($tmpPath, $maxX, $maxY);
  45. } catch (\Exception $e) {
  46. \OC::$server->getLogger()->logException($e, [
  47. 'message' => 'File: ' . $fileview->getAbsolutePath($path) . ' Imagick says:',
  48. 'level' => ILogger::ERROR,
  49. 'app' => 'core',
  50. ]);
  51. return false;
  52. }
  53. unlink($tmpPath);
  54. //new bitmap image object
  55. $image = new \OC_Image();
  56. $image->loadFromData($bp);
  57. //check if image object is valid
  58. return $image->valid() ? $image : false;
  59. }
  60. /**
  61. * Returns a preview of maxX times maxY dimensions in PNG format
  62. *
  63. * * The default resolution is already 72dpi, no need to change it for a bitmap output
  64. * * It's possible to have proper colour conversion using profileimage().
  65. * ICC profiles are here: http://www.color.org/srgbprofiles.xalter
  66. * * It's possible to Gamma-correct an image via gammaImage()
  67. *
  68. * @param string $tmpPath the location of the file to convert
  69. * @param int $maxX
  70. * @param int $maxY
  71. *
  72. * @return \Imagick
  73. */
  74. private function getResizedPreview($tmpPath, $maxX, $maxY) {
  75. $bp = new Imagick();
  76. // Layer 0 contains either the bitmap or a flat representation of all vector layers
  77. $bp->readImage($tmpPath . '[0]');
  78. $bp = $this->resize($bp, $maxX, $maxY);
  79. $bp->setImageFormat('png');
  80. return $bp;
  81. }
  82. /**
  83. * Returns a resized \Imagick object
  84. *
  85. * If you want to know more on the various methods available to resize an
  86. * image, check out this link : @link https://stackoverflow.com/questions/8517304/what-the-difference-of-sample-resample-scale-resize-adaptive-resize-thumbnail-im
  87. *
  88. * @param \Imagick $bp
  89. * @param int $maxX
  90. * @param int $maxY
  91. *
  92. * @return \Imagick
  93. */
  94. private function resize($bp, $maxX, $maxY) {
  95. list($previewWidth, $previewHeight) = array_values($bp->getImageGeometry());
  96. // We only need to resize a preview which doesn't fit in the maximum dimensions
  97. if ($previewWidth > $maxX || $previewHeight > $maxY) {
  98. // TODO: LANCZOS is the default filter, CATROM could bring similar results faster
  99. $bp->resizeImage($maxX, $maxY, imagick::FILTER_LANCZOS, 1, true);
  100. }
  101. return $bp;
  102. }
  103. }