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.2KB

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