HEIC.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2018 ownCloud GmbH
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace OC\Preview;
  9. use OCP\Files\File;
  10. use OCP\Files\FileInfo;
  11. use OCP\IImage;
  12. use OCP\Server;
  13. use Psr\Log\LoggerInterface;
  14. /**
  15. * Creates a JPG preview using ImageMagick via the PECL extension
  16. *
  17. * @package OC\Preview
  18. */
  19. class HEIC extends ProviderV2 {
  20. /**
  21. * {@inheritDoc}
  22. */
  23. public function getMimeType(): string {
  24. return '/image\/(x-)?hei(f|c)/';
  25. }
  26. /**
  27. * {@inheritDoc}
  28. */
  29. public function isAvailable(FileInfo $file): bool {
  30. return in_array('HEIC', \Imagick::queryFormats('HEI*'));
  31. }
  32. /**
  33. * {@inheritDoc}
  34. */
  35. public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
  36. if (!$this->isAvailable($file)) {
  37. return null;
  38. }
  39. $tmpPath = $this->getLocalFile($file);
  40. if ($tmpPath === false) {
  41. Server::get(LoggerInterface::class)->error(
  42. 'Failed to get local file to generate thumbnail for: ' . $file->getPath(),
  43. ['app' => 'core']
  44. );
  45. return null;
  46. }
  47. // Creates \Imagick object from the heic file
  48. try {
  49. $bp = $this->getResizedPreview($tmpPath, $maxX, $maxY);
  50. $bp->setFormat('jpg');
  51. } catch (\Exception $e) {
  52. \OC::$server->get(LoggerInterface::class)->error(
  53. 'File: ' . $file->getPath() . ' Imagick says:',
  54. [
  55. 'exception' => $e,
  56. 'app' => 'core',
  57. ]
  58. );
  59. return null;
  60. }
  61. $this->cleanTmpFiles();
  62. //new bitmap image object
  63. $image = new \OCP\Image();
  64. $image->loadFromData((string)$bp);
  65. //check if image object is valid
  66. return $image->valid() ? $image : null;
  67. }
  68. /**
  69. * Returns a preview of maxX times maxY dimensions in JPG format
  70. *
  71. * * The default resolution is already 72dpi, no need to change it for a bitmap output
  72. * * It's possible to have proper colour conversion using profileimage().
  73. * ICC profiles are here: http://www.color.org/srgbprofiles.xalter
  74. * * It's possible to Gamma-correct an image via gammaImage()
  75. *
  76. * @param string $tmpPath the location of the file to convert
  77. * @param int $maxX
  78. * @param int $maxY
  79. *
  80. * @return \Imagick
  81. *
  82. * @throws \Exception
  83. */
  84. private function getResizedPreview($tmpPath, $maxX, $maxY) {
  85. $bp = new \Imagick();
  86. // Some HEIC files just contain (or at least are identified as) other formats
  87. // like JPEG. We just need to check if the image is safe to process.
  88. $bp->pingImage($tmpPath . '[0]');
  89. $mimeType = $bp->getImageMimeType();
  90. if (!preg_match('/^image\/(x-)?(png|jpeg|gif|bmp|tiff|webp|hei(f|c)|avif)$/', $mimeType)) {
  91. throw new \Exception('File mime type does not match the preview provider: ' . $mimeType);
  92. }
  93. // Layer 0 contains either the bitmap or a flat representation of all vector layers
  94. $bp->readImage($tmpPath . '[0]');
  95. // Fix orientation from EXIF
  96. $bp->autoOrient();
  97. $bp->setImageFormat('jpg');
  98. $bp = $this->resize($bp, $maxX, $maxY);
  99. return $bp;
  100. }
  101. /**
  102. * Returns a resized \Imagick object
  103. *
  104. * If you want to know more on the various methods available to resize an
  105. * image, check out this link : @link https://stackoverflow.com/questions/8517304/what-the-difference-of-sample-resample-scale-resize-adaptive-resize-thumbnail-im
  106. *
  107. * @param \Imagick $bp
  108. * @param int $maxX
  109. * @param int $maxY
  110. *
  111. * @return \Imagick
  112. */
  113. private function resize($bp, $maxX, $maxY) {
  114. [$previewWidth, $previewHeight] = array_values($bp->getImageGeometry());
  115. // We only need to resize a preview which doesn't fit in the maximum dimensions
  116. if ($previewWidth > $maxX || $previewHeight > $maxY) {
  117. // If we want a small image (thumbnail) let's be most space- and time-efficient
  118. if ($maxX <= 500 && $maxY <= 500) {
  119. $bp->thumbnailImage($maxY, $maxX, true);
  120. $bp->stripImage();
  121. } else {
  122. // A bigger image calls for some better resizing algorithm
  123. // According to http://www.imagemagick.org/Usage/filter/#lanczos
  124. // the catrom filter is almost identical to Lanczos2, but according
  125. // to https://www.php.net/manual/en/imagick.resizeimage.php it is
  126. // significantly faster
  127. $bp->resizeImage($maxX, $maxY, \Imagick::FILTER_CATROM, 1, true);
  128. }
  129. }
  130. return $bp;
  131. }
  132. }