Bitmap.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Preview;
  8. use Imagick;
  9. use OCP\Files\File;
  10. use OCP\IImage;
  11. use Psr\Log\LoggerInterface;
  12. /**
  13. * Creates a PNG preview using ImageMagick via the PECL extension
  14. *
  15. * @package OC\Preview
  16. */
  17. abstract class Bitmap extends ProviderV2 {
  18. /**
  19. * List of MIME types that this preview provider is allowed to process.
  20. *
  21. * These should correspond to the MIME types *identified* by Imagemagick
  22. * for files to be processed by this provider. These do / will not
  23. * necessarily need to match the MIME types stored in the database
  24. * (which are identified by IMimeTypeDetector).
  25. *
  26. * @return string Regular expression
  27. */
  28. abstract protected function getAllowedMimeTypes(): string;
  29. /**
  30. * {@inheritDoc}
  31. */
  32. public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
  33. $tmpPath = $this->getLocalFile($file);
  34. if ($tmpPath === false) {
  35. \OC::$server->get(LoggerInterface::class)->error(
  36. 'Failed to get thumbnail for: ' . $file->getPath(),
  37. ['app' => 'core']
  38. );
  39. return null;
  40. }
  41. // Creates \Imagick object from bitmap or vector file
  42. try {
  43. $bp = $this->getResizedPreview($tmpPath, $maxX, $maxY);
  44. } catch (\Exception $e) {
  45. \OC::$server->get(LoggerInterface::class)->info(
  46. 'File: ' . $file->getPath() . ' Imagick says:',
  47. [
  48. 'exception' => $e,
  49. 'app' => 'core',
  50. ]
  51. );
  52. return null;
  53. }
  54. $this->cleanTmpFiles();
  55. //new bitmap image object
  56. $image = new \OCP\Image();
  57. $image->loadFromData((string) $bp);
  58. //check if image object is valid
  59. return $image->valid() ? $image : null;
  60. }
  61. /**
  62. * Returns a preview of maxX times maxY dimensions in PNG format
  63. *
  64. * * The default resolution is already 72dpi, no need to change it for a bitmap output
  65. * * It's possible to have proper colour conversion using profileimage().
  66. * ICC profiles are here: http://www.color.org/srgbprofiles.xalter
  67. * * It's possible to Gamma-correct an image via gammaImage()
  68. *
  69. * @param string $tmpPath the location of the file to convert
  70. * @param int $maxX
  71. * @param int $maxY
  72. *
  73. * @return \Imagick
  74. *
  75. * @throws \Exception
  76. */
  77. private function getResizedPreview($tmpPath, $maxX, $maxY) {
  78. $bp = new Imagick();
  79. // Validate mime type
  80. $bp->pingImage($tmpPath . '[0]');
  81. $mimeType = $bp->getImageMimeType();
  82. if (!preg_match($this->getAllowedMimeTypes(), $mimeType)) {
  83. throw new \Exception('File mime type does not match the preview provider: ' . $mimeType);
  84. }
  85. // Layer 0 contains either the bitmap or a flat representation of all vector layers
  86. $bp->readImage($tmpPath . '[0]');
  87. $bp = $this->resize($bp, $maxX, $maxY);
  88. $bp->setImageFormat('png');
  89. return $bp;
  90. }
  91. /**
  92. * Returns a resized \Imagick object
  93. *
  94. * If you want to know more on the various methods available to resize an
  95. * image, check out this link : @link https://stackoverflow.com/questions/8517304/what-the-difference-of-sample-resample-scale-resize-adaptive-resize-thumbnail-im
  96. *
  97. * @param \Imagick $bp
  98. * @param int $maxX
  99. * @param int $maxY
  100. *
  101. * @return \Imagick
  102. */
  103. private function resize($bp, $maxX, $maxY) {
  104. [$previewWidth, $previewHeight] = array_values($bp->getImageGeometry());
  105. // We only need to resize a preview which doesn't fit in the maximum dimensions
  106. if ($previewWidth > $maxX || $previewHeight > $maxY) {
  107. // TODO: LANCZOS is the default filter, CATROM could bring similar results faster
  108. $bp->resizeImage($maxX, $maxY, imagick::FILTER_LANCZOS, 1, true);
  109. }
  110. return $bp;
  111. }
  112. }