HEIC.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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\/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. private function getResizedPreview($tmpPath, $maxX, $maxY) {
  102. $bp = new \Imagick();
  103. // Layer 0 contains either the bitmap or a flat representation of all vector layers
  104. $bp->readImage($tmpPath . '[0]');
  105. // Fix orientation from EXIF
  106. $bp->autoOrient();
  107. $bp->setImageFormat('jpg');
  108. $bp = $this->resize($bp, $maxX, $maxY);
  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. // If we want a small image (thumbnail) let's be most space- and time-efficient
  128. if ($maxX <= 500 && $maxY <= 500) {
  129. $bp->thumbnailImage($maxY, $maxX, true);
  130. $bp->stripImage();
  131. } else {
  132. // A bigger image calls for some better resizing algorithm
  133. // According to http://www.imagemagick.org/Usage/filter/#lanczos
  134. // the catrom filter is almost identical to Lanczos2, but according
  135. // to https://www.php.net/manual/en/imagick.resizeimage.php it is
  136. // significantly faster
  137. $bp->resizeImage($maxX, $maxY, \Imagick::FILTER_CATROM, 1, true);
  138. }
  139. }
  140. return $bp;
  141. }
  142. }