HEIC.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @author Thomas Müller <thomas.mueller@tmit.eu>
  5. *
  6. * @copyright Copyright (c) 2018, ownCloud GmbH
  7. * @copyright Copyright (c) 2018, Sebastian Steinmetz (me@sebastiansteinmetz.ch)
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC\Preview;
  24. use OCP\ILogger;
  25. /**
  26. * Creates a JPG preview using ImageMagick via the PECL extension
  27. *
  28. * @package OC\Preview
  29. */
  30. class HEIC extends Provider {
  31. /**
  32. * {@inheritDoc}
  33. */
  34. public function getMimeType(): string {
  35. return '/image\/hei(f|c)/';
  36. }
  37. /**
  38. * {@inheritDoc}
  39. */
  40. public function isAvailable(\OCP\Files\FileInfo $file): bool {
  41. return in_array('HEIC', \Imagick::queryFormats("HEI*"));
  42. }
  43. /**
  44. * {@inheritDoc}
  45. */
  46. public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) {
  47. $tmpPath = $fileview->toTmpFile($path);
  48. if (!$tmpPath) {
  49. return false;
  50. }
  51. // Creates \Imagick object from the heic file
  52. try {
  53. $bp = $this->getResizedPreview($tmpPath, $maxX, $maxY);
  54. $bp->setFormat('jpg');
  55. } catch (\Exception $e) {
  56. \OC::$server->getLogger()->logException($e, [
  57. 'message' => 'File: ' . $fileview->getAbsolutePath($path) . ' Imagick says:',
  58. 'level' => ILogger::ERROR,
  59. 'app' => 'core',
  60. ]);
  61. return false;
  62. }
  63. unlink($tmpPath);
  64. //new bitmap image object
  65. $image = new \OC_Image();
  66. $image->loadFromData($bp);
  67. //check if image object is valid
  68. return $image->valid() ? $image : false;
  69. }
  70. /**
  71. * Returns a preview of maxX times maxY dimensions in JPG format
  72. *
  73. * * The default resolution is already 72dpi, no need to change it for a bitmap output
  74. * * It's possible to have proper colour conversion using profileimage().
  75. * ICC profiles are here: http://www.color.org/srgbprofiles.xalter
  76. * * It's possible to Gamma-correct an image via gammaImage()
  77. *
  78. * @param string $tmpPath the location of the file to convert
  79. * @param int $maxX
  80. * @param int $maxY
  81. *
  82. * @return \Imagick
  83. */
  84. private function getResizedPreview($tmpPath, $maxX, $maxY) {
  85. $bp = new \Imagick();
  86. // Layer 0 contains either the bitmap or a flat representation of all vector layers
  87. $bp->readImage($tmpPath . '[0]');
  88. $bp->setImageFormat('jpg');
  89. $bp = $this->resize($bp, $maxX, $maxY);
  90. return $bp;
  91. }
  92. /**
  93. * Returns a resized \Imagick object
  94. *
  95. * If you want to know more on the various methods available to resize an
  96. * image, check out this link : @link https://stackoverflow.com/questions/8517304/what-the-difference-of-sample-resample-scale-resize-adaptive-resize-thumbnail-im
  97. *
  98. * @param \Imagick $bp
  99. * @param int $maxX
  100. * @param int $maxY
  101. *
  102. * @return \Imagick
  103. */
  104. private function resize($bp, $maxX, $maxY) {
  105. list($previewWidth, $previewHeight) = array_values($bp->getImageGeometry());
  106. // We only need to resize a preview which doesn't fit in the maximum dimensions
  107. if ($previewWidth > $maxX || $previewHeight > $maxY) {
  108. // If we want a small image (thumbnail) let's be most space- and time-efficient
  109. if ($maxX <= 500 && $maxY <= 500) {
  110. $bp->thumbnailImage($maxY, $maxX, true);
  111. $bp->stripImage();
  112. } else {
  113. // A bigger image calls for some better resizing algorithm
  114. // According to http://www.imagemagick.org/Usage/filter/#lanczos
  115. // the catrom filter is almost identical to Lanczos2, but according
  116. // to http://php.net/manual/en/imagick.resizeimage.php it is
  117. // significantly faster
  118. $bp->resizeImage($maxX, $maxY, \Imagick::FILTER_CATROM, 1, true);
  119. }
  120. }
  121. return $bp;
  122. }
  123. }