2018-08-03 16:33:58 +03:00
|
|
|
<?php
|
2019-12-03 21:57:53 +03:00
|
|
|
|
2018-08-24 22:27:21 +03:00
|
|
|
declare(strict_types=1);
|
2019-12-03 21:57:53 +03:00
|
|
|
|
2018-08-03 16:33:58 +03:00
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2018, ownCloud GmbH
|
|
|
|
* @copyright Copyright (c) 2018, Sebastian Steinmetz (me@sebastiansteinmetz.ch)
|
2019-12-03 21:57:53 +03:00
|
|
|
*
|
2020-12-30 16:07:05 +03:00
|
|
|
* @author J0WI <J0WI@users.noreply.github.com>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
* @author Sebastian Steinmetz <462714+steiny2k@users.noreply.github.com>
|
|
|
|
* @author Sebastian Steinmetz <me@sebastiansteinmetz.ch>
|
|
|
|
*
|
2018-08-03 16:33:58 +03:00
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2018-08-03 16:33:58 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Preview;
|
|
|
|
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCP\Files\File;
|
2019-06-04 16:25:25 +03:00
|
|
|
use OCP\IImage;
|
2018-08-24 22:27:21 +03:00
|
|
|
use OCP\ILogger;
|
|
|
|
|
2018-08-24 09:58:28 +03:00
|
|
|
/**
|
|
|
|
* Creates a JPG preview using ImageMagick via the PECL extension
|
|
|
|
*
|
|
|
|
* @package OC\Preview
|
|
|
|
*/
|
2019-06-04 16:25:25 +03:00
|
|
|
class HEIC extends ProviderV2 {
|
2018-08-03 16:33:58 +03:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2018-08-24 22:27:21 +03:00
|
|
|
public function getMimeType(): string {
|
2018-08-03 16:33:58 +03:00
|
|
|
return '/image\/hei(f|c)/';
|
|
|
|
}
|
2018-08-17 19:34:51 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2018-08-24 22:27:21 +03:00
|
|
|
public function isAvailable(\OCP\Files\FileInfo $file): bool {
|
|
|
|
return in_array('HEIC', \Imagick::queryFormats("HEI*"));
|
2018-08-24 09:58:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2019-06-04 16:25:25 +03:00
|
|
|
public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
|
|
|
|
$tmpPath = $this->getLocalFile($file);
|
2018-08-24 09:58:28 +03:00
|
|
|
|
|
|
|
// 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, [
|
2019-06-04 16:25:25 +03:00
|
|
|
'message' => 'File: ' . $file->getPath() . ' Imagick says:',
|
2018-08-24 09:58:28 +03:00
|
|
|
'level' => ILogger::ERROR,
|
|
|
|
'app' => 'core',
|
|
|
|
]);
|
2019-06-04 16:25:25 +03:00
|
|
|
return null;
|
2018-08-24 09:58:28 +03:00
|
|
|
}
|
|
|
|
|
2019-06-04 16:25:25 +03:00
|
|
|
$this->cleanTmpFiles();
|
2018-08-24 09:58:28 +03:00
|
|
|
|
|
|
|
//new bitmap image object
|
|
|
|
$image = new \OC_Image();
|
|
|
|
$image->loadFromData($bp);
|
|
|
|
//check if image object is valid
|
2019-06-04 16:25:25 +03:00
|
|
|
return $image->valid() ? $image : null;
|
2018-08-24 09:58:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2018-08-24 22:27:21 +03:00
|
|
|
$bp = new \Imagick();
|
2018-08-24 09:58:28 +03:00
|
|
|
|
|
|
|
// 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) {
|
2021-01-12 12:15:48 +03:00
|
|
|
[$previewWidth, $previewHeight] = array_values($bp->getImageGeometry());
|
2018-08-24 09:58:28 +03:00
|
|
|
|
|
|
|
// 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
|
2020-09-17 18:23:07 +03:00
|
|
|
// to https://www.php.net/manual/en/imagick.resizeimage.php it is
|
2018-08-24 09:58:28 +03:00
|
|
|
// significantly faster
|
2018-08-24 22:27:21 +03:00
|
|
|
$bp->resizeImage($maxX, $maxY, \Imagick::FILTER_CATROM, 1, true);
|
2018-08-24 09:58:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $bp;
|
2018-08-17 19:34:51 +03:00
|
|
|
}
|
2018-08-03 16:33:58 +03:00
|
|
|
}
|