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.

119 lines
3.5 KiB

3 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Olivier Paroz <github@oparoz.com>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC\Preview;
  28. use Imagick;
  29. use OCP\Files\File;
  30. use OCP\IImage;
  31. use OCP\ILogger;
  32. /**
  33. * Creates a PNG preview using ImageMagick via the PECL extension
  34. *
  35. * @package OC\Preview
  36. */
  37. abstract class Bitmap extends ProviderV2 {
  38. /**
  39. * {@inheritDoc}
  40. */
  41. public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
  42. $tmpPath = $this->getLocalFile($file);
  43. // Creates \Imagick object from bitmap or vector file
  44. try {
  45. $bp = $this->getResizedPreview($tmpPath, $maxX, $maxY);
  46. } catch (\Exception $e) {
  47. \OC::$server->getLogger()->logException($e, [
  48. 'message' => 'File: ' . $file->getPath() . ' Imagick says:',
  49. 'level' => ILogger::ERROR,
  50. 'app' => 'core',
  51. ]);
  52. return null;
  53. }
  54. $this->cleanTmpFiles();
  55. //new bitmap image object
  56. $image = new \OC_Image();
  57. $image->loadFromData($bp);
  58. //check if image object is valid
  59. return $image->valid() ? $image : null;
  60. }
  61. /**
  62. * Returns a preview of maxX times maxY dimensions in PNG format
  63. *
  64. * * The default resolution is already 72dpi, no need to change it for a bitmap output
  65. * * It's possible to have proper colour conversion using profileimage().
  66. * ICC profiles are here: http://www.color.org/srgbprofiles.xalter
  67. * * It's possible to Gamma-correct an image via gammaImage()
  68. *
  69. * @param string $tmpPath the location of the file to convert
  70. * @param int $maxX
  71. * @param int $maxY
  72. *
  73. * @return \Imagick
  74. */
  75. private function getResizedPreview($tmpPath, $maxX, $maxY) {
  76. $bp = new Imagick();
  77. // Layer 0 contains either the bitmap or a flat representation of all vector layers
  78. $bp->readImage($tmpPath . '[0]');
  79. $bp = $this->resize($bp, $maxX, $maxY);
  80. $bp->setImageFormat('png');
  81. return $bp;
  82. }
  83. /**
  84. * Returns a resized \Imagick object
  85. *
  86. * If you want to know more on the various methods available to resize an
  87. * image, check out this link : @link https://stackoverflow.com/questions/8517304/what-the-difference-of-sample-resample-scale-resize-adaptive-resize-thumbnail-im
  88. *
  89. * @param \Imagick $bp
  90. * @param int $maxX
  91. * @param int $maxY
  92. *
  93. * @return \Imagick
  94. */
  95. private function resize($bp, $maxX, $maxY) {
  96. list($previewWidth, $previewHeight) = array_values($bp->getImageGeometry());
  97. // We only need to resize a preview which doesn't fit in the maximum dimensions
  98. if ($previewWidth > $maxX || $previewHeight > $maxY) {
  99. // TODO: LANCZOS is the default filter, CATROM could bring similar results faster
  100. $bp->resizeImage($maxX, $maxY, imagick::FILTER_LANCZOS, 1, true);
  101. }
  102. return $bp;
  103. }
  104. }