Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Fix image ration not preserved when cropping
Signed-off-by: Samuel CHEMLA <[email protected]>
  • Loading branch information
phpbg committed Sep 5, 2019
commit 51041f979fa091e8bf8a20776230dd378eeb1f4b
33 changes: 19 additions & 14 deletions lib/private/Preview/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,18 @@ public function getPreview(File $file, $width = -1, $height = -1, $crop = false,
$previewVersion = $file->getPreviewVersion() . '-';
}

$maxWidth = (int)$this->config->getSystemValue('preview_max_x', 4096);
$maxHeight = (int)$this->config->getSystemValue('preview_max_y', 4096);
// Compute max width and height preserving aspect ratio
// TODO test this with images smaller than preview_max_x and/or preview_max_y
// TODO Does this code really belong to here... see lib/private/legacy/image.php::resize() for example
$image = new OCPImage();
$image->loadFromData($file->getContent());
$heightOrig = $image->height();
$widthOrig = $image->width();
$ratio = $widthOrig / $heightOrig;
$maxWidthConfig = (int)$this->config->getSystemValue('preview_max_x', 4096);
$maxHeightConfig = (int)$this->config->getSystemValue('preview_max_y', 4096);
$maxWidth = min($maxWidthConfig, $ratio * $maxHeightConfig);
$maxHeight = min($maxHeightConfig, $maxWidthConfig / $ratio);

// If both width and heigth are -1 we just want the max preview
if ($width === -1 && $height === -1) {
Expand All @@ -136,12 +146,13 @@ public function getPreview(File $file, $width = -1, $height = -1, $crop = false,
return $maxPreview;
}*/


// Try to get a cached preview. Else generate (and store) one
try {
try {
$preview = $this->getCachedPreview($previewFolder, $width, $height, $crop, $mimeType, $previewVersion);
} catch (NotFoundException $e) {
$preview = $this->generatePreview($previewFolder, $file, $width, $height, $crop, $maxWidth, $maxHeight, $previewVersion);
$preview = $this->generatePreview($previewFolder, $image, $width, $height, $crop, $previewVersion);
}
} catch (\InvalidArgumentException $e) {
throw new NotFoundException();
Expand Down Expand Up @@ -279,32 +290,26 @@ private function calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHei
* @param int $width
* @param int $height
* @param bool $crop
* @param int $maxWidth
* @param int $maxHeight
* @param string $prefix
* @return ISimpleFile
* @throws NotFoundException
* @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid)
*/
private function generatePreview(ISimpleFolder $previewFolder, File $file, $width, $height, $crop, $maxWidth, $maxHeight, $prefix) {
$preview = new OCPImage();
$preview->loadFromData($file->getContent());

private function generatePreview(ISimpleFolder $previewFolder, OCPImage $preview, $width, $height, $crop, $prefix) {
if (!$preview->valid()) {
throw new \InvalidArgumentException('Failed to generate preview, failed to load image');
}

if ($crop) {
if ($height !== $preview->height() && $width !== $preview->width()) {
//Resize
$widthR = $preview->width() / $width;
$heightR = $preview->height() / $height;
$ratioOrig = $preview->width() / $preview->height();

if ($widthR > $heightR) {
if ($ratioOrig > 1) {
$scaleH = $height;
$scaleW = $maxWidth / $heightR;
$scaleW = $height * $ratioOrig;
} else {
$scaleH = $maxHeight / $widthR;
$scaleH = $width / $ratioOrig;
$scaleW = $width;
}
$preview->preciseResize((int)round($scaleW), (int)round($scaleH));
Expand Down