From ed15a6f2a25f07f621f3f0966ad7691cd3c2e645 Mon Sep 17 00:00:00 2001 From: Sebastian Steinmetz Date: Fri, 24 Aug 2018 08:58:28 +0200 Subject: [PATCH 1/2] HEIC previews as JPG rather than PNGs to save space. Signed-off-by: Sebastian Steinmetz --- lib/private/Preview/HEIC.php | 101 ++++++++++++++++++++++++++++++++++- 1 file changed, 99 insertions(+), 2 deletions(-) diff --git a/lib/private/Preview/HEIC.php b/lib/private/Preview/HEIC.php index 151326129b..a071eb51ab 100644 --- a/lib/private/Preview/HEIC.php +++ b/lib/private/Preview/HEIC.php @@ -22,7 +22,12 @@ namespace OC\Preview; -class HEIC extends Bitmap { +/** + * Creates a JPG preview using ImageMagick via the PECL extension + * + * @package OC\Preview + */ +class HEIC extends Provider { /** * {@inheritDoc} */ @@ -34,7 +39,99 @@ class HEIC extends Bitmap { * {@inheritDoc} */ public function isAvailable(\OCP\Files\FileInfo $file) { - return in_array("HEIC", \Imagick::queryFonts("HEI*") ); + return in_array("HEIC", \Imagick::queryFormats("HEI*") ); + } + + /** + * {@inheritDoc} + */ + public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { + $tmpPath = $fileview->toTmpFile($path); + if (!$tmpPath) { + return false; + } + + // Creates \Imagick object from the heic file + try { + $bp = $this->getResizedPreview($tmpPath, $maxX, $maxY); + $bp->setFormat('jpg'); + } catch (\Exception $e) { + \OC::$server->getLogger()->logException($e, [ + 'message' => 'File: ' . $fileview->getAbsolutePath($path) . ' Imagick says:', + 'level' => ILogger::ERROR, + 'app' => 'core', + ]); + return false; + } + + unlink($tmpPath); + + //new bitmap image object + $image = new \OC_Image(); + $image->loadFromData($bp); + //check if image object is valid + return $image->valid() ? $image : false; + } + + /** + * Returns a preview of maxX times maxY dimensions in JPG format + * + * * The default resolution is already 72dpi, no need to change it for a bitmap output + * * It's possible to have proper colour conversion using profileimage(). + * ICC profiles are here: http://www.color.org/srgbprofiles.xalter + * * It's possible to Gamma-correct an image via gammaImage() + * + * @param string $tmpPath the location of the file to convert + * @param int $maxX + * @param int $maxY + * + * @return \Imagick + */ + private function getResizedPreview($tmpPath, $maxX, $maxY) { + $bp = new Imagick(); + + // Layer 0 contains either the bitmap or a flat representation of all vector layers + $bp->readImage($tmpPath . '[0]'); + + $bp->setImageFormat('jpg'); + + $bp = $this->resize($bp, $maxX, $maxY); + + return $bp; + } + + /** + * Returns a resized \Imagick object + * + * If you want to know more on the various methods available to resize an + * image, check out this link : @link https://stackoverflow.com/questions/8517304/what-the-difference-of-sample-resample-scale-resize-adaptive-resize-thumbnail-im + * + * @param \Imagick $bp + * @param int $maxX + * @param int $maxY + * + * @return \Imagick + */ + private function resize($bp, $maxX, $maxY) { + list($previewWidth, $previewHeight) = array_values($bp->getImageGeometry()); + + // We only need to resize a preview which doesn't fit in the maximum dimensions + if ($previewWidth > $maxX || $previewHeight > $maxY) { + // If we want a small image (thumbnail) let's be most space- and time-efficient + if ($maxX <= 500 && $maxY <= 500) { + $bp->thumbnailImage($maxY, $maxX, true); + $bp->stripImage(); + } else { + // A bigger image calls for some better resizing algorithm + // According to http://www.imagemagick.org/Usage/filter/#lanczos + // the catrom filter is almost identical to Lanczos2, but according + // to http://php.net/manual/en/imagick.resizeimage.php it is + // significantly faster + $bp->resizeImage($maxX, $maxY, imagick::FILTER_CATROM, 1, true); + } + } + + return $bp; } } From 47589421219cf8361cc725421a21f8426370c2ef Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Fri, 24 Aug 2018 21:27:21 +0200 Subject: [PATCH 2/2] Fixes Signed-off-by: Roeland Jago Douma --- lib/private/Preview/HEIC.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/private/Preview/HEIC.php b/lib/private/Preview/HEIC.php index a071eb51ab..54bc212b6b 100644 --- a/lib/private/Preview/HEIC.php +++ b/lib/private/Preview/HEIC.php @@ -1,4 +1,5 @@ * @@ -22,6 +23,8 @@ namespace OC\Preview; +use OCP\ILogger; + /** * Creates a JPG preview using ImageMagick via the PECL extension * @@ -31,15 +34,15 @@ class HEIC extends Provider { /** * {@inheritDoc} */ - public function getMimeType() { + public function getMimeType(): string { return '/image\/hei(f|c)/'; } /** * {@inheritDoc} */ - public function isAvailable(\OCP\Files\FileInfo $file) { - return in_array("HEIC", \Imagick::queryFormats("HEI*") ); + public function isAvailable(\OCP\Files\FileInfo $file): bool { + return in_array('HEIC', \Imagick::queryFormats("HEI*")); } /** @@ -88,7 +91,7 @@ class HEIC extends Provider { * @return \Imagick */ private function getResizedPreview($tmpPath, $maxX, $maxY) { - $bp = new Imagick(); + $bp = new \Imagick(); // Layer 0 contains either the bitmap or a flat representation of all vector layers $bp->readImage($tmpPath . '[0]'); @@ -127,7 +130,7 @@ class HEIC extends Provider { // the catrom filter is almost identical to Lanczos2, but according // to http://php.net/manual/en/imagick.resizeimage.php it is // significantly faster - $bp->resizeImage($maxX, $maxY, imagick::FILTER_CATROM, 1, true); + $bp->resizeImage($maxX, $maxY, \Imagick::FILTER_CATROM, 1, true); } }