2016-08-04 20:41:04 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
2020-04-29 12:57:22 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-08-04 20:41:04 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2016-08-04 20:41:04 +03:00
|
|
|
*
|
|
|
|
*/
|
2016-10-14 22:21:37 +03:00
|
|
|
|
|
|
|
namespace OC\Preview;
|
2016-08-04 20:41:04 +03:00
|
|
|
|
|
|
|
use OCP\Files\File;
|
2016-10-07 15:16:38 +03:00
|
|
|
use OCP\Files\IAppData;
|
2016-08-04 20:41:04 +03:00
|
|
|
use OCP\Files\NotFoundException;
|
2017-03-19 22:30:46 +03:00
|
|
|
use OCP\Files\NotPermittedException;
|
2016-10-07 15:16:38 +03:00
|
|
|
use OCP\Files\SimpleFS\ISimpleFile;
|
|
|
|
use OCP\Files\SimpleFS\ISimpleFolder;
|
2016-08-04 20:41:04 +03:00
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\IImage;
|
|
|
|
use OCP\IPreview;
|
2019-06-04 16:25:25 +03:00
|
|
|
use OCP\Preview\IProviderV2;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCP\Preview\IVersionedPreviewFile;
|
2016-11-10 16:04:59 +03:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
|
|
|
use Symfony\Component\EventDispatcher\GenericEvent;
|
2016-08-04 20:41:04 +03:00
|
|
|
|
2016-10-14 22:21:37 +03:00
|
|
|
class Generator {
|
2016-08-04 20:41:04 +03:00
|
|
|
|
|
|
|
/** @var IPreview */
|
|
|
|
private $previewManager;
|
|
|
|
/** @var IConfig */
|
|
|
|
private $config;
|
2016-10-07 15:16:38 +03:00
|
|
|
/** @var IAppData */
|
|
|
|
private $appData;
|
2016-10-17 22:53:23 +03:00
|
|
|
/** @var GeneratorHelper */
|
|
|
|
private $helper;
|
2016-11-10 16:04:59 +03:00
|
|
|
/** @var EventDispatcherInterface */
|
|
|
|
private $eventDispatcher;
|
2016-08-04 20:41:04 +03:00
|
|
|
|
2016-10-14 20:27:14 +03:00
|
|
|
/**
|
|
|
|
* @param IConfig $config
|
|
|
|
* @param IPreview $previewManager
|
|
|
|
* @param IAppData $appData
|
2016-10-17 22:53:23 +03:00
|
|
|
* @param GeneratorHelper $helper
|
2016-11-10 16:04:59 +03:00
|
|
|
* @param EventDispatcherInterface $eventDispatcher
|
2016-10-14 20:27:14 +03:00
|
|
|
*/
|
2016-08-04 20:41:04 +03:00
|
|
|
public function __construct(
|
|
|
|
IConfig $config,
|
|
|
|
IPreview $previewManager,
|
2016-10-17 22:53:23 +03:00
|
|
|
IAppData $appData,
|
2016-11-10 16:04:59 +03:00
|
|
|
GeneratorHelper $helper,
|
|
|
|
EventDispatcherInterface $eventDispatcher
|
2016-08-04 20:41:04 +03:00
|
|
|
) {
|
|
|
|
$this->config = $config;
|
|
|
|
$this->previewManager = $previewManager;
|
2016-10-07 15:16:38 +03:00
|
|
|
$this->appData = $appData;
|
2016-10-17 22:53:23 +03:00
|
|
|
$this->helper = $helper;
|
2016-11-10 16:04:59 +03:00
|
|
|
$this->eventDispatcher = $eventDispatcher;
|
2016-08-04 20:41:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a preview of a file
|
|
|
|
*
|
|
|
|
* The cache is searched first and if nothing usable was found then a preview is
|
|
|
|
* generated by one of the providers
|
|
|
|
*
|
2016-10-16 17:48:11 +03:00
|
|
|
* @param File $file
|
2016-08-04 20:41:04 +03:00
|
|
|
* @param int $width
|
|
|
|
* @param int $height
|
|
|
|
* @param bool $crop
|
|
|
|
* @param string $mode
|
2016-10-16 21:42:35 +03:00
|
|
|
* @param string $mimeType
|
2016-10-07 15:16:38 +03:00
|
|
|
* @return ISimpleFile
|
2016-08-04 20:41:04 +03:00
|
|
|
* @throws NotFoundException
|
2017-05-02 00:35:47 +03:00
|
|
|
* @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid)
|
2016-08-04 20:41:04 +03:00
|
|
|
*/
|
2016-10-16 21:42:35 +03:00
|
|
|
public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) {
|
2020-02-16 03:45:47 +03:00
|
|
|
$specification = [
|
|
|
|
'width' => $width,
|
|
|
|
'height' => $height,
|
|
|
|
'crop' => $crop,
|
|
|
|
'mode' => $mode,
|
|
|
|
];
|
|
|
|
$this->eventDispatcher->dispatch(
|
|
|
|
IPreview::EVENT,
|
|
|
|
new GenericEvent($file, $specification)
|
|
|
|
);
|
|
|
|
|
|
|
|
// since we only ask for one preview, and the generate method return the last one it created, it returns the one we want
|
|
|
|
return $this->generatePreviews($file, [$specification], $mimeType);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates previews of a file
|
|
|
|
*
|
|
|
|
* @param File $file
|
|
|
|
* @param array $specifications
|
|
|
|
* @param string $mimeType
|
|
|
|
* @return ISimpleFile the last preview that was generated
|
|
|
|
* @throws NotFoundException
|
|
|
|
* @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid)
|
|
|
|
*/
|
|
|
|
public function generatePreviews(File $file, array $specifications, $mimeType = null) {
|
2018-05-29 22:09:08 +03:00
|
|
|
//Make sure that we can read the file
|
|
|
|
if (!$file->isReadable()) {
|
|
|
|
throw new NotFoundException('Cannot read file');
|
|
|
|
}
|
|
|
|
|
2016-10-16 21:42:35 +03:00
|
|
|
if ($mimeType === null) {
|
|
|
|
$mimeType = $file->getMimeType();
|
|
|
|
}
|
|
|
|
if (!$this->previewManager->isMimeSupported($mimeType)) {
|
2016-08-04 20:41:04 +03:00
|
|
|
throw new NotFoundException();
|
|
|
|
}
|
|
|
|
|
2016-10-16 17:48:11 +03:00
|
|
|
$previewFolder = $this->getPreviewFolder($file);
|
2016-08-04 20:41:04 +03:00
|
|
|
|
2019-06-04 14:44:15 +03:00
|
|
|
$previewVersion = '';
|
|
|
|
if ($file instanceof IVersionedPreviewFile) {
|
|
|
|
$previewVersion = $file->getPreviewVersion() . '-';
|
|
|
|
}
|
|
|
|
|
2016-08-04 20:41:04 +03:00
|
|
|
// Get the max preview and infer the max preview sizes from that
|
2019-06-04 14:44:15 +03:00
|
|
|
$maxPreview = $this->getMaxPreview($previewFolder, $file, $mimeType, $previewVersion);
|
2020-02-16 04:34:09 +03:00
|
|
|
$maxPreviewImage = null; // only load the image when we need it
|
2018-01-22 15:57:00 +03:00
|
|
|
if ($maxPreview->getSize() === 0) {
|
|
|
|
$maxPreview->delete();
|
|
|
|
throw new NotFoundException('Max preview size 0, invalid!');
|
|
|
|
}
|
|
|
|
|
2020-02-16 03:45:47 +03:00
|
|
|
[$maxWidth, $maxHeight] = $this->getPreviewSize($maxPreview, $previewVersion);
|
2016-08-04 20:41:04 +03:00
|
|
|
|
2020-02-16 03:45:47 +03:00
|
|
|
$preview = null;
|
2017-09-21 12:32:22 +03:00
|
|
|
|
2020-02-16 03:45:47 +03:00
|
|
|
foreach ($specifications as $specification) {
|
|
|
|
$width = $specification['width'] ?? -1;
|
|
|
|
$height = $specification['height'] ?? -1;
|
|
|
|
$crop = $specification['crop'] ?? false;
|
|
|
|
$mode = $specification['mode'] ?? IPreview::MODE_FILL;
|
2016-08-04 20:41:04 +03:00
|
|
|
|
2020-02-16 03:45:47 +03:00
|
|
|
// If both width and heigth are -1 we just want the max preview
|
|
|
|
if ($width === -1 && $height === -1) {
|
|
|
|
$width = $maxWidth;
|
|
|
|
$height = $maxHeight;
|
|
|
|
}
|
2017-03-19 22:23:23 +03:00
|
|
|
|
2020-02-16 03:45:47 +03:00
|
|
|
// Calculate the preview size
|
|
|
|
[$width, $height] = $this->calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHeight);
|
|
|
|
|
|
|
|
// No need to generate a preview that is just the max preview
|
|
|
|
if ($width === $maxWidth && $height === $maxHeight) {
|
|
|
|
// ensure correct return value if this was the last one
|
|
|
|
$preview = $maxPreview;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to get a cached preview. Else generate (and store) one
|
2018-01-07 13:46:24 +03:00
|
|
|
try {
|
2020-02-16 03:45:47 +03:00
|
|
|
try {
|
|
|
|
$preview = $this->getCachedPreview($previewFolder, $width, $height, $crop, $maxPreview->getMimeType(), $previewVersion);
|
|
|
|
} catch (NotFoundException $e) {
|
2020-02-16 04:34:09 +03:00
|
|
|
if ($maxPreviewImage === null) {
|
|
|
|
$maxPreviewImage = $this->helper->getImage($maxPreview);
|
|
|
|
}
|
|
|
|
|
|
|
|
$preview = $this->generatePreview($previewFolder, $maxPreviewImage, $width, $height, $crop, $maxWidth, $maxHeight, $previewVersion);
|
2020-02-16 03:45:47 +03:00
|
|
|
}
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
2020-04-09 16:59:18 +03:00
|
|
|
throw new NotFoundException("", 0, $e);
|
2018-01-07 13:46:24 +03:00
|
|
|
}
|
2016-08-04 20:41:04 +03:00
|
|
|
|
2020-02-16 03:45:47 +03:00
|
|
|
if ($preview->getSize() === 0) {
|
|
|
|
$preview->delete();
|
|
|
|
throw new NotFoundException('Cached preview size 0, invalid!');
|
|
|
|
}
|
2018-01-22 15:57:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $preview;
|
2016-08-04 20:41:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-10-07 15:16:38 +03:00
|
|
|
* @param ISimpleFolder $previewFolder
|
2016-10-16 17:48:11 +03:00
|
|
|
* @param File $file
|
2016-10-16 21:42:35 +03:00
|
|
|
* @param string $mimeType
|
2019-06-04 14:44:15 +03:00
|
|
|
* @param string $prefix
|
2016-10-07 15:16:38 +03:00
|
|
|
* @return ISimpleFile
|
2016-08-04 20:41:04 +03:00
|
|
|
* @throws NotFoundException
|
|
|
|
*/
|
2019-06-04 14:44:15 +03:00
|
|
|
private function getMaxPreview(ISimpleFolder $previewFolder, File $file, $mimeType, $prefix) {
|
2016-08-04 20:41:04 +03:00
|
|
|
$nodes = $previewFolder->getDirectoryListing();
|
|
|
|
|
|
|
|
foreach ($nodes as $node) {
|
2019-06-04 14:44:15 +03:00
|
|
|
$name = $node->getName();
|
|
|
|
if (($prefix === '' || strpos($name, $prefix) === 0) && strpos($name, 'max')) {
|
2016-08-04 20:41:04 +03:00
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$previewProviders = $this->previewManager->getProviders();
|
|
|
|
foreach ($previewProviders as $supportedMimeType => $providers) {
|
2016-10-16 21:42:35 +03:00
|
|
|
if (!preg_match($supportedMimeType, $mimeType)) {
|
2016-08-04 20:41:04 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-06-04 16:25:25 +03:00
|
|
|
foreach ($providers as $providerClosure) {
|
|
|
|
$provider = $this->helper->getProvider($providerClosure);
|
|
|
|
if (!($provider instanceof IProviderV2)) {
|
2016-08-04 20:41:04 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-08-16 11:45:51 +03:00
|
|
|
if (!$provider->isAvailable($file)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-01-08 13:39:09 +03:00
|
|
|
$maxWidth = (int)$this->config->getSystemValue('preview_max_x', 4096);
|
|
|
|
$maxHeight = (int)$this->config->getSystemValue('preview_max_y', 4096);
|
2016-08-04 20:41:04 +03:00
|
|
|
|
2016-10-17 22:53:23 +03:00
|
|
|
$preview = $this->helper->getThumbnail($provider, $file, $maxWidth, $maxHeight);
|
2016-08-04 20:41:04 +03:00
|
|
|
|
|
|
|
if (!($preview instanceof IImage)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-01-07 13:46:24 +03:00
|
|
|
// Try to get the extention.
|
|
|
|
try {
|
|
|
|
$ext = $this->getExtention($preview->dataMimeType());
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
// Just continue to the next iteration if this preview doesn't have a valid mimetype
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-06-04 14:44:15 +03:00
|
|
|
$path = $prefix . (string)$preview->width() . '-' . (string)$preview->height() . '-max.' . $ext;
|
2017-03-19 22:30:46 +03:00
|
|
|
try {
|
|
|
|
$file = $previewFolder->newFile($path);
|
|
|
|
$file->putContent($preview->data());
|
|
|
|
} catch (NotPermittedException $e) {
|
|
|
|
throw new NotFoundException();
|
|
|
|
}
|
2016-08-04 20:41:04 +03:00
|
|
|
|
|
|
|
return $file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-10-07 15:16:38 +03:00
|
|
|
* @param ISimpleFile $file
|
2019-06-04 14:44:15 +03:00
|
|
|
* @param string $prefix
|
2016-08-04 20:41:04 +03:00
|
|
|
* @return int[]
|
|
|
|
*/
|
2019-06-04 14:44:15 +03:00
|
|
|
private function getPreviewSize(ISimpleFile $file, string $prefix = '') {
|
|
|
|
$size = explode('-', substr($file->getName(), strlen($prefix)));
|
2016-08-04 20:41:04 +03:00
|
|
|
return [(int)$size[0], (int)$size[1]];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $width
|
|
|
|
* @param int $height
|
|
|
|
* @param bool $crop
|
2018-01-04 12:00:07 +03:00
|
|
|
* @param string $mimeType
|
2019-06-04 14:44:15 +03:00
|
|
|
* @param string $prefix
|
2016-08-04 20:41:04 +03:00
|
|
|
* @return string
|
|
|
|
*/
|
2019-06-04 14:44:15 +03:00
|
|
|
private function generatePath($width, $height, $crop, $mimeType, $prefix) {
|
|
|
|
$path = $prefix . (string)$width . '-' . (string)$height;
|
2016-08-04 20:41:04 +03:00
|
|
|
if ($crop) {
|
|
|
|
$path .= '-crop';
|
|
|
|
}
|
2018-01-04 12:00:07 +03:00
|
|
|
|
|
|
|
$ext = $this->getExtention($mimeType);
|
|
|
|
$path .= '.' . $ext;
|
2016-08-04 20:41:04 +03:00
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $width
|
|
|
|
* @param int $height
|
|
|
|
* @param bool $crop
|
|
|
|
* @param string $mode
|
|
|
|
* @param int $maxWidth
|
|
|
|
* @param int $maxHeight
|
|
|
|
* @return int[]
|
|
|
|
*/
|
|
|
|
private function calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHeight) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we are not cropping we have to make sure the requested image
|
|
|
|
* respects the aspect ratio of the original.
|
|
|
|
*/
|
|
|
|
if (!$crop) {
|
|
|
|
$ratio = $maxHeight / $maxWidth;
|
|
|
|
|
|
|
|
if ($width === -1) {
|
|
|
|
$width = $height / $ratio;
|
|
|
|
}
|
|
|
|
if ($height === -1) {
|
|
|
|
$height = $width * $ratio;
|
|
|
|
}
|
|
|
|
|
|
|
|
$ratioH = $height / $maxHeight;
|
|
|
|
$ratioW = $width / $maxWidth;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fill means that the $height and $width are the max
|
|
|
|
* Cover means min.
|
|
|
|
*/
|
2016-10-16 17:48:11 +03:00
|
|
|
if ($mode === IPreview::MODE_FILL) {
|
2016-08-04 20:41:04 +03:00
|
|
|
if ($ratioH > $ratioW) {
|
|
|
|
$height = $width * $ratio;
|
|
|
|
} else {
|
|
|
|
$width = $height / $ratio;
|
|
|
|
}
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($mode === IPreview::MODE_COVER) {
|
2016-08-04 20:41:04 +03:00
|
|
|
if ($ratioH > $ratioW) {
|
|
|
|
$width = $height / $ratio;
|
|
|
|
} else {
|
|
|
|
$height = $width * $ratio;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($height !== $maxHeight && $width !== $maxWidth) {
|
|
|
|
/*
|
2018-10-31 15:40:41 +03:00
|
|
|
* Scale to the nearest power of four
|
2016-08-04 20:41:04 +03:00
|
|
|
*/
|
2018-10-31 15:40:41 +03:00
|
|
|
$pow4height = 4 ** ceil(log($height) / log(4));
|
|
|
|
$pow4width = 4 ** ceil(log($width) / log(4));
|
2016-08-04 20:41:04 +03:00
|
|
|
|
2018-10-31 15:40:41 +03:00
|
|
|
// Minimum size is 64
|
|
|
|
$pow4height = max($pow4height, 64);
|
|
|
|
$pow4width = max($pow4width, 64);
|
|
|
|
|
|
|
|
$ratioH = $height / $pow4height;
|
|
|
|
$ratioW = $width / $pow4width;
|
2016-08-04 20:41:04 +03:00
|
|
|
|
|
|
|
if ($ratioH < $ratioW) {
|
2018-10-31 15:40:41 +03:00
|
|
|
$width = $pow4width;
|
2017-03-19 22:30:46 +03:00
|
|
|
$height /= $ratioW;
|
2016-08-04 20:41:04 +03:00
|
|
|
} else {
|
2018-10-31 15:40:41 +03:00
|
|
|
$height = $pow4height;
|
2017-03-19 22:30:46 +03:00
|
|
|
$width /= $ratioH;
|
2016-08-04 20:41:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2020-04-09 10:22:29 +03:00
|
|
|
* Make sure the requested height and width fall within the max
|
|
|
|
* of the preview.
|
|
|
|
*/
|
2016-08-04 20:41:04 +03:00
|
|
|
if ($height > $maxHeight) {
|
|
|
|
$ratio = $height / $maxHeight;
|
|
|
|
$height = $maxHeight;
|
2017-03-19 22:30:46 +03:00
|
|
|
$width /= $ratio;
|
2016-08-04 20:41:04 +03:00
|
|
|
}
|
|
|
|
if ($width > $maxWidth) {
|
|
|
|
$ratio = $width / $maxWidth;
|
|
|
|
$width = $maxWidth;
|
2017-03-19 22:30:46 +03:00
|
|
|
$height /= $ratio;
|
2016-08-04 20:41:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return [(int)round($width), (int)round($height)];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-10-07 15:16:38 +03:00
|
|
|
* @param ISimpleFolder $previewFolder
|
|
|
|
* @param ISimpleFile $maxPreview
|
2016-08-04 20:41:04 +03:00
|
|
|
* @param int $width
|
|
|
|
* @param int $height
|
|
|
|
* @param bool $crop
|
2016-10-16 16:47:30 +03:00
|
|
|
* @param int $maxWidth
|
2016-08-04 20:41:04 +03:00
|
|
|
* @param int $maxHeight
|
2019-06-04 14:44:15 +03:00
|
|
|
* @param string $prefix
|
2016-10-07 15:16:38 +03:00
|
|
|
* @return ISimpleFile
|
2016-08-04 20:41:04 +03:00
|
|
|
* @throws NotFoundException
|
2017-05-02 00:35:47 +03:00
|
|
|
* @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid)
|
2016-08-04 20:41:04 +03:00
|
|
|
*/
|
2020-02-16 04:34:09 +03:00
|
|
|
private function generatePreview(ISimpleFolder $previewFolder, IImage $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight, $prefix) {
|
|
|
|
$preview = $maxPreview;
|
2017-05-01 15:03:00 +03:00
|
|
|
if (!$preview->valid()) {
|
|
|
|
throw new \InvalidArgumentException('Failed to generate preview, failed to load image');
|
|
|
|
}
|
|
|
|
|
2016-08-04 20:41:04 +03:00
|
|
|
if ($crop) {
|
|
|
|
if ($height !== $preview->height() && $width !== $preview->width()) {
|
|
|
|
//Resize
|
|
|
|
$widthR = $preview->width() / $width;
|
|
|
|
$heightR = $preview->height() / $height;
|
|
|
|
|
|
|
|
if ($widthR > $heightR) {
|
|
|
|
$scaleH = $height;
|
|
|
|
$scaleW = $maxWidth / $heightR;
|
|
|
|
} else {
|
|
|
|
$scaleH = $maxHeight / $widthR;
|
|
|
|
$scaleW = $width;
|
|
|
|
}
|
2020-02-16 04:34:09 +03:00
|
|
|
$preview = $preview->preciseResizeCopy((int)round($scaleW), (int)round($scaleH));
|
2016-08-04 20:41:04 +03:00
|
|
|
}
|
2018-01-13 02:34:28 +03:00
|
|
|
$cropX = (int)floor(abs($width - $preview->width()) * 0.5);
|
2019-12-22 17:07:11 +03:00
|
|
|
$cropY = (int)floor(abs($height - $preview->height()) * 0.5);
|
2020-02-16 04:34:09 +03:00
|
|
|
$preview = $preview->cropCopy($cropX, $cropY, $width, $height);
|
2016-08-04 20:41:04 +03:00
|
|
|
} else {
|
2020-02-16 04:34:09 +03:00
|
|
|
$preview = $maxPreview->resizeCopy(max($width, $height));
|
2016-08-04 20:41:04 +03:00
|
|
|
}
|
|
|
|
|
2017-05-01 15:03:00 +03:00
|
|
|
|
2019-06-04 14:44:15 +03:00
|
|
|
$path = $this->generatePath($width, $height, $crop, $preview->dataMimeType(), $prefix);
|
2017-03-19 22:30:46 +03:00
|
|
|
try {
|
|
|
|
$file = $previewFolder->newFile($path);
|
|
|
|
$file->putContent($preview->data());
|
|
|
|
} catch (NotPermittedException $e) {
|
|
|
|
throw new NotFoundException();
|
|
|
|
}
|
2016-08-04 20:41:04 +03:00
|
|
|
|
|
|
|
return $file;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-10-07 15:16:38 +03:00
|
|
|
* @param ISimpleFolder $previewFolder
|
2016-08-04 20:41:04 +03:00
|
|
|
* @param int $width
|
|
|
|
* @param int $height
|
|
|
|
* @param bool $crop
|
2018-01-04 12:00:07 +03:00
|
|
|
* @param string $mimeType
|
2019-06-04 14:44:15 +03:00
|
|
|
* @param string $prefix
|
2016-10-07 15:16:38 +03:00
|
|
|
* @return ISimpleFile
|
2016-08-04 20:41:04 +03:00
|
|
|
*
|
|
|
|
* @throws NotFoundException
|
|
|
|
*/
|
2019-06-04 14:44:15 +03:00
|
|
|
private function getCachedPreview(ISimpleFolder $previewFolder, $width, $height, $crop, $mimeType, $prefix) {
|
|
|
|
$path = $this->generatePath($width, $height, $crop, $mimeType, $prefix);
|
2016-08-04 20:41:04 +03:00
|
|
|
|
2016-10-07 15:16:38 +03:00
|
|
|
return $previewFolder->getFile($path);
|
2016-08-04 20:41:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the specific preview folder for this file
|
|
|
|
*
|
2016-10-16 17:48:11 +03:00
|
|
|
* @param File $file
|
2016-10-07 15:16:38 +03:00
|
|
|
* @return ISimpleFolder
|
2016-08-04 20:41:04 +03:00
|
|
|
*/
|
2016-10-16 17:48:11 +03:00
|
|
|
private function getPreviewFolder(File $file) {
|
2016-08-04 20:41:04 +03:00
|
|
|
try {
|
2016-10-16 17:48:11 +03:00
|
|
|
$folder = $this->appData->getFolder($file->getId());
|
2016-08-04 20:41:04 +03:00
|
|
|
} catch (NotFoundException $e) {
|
2016-10-16 17:48:11 +03:00
|
|
|
$folder = $this->appData->newFolder($file->getId());
|
2016-08-04 20:41:04 +03:00
|
|
|
}
|
|
|
|
|
2016-10-07 15:16:38 +03:00
|
|
|
return $folder;
|
2016-08-04 20:41:04 +03:00
|
|
|
}
|
2018-01-04 12:00:07 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $mimeType
|
|
|
|
* @return null|string
|
2018-01-07 13:46:24 +03:00
|
|
|
* @throws \InvalidArgumentException
|
2018-01-04 12:00:07 +03:00
|
|
|
*/
|
|
|
|
private function getExtention($mimeType) {
|
|
|
|
switch ($mimeType) {
|
|
|
|
case 'image/png':
|
|
|
|
return 'png';
|
|
|
|
case 'image/jpeg':
|
|
|
|
return 'jpg';
|
|
|
|
case 'image/gif':
|
|
|
|
return 'gif';
|
|
|
|
default:
|
2020-04-09 16:59:18 +03:00
|
|
|
throw new \InvalidArgumentException('Not a valid mimetype: "' . $mimeType . '"');
|
2018-01-04 12:00:07 +03:00
|
|
|
}
|
|
|
|
}
|
2016-08-04 20:41:04 +03:00
|
|
|
}
|