1
0

HEIC.php 4.1 KB

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