Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add memories.image.highres/convert_all_images_formarts/format/quality/_max_x/_max_y/ #653

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
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
45 changes: 41 additions & 4 deletions lib/Controller/ImageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,25 @@ public function decodable(string $id): Http\Response
$blob = $file->getContent();

// Convert image to JPEG if required
if (!\in_array($mimetype, ['image/png', 'image/webp', 'image/jpeg', 'image/gif'], true)) {
$highres_enabled = $this->config->getSystemValueString('memories.image.highres.convert_all_images_formarts_enabled', 'false');
$format = $this->config->getSystemValueString('memories.image.highres.format', 'jpeg');
if ($highres_enabled == 'true') {
switch ($format) {
case 'jpeg':
if (!\in_array($mimetype, ['image/png', 'image/gif'], true)) {
[$blob, $mimetype] = $this->getImageJPEG($blob, $mimetype);
}
break;
case 'webp':
if (!\in_array($mimetype, ['image/gif'], true)) {
[$blob, $mimetype] = $this->getImageJPEG($blob, $mimetype);
}
break;
}
} else {
if (!\in_array($mimetype, ['image/png', 'image/webp', 'image/jpeg', 'image/gif'], true)) {
[$blob, $mimetype] = $this->getImageJPEG($blob, $mimetype);
}
}

// Return the image
Expand Down Expand Up @@ -416,14 +433,34 @@ private function getImageJPEG($blob, $mimetype): array
} catch (\ImagickException $e) {
throw Exceptions::Forbidden('Imagick failed to read image: '.$e->getMessage());
}

// Convert to JPEG
try {
$image->autoOrient();
$image->setImageFormat('jpeg');
$image->setImageCompressionQuality(85);

$format = $this->config->getSystemValueString('memories.image.highres.format', 'jpeg');
$image->setImageFormat($format);

$quality = (int)$this->config->getSystemValue('memories.image.highres.quality', '95');
$image->setImageCompressionQuality($quality);

// Set maximum width and height
$maxWidth = (int)$this->config->getSystemValue('memories.image.highres_max_x', '0');
$maxHeight = (int)$this->config->getSystemValue('memories.image.highres_max_y', '0');

// Check if the image exceeds the maximum resolution
$width = (int)$image->getImageWidth();
$height = (int)$image->getImageHeight();

if ($maxWidth > 0 && $maxHeight > 0) {
if ($width > $maxWidth || $height > $maxHeight) {
// Resize the image
$image->scaleImage((int)$maxWidth, (int)$maxHeight, true);
}
}

$blob = $image->getImageBlob();
$mimetype = $image->getImageMimeType();

} catch (\ImagickException $e) {
throw Exceptions::Forbidden('Imagick failed to convert image: '.$e->getMessage());
} finally {
Expand Down