HEIC.php 5.0 KB

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