bitmap.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * @author Joas Schilling <nickvergessen@owncloud.com>
  4. * @author Morris Jobke <hey@morrisjobke.de>
  5. * @author Olivier Paroz <owncloud@interfasys.ch>
  6. *
  7. * @copyright Copyright (c) 2015, ownCloud, Inc.
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC\Preview;
  24. use Imagick;
  25. /**
  26. * Creates a PNG preview using ImageMagick via the PECL extension
  27. *
  28. * @package OC\Preview
  29. */
  30. abstract class Bitmap extends Provider {
  31. /**
  32. * {@inheritDoc}
  33. */
  34. public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) {
  35. $tmpPath = $fileview->toTmpFile($path);
  36. if (!$tmpPath) {
  37. return false;
  38. }
  39. // Creates \Imagick object from bitmap or vector file
  40. try {
  41. $bp = $this->getResizedPreview($tmpPath, $maxX, $maxY);
  42. } catch (\Exception $e) {
  43. \OC_Log::write('core', 'ImageMagick says: ' . $e->getmessage(), \OC_Log::ERROR);
  44. return false;
  45. }
  46. unlink($tmpPath);
  47. //new bitmap image object
  48. $image = new \OC_Image();
  49. $image->loadFromData($bp);
  50. //check if image object is valid
  51. return $image->valid() ? $image : false;
  52. }
  53. /**
  54. * Returns a preview of maxX times maxY dimensions in PNG format
  55. *
  56. * * The default resolution is already 72dpi, no need to change it for a bitmap output
  57. * * It's possible to have proper colour conversion using profileimage().
  58. * ICC profiles are here: http://www.color.org/srgbprofiles.xalter
  59. * * It's possible to Gamma-correct an image via gammaImage()
  60. *
  61. * @param string $tmpPath the location of the file to convert
  62. * @param int $maxX
  63. * @param int $maxY
  64. *
  65. * @return \Imagick
  66. */
  67. private function getResizedPreview($tmpPath, $maxX, $maxY) {
  68. $bp = new Imagick();
  69. // Layer 0 contains either the bitmap or a flat representation of all vector layers
  70. $bp->readImage($tmpPath . '[0]');
  71. $bp = $this->resize($bp, $maxX, $maxY);
  72. $bp->setImageFormat('png');
  73. return $bp;
  74. }
  75. /**
  76. * Returns a resized \Imagick object
  77. *
  78. * If you want to know more on the various methods available to resize an
  79. * image, check out this link : @link https://stackoverflow.com/questions/8517304/what-the-difference-of-sample-resample-scale-resize-adaptive-resize-thumbnail-im
  80. *
  81. * @param \Imagick $bp
  82. * @param int $maxX
  83. * @param int $maxY
  84. *
  85. * @return \Imagick
  86. */
  87. private function resize($bp, $maxX, $maxY) {
  88. list($previewWidth, $previewHeight) = array_values($bp->getImageGeometry());
  89. // We only need to resize a preview which doesn't fit in the maximum dimensions
  90. if ($previewWidth > $maxX || $previewHeight > $maxY) {
  91. // TODO: LANCZOS is the default filter, CATROM could bring similar results faster
  92. $bp->resizeImage($maxX, $maxY, imagick::FILTER_LANCZOS, 1, true);
  93. }
  94. return $bp;
  95. }
  96. }