Bitmap.php 3.3 KB

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