You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HEIC.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 OCP\ILogger;
  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. // Creates \Imagick object from the heic file
  60. try {
  61. $bp = $this->getResizedPreview($tmpPath, $maxX, $maxY);
  62. $bp->setFormat('jpg');
  63. } catch (\Exception $e) {
  64. \OC::$server->getLogger()->logException($e, [
  65. 'message' => 'File: ' . $file->getPath() . ' Imagick says:',
  66. 'level' => ILogger::ERROR,
  67. 'app' => 'core',
  68. ]);
  69. return null;
  70. }
  71. $this->cleanTmpFiles();
  72. //new bitmap image object
  73. $image = new \OC_Image();
  74. $image->loadFromData((string) $bp);
  75. //check if image object is valid
  76. return $image->valid() ? $image : null;
  77. }
  78. /**
  79. * Returns a preview of maxX times maxY dimensions in JPG format
  80. *
  81. * * The default resolution is already 72dpi, no need to change it for a bitmap output
  82. * * It's possible to have proper colour conversion using profileimage().
  83. * ICC profiles are here: http://www.color.org/srgbprofiles.xalter
  84. * * It's possible to Gamma-correct an image via gammaImage()
  85. *
  86. * @param string $tmpPath the location of the file to convert
  87. * @param int $maxX
  88. * @param int $maxY
  89. *
  90. * @return \Imagick
  91. */
  92. private function getResizedPreview($tmpPath, $maxX, $maxY) {
  93. $bp = new \Imagick();
  94. // Layer 0 contains either the bitmap or a flat representation of all vector layers
  95. $bp->readImage($tmpPath . '[0]');
  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. }