2014-10-04 19:50:12 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2016-07-21 18:07:57 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2015-06-25 12:43:55 +03:00
|
|
|
* @author Olivier Paroz <github@oparoz.com>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2015-03-26 13:44:34 +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/>
|
2015-03-26 13:44:34 +03:00
|
|
|
*
|
2014-10-04 19:50:12 +04:00
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2014-11-28 11:25:40 +03:00
|
|
|
namespace OC\Preview;
|
2014-10-04 19:50:12 +04:00
|
|
|
|
2015-04-01 16:54:22 +03:00
|
|
|
use Imagick;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCP\Files\File;
|
2019-06-04 16:25:25 +03:00
|
|
|
use OCP\IImage;
|
2018-04-25 16:22:28 +03:00
|
|
|
use OCP\ILogger;
|
2015-04-01 16:54:22 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a PNG preview using ImageMagick via the PECL extension
|
|
|
|
*
|
|
|
|
* @package OC\Preview
|
|
|
|
*/
|
2019-06-04 16:25:25 +03:00
|
|
|
abstract class Bitmap extends ProviderV2 {
|
2015-04-01 16:54:22 +03:00
|
|
|
|
2014-11-28 11:25:40 +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);
|
2014-11-28 11:25:40 +03:00
|
|
|
|
2015-04-01 16:54:22 +03:00
|
|
|
// Creates \Imagick object from bitmap or vector file
|
2014-11-28 11:25:40 +03:00
|
|
|
try {
|
2015-04-01 16:54:22 +03:00
|
|
|
$bp = $this->getResizedPreview($tmpPath, $maxX, $maxY);
|
2014-11-28 11:25:40 +03:00
|
|
|
} catch (\Exception $e) {
|
2018-01-17 17:21:56 +03:00
|
|
|
\OC::$server->getLogger()->logException($e, [
|
2019-06-04 16:25:25 +03:00
|
|
|
'message' => 'File: ' . $file->getPath() . ' Imagick says:',
|
2018-04-25 16:22:28 +03:00
|
|
|
'level' => ILogger::ERROR,
|
2018-01-17 17:21:56 +03:00
|
|
|
'app' => 'core',
|
|
|
|
]);
|
2019-06-04 16:25:25 +03:00
|
|
|
return null;
|
2014-10-04 19:50:12 +04:00
|
|
|
}
|
|
|
|
|
2019-06-04 16:25:25 +03:00
|
|
|
$this->cleanTmpFiles();
|
2014-11-27 21:40:23 +03:00
|
|
|
|
2014-11-28 11:25:40 +03:00
|
|
|
//new bitmap image object
|
2015-04-01 16:54:22 +03:00
|
|
|
$image = new \OC_Image();
|
|
|
|
$image->loadFromData($bp);
|
2014-11-28 11:25:40 +03:00
|
|
|
//check if image object is valid
|
2019-06-04 16:25:25 +03:00
|
|
|
return $image->valid() ? $image : null;
|
2014-10-04 19:50:12 +04:00
|
|
|
}
|
2015-04-01 16:54:22 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a preview of maxX times maxY dimensions in PNG 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 = $this->resize($bp, $maxX, $maxY);
|
|
|
|
|
|
|
|
$bp->setImageFormat('png');
|
|
|
|
|
|
|
|
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());
|
2015-04-01 16:54:22 +03:00
|
|
|
|
|
|
|
// We only need to resize a preview which doesn't fit in the maximum dimensions
|
|
|
|
if ($previewWidth > $maxX || $previewHeight > $maxY) {
|
|
|
|
// TODO: LANCZOS is the default filter, CATROM could bring similar results faster
|
|
|
|
$bp->resizeImage($maxX, $maxY, imagick::FILTER_LANCZOS, 1, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $bp;
|
|
|
|
}
|
2014-11-28 11:25:40 +03:00
|
|
|
}
|