1
0

Bitmap.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. * List of MIME types that this preview provider is allowed to process.
  40. *
  41. * These should correspond to the MIME types *identified* by Imagemagick
  42. * for files to be processed by this provider. These do / will not
  43. * necessarily need to match the MIME types stored in the database
  44. * (which are identified by IMimeTypeDetector).
  45. *
  46. * @return string Regular expression
  47. */
  48. abstract protected function getAllowedMimeTypes(): string;
  49. /**
  50. * {@inheritDoc}
  51. */
  52. public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
  53. $tmpPath = $this->getLocalFile($file);
  54. if ($tmpPath === false) {
  55. \OC::$server->get(LoggerInterface::class)->error(
  56. 'Failed to get thumbnail for: ' . $file->getPath(),
  57. ['app' => 'core']
  58. );
  59. return null;
  60. }
  61. // Creates \Imagick object from bitmap or vector file
  62. try {
  63. $bp = $this->getResizedPreview($tmpPath, $maxX, $maxY);
  64. } catch (\Exception $e) {
  65. \OC::$server->get(LoggerInterface::class)->info(
  66. 'File: ' . $file->getPath() . ' Imagick says:',
  67. [
  68. 'exception' => $e,
  69. 'app' => 'core',
  70. ]
  71. );
  72. return null;
  73. }
  74. $this->cleanTmpFiles();
  75. //new bitmap image object
  76. $image = new \OCP\Image();
  77. $image->loadFromData((string) $bp);
  78. //check if image object is valid
  79. return $image->valid() ? $image : null;
  80. }
  81. /**
  82. * Returns a preview of maxX times maxY dimensions in PNG format
  83. *
  84. * * The default resolution is already 72dpi, no need to change it for a bitmap output
  85. * * It's possible to have proper colour conversion using profileimage().
  86. * ICC profiles are here: http://www.color.org/srgbprofiles.xalter
  87. * * It's possible to Gamma-correct an image via gammaImage()
  88. *
  89. * @param string $tmpPath the location of the file to convert
  90. * @param int $maxX
  91. * @param int $maxY
  92. *
  93. * @return \Imagick
  94. *
  95. * @throws \Exception
  96. */
  97. private function getResizedPreview($tmpPath, $maxX, $maxY) {
  98. $bp = new Imagick();
  99. // Validate mime type
  100. $bp->pingImage($tmpPath . '[0]');
  101. $mimeType = $bp->getImageMimeType();
  102. if (!preg_match($this->getAllowedMimeTypes(), $mimeType)) {
  103. throw new \Exception('File mime type does not match the preview provider: ' . $mimeType);
  104. }
  105. // Layer 0 contains either the bitmap or a flat representation of all vector layers
  106. $bp->readImage($tmpPath . '[0]');
  107. $bp = $this->resize($bp, $maxX, $maxY);
  108. $bp->setImageFormat('png');
  109. return $bp;
  110. }
  111. /**
  112. * Returns a resized \Imagick object
  113. *
  114. * If you want to know more on the various methods available to resize an
  115. * image, check out this link : @link https://stackoverflow.com/questions/8517304/what-the-difference-of-sample-resample-scale-resize-adaptive-resize-thumbnail-im
  116. *
  117. * @param \Imagick $bp
  118. * @param int $maxX
  119. * @param int $maxY
  120. *
  121. * @return \Imagick
  122. */
  123. private function resize($bp, $maxX, $maxY) {
  124. [$previewWidth, $previewHeight] = array_values($bp->getImageGeometry());
  125. // We only need to resize a preview which doesn't fit in the maximum dimensions
  126. if ($previewWidth > $maxX || $previewHeight > $maxY) {
  127. // TODO: LANCZOS is the default filter, CATROM could bring similar results faster
  128. $bp->resizeImage($maxX, $maxY, imagick::FILTER_LANCZOS, 1, true);
  129. }
  130. return $bp;
  131. }
  132. }