Bitmap.php 3.7 KB

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