Skip to content
Open
Show file tree
Hide file tree
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
Next Next commit
Fixed conflicts in Image.php and mimetypemapping.dist.json
  • Loading branch information
janisplayer committed Apr 11, 2025
commit cd78b74cf956e2ddd948ea88128af6f77c949368
69 changes: 18 additions & 51 deletions lib/private/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ class Image implements IImage {

// Default quality for jpeg images
protected const DEFAULT_JPEG_QUALITY = 80;
protected const DEFAULT_WEBP_QUALITY = 80;
protected const DEFAULT_AVIF_QUALITY = 50;

// Default quality for webp images
protected const DEFAULT_WEBP_QUALITY = 80;

// Default quality for avif images
protected const DEFAULT_AVIF_QUALITY = 50;

// tmp resource.
protected GdImage|false $resource = false;
// Default to png if file type isn't evident.
Expand Down Expand Up @@ -229,12 +230,6 @@ private function _output(?string $filePath = null, ?string $mimeType = null): bo
case 'image/jpeg':
$imageType = IMAGETYPE_JPEG;
break;
case 'image/webp':
$imageType = IMAGETYPE_WEBP;
break;
case 'image/avif':
$imageType = IMAGETYPE_JPEG;
break;
case 'image/png':
$imageType = IMAGETYPE_PNG;
break;
Expand All @@ -248,6 +243,9 @@ private function _output(?string $filePath = null, ?string $mimeType = null): bo
case 'image/webp':
$imageType = IMAGETYPE_WEBP;
break;
case 'image/avif':
$imageType = IMAGETYPE_JPEG;
break;
default:
throw new \Exception('Image::_output(): "' . $mimeType . '" is not supported when forcing a specific output format');
}
Expand All @@ -274,16 +272,6 @@ private function _output(?string $filePath = null, ?string $mimeType = null): bo
imageinterlace($this->resource, true);
$retVal = imagejpeg($this->resource, $filePath, $this->getJpegQuality());
break;
case "image/webp":
/** @psalm-suppress InvalidScalarArgument */
imageinterlace($this->resource, (PHP_VERSION_ID >= 80000 ? true : 1));
$retVal = imagewebp($this->resource, $filePath, $this->getWebpQuality());
break;
case "image/avif":
/** @psalm-suppress InvalidScalarArgument */
imageinterlace($this->resource, (PHP_VERSION_ID >= 80100 ? true : 1));
$retVal = imageavif($this->resource, $filePath, $this->getAvifQuality(), 10);
break;
case IMAGETYPE_PNG:
$retVal = imagepng($this->resource, $filePath);
break;
Expand All @@ -304,6 +292,9 @@ private function _output(?string $filePath = null, ?string $mimeType = null): bo
case IMAGETYPE_WEBP:
$retVal = imagewebp($this->resource, null, $this->getWebpQuality());
break;
case IMAGETYPE_AVIF:
$retVal = imageavif($this->resource, $filePath, $this->getAvifQuality(), 10);
break;
default:
$retVal = imagepng($this->resource, $filePath);
}
Expand Down Expand Up @@ -353,12 +344,11 @@ public function dataMimeType(): ?string {
switch ($this->mimeType) {
case 'image/png':
case 'image/jpeg':
case 'image/webp':
case 'image/avif':
return 'image/jpeg';
case 'image/gif':
case 'image/webp':
return $this->mimeType;
case 'image/avif':
return 'image/jpeg';
default:
return 'image/png';
}
Expand Down Expand Up @@ -388,31 +378,24 @@ public function data(): ?string {
default:
}
}
switch ($imageType) {
case "image/png":
switch ($this->mimeType) {
case 'image/png':
$res = imagepng($this->resource);
break;
case 'image/jpeg':
imageinterlace($this->resource, true);
$quality = $this->getJpegQuality();
$res = imagejpeg($this->resource, null, $quality);
break;
case "image/webp":
/** @psalm-suppress InvalidScalarArgument */
imageinterlace($this->resource, (PHP_VERSION_ID >= 80000 ? true : 1));
$res = imagewebp($this->resource, null, $this->getWebpQuality());
break;
case "image/avif":
/** @psalm-suppress InvalidScalarArgument */
imageinterlace($this->resource, (PHP_VERSION_ID >= 80100 ? true : 1));
$res = imageavif($this->resource, null, $this->getAvifQuality(), 10);
break;
case "image/gif":
case 'image/gif':
$res = imagegif($this->resource);
break;
case 'image/webp':
$res = imagewebp($this->resource, null, $this->getWebpQuality());
break;
case "image/avif":
$res = imageavif($this->resource, null, $this->getAvifQuality(), 10);
break;
default:
$res = imagepng($this->resource);
$this->logger->info('Image->data. Could not guess mime-type, defaulting to png', ['app' => 'core']);
Expand Down Expand Up @@ -441,27 +424,11 @@ protected function getJpegQuality(): int {
return min(100, max(10, $quality));
}

protected function getWebpQuality(): int {
$quality = $this->config->getAppValue('preview', 'webp_quality', (string) self::DEFAULT_WEBP_QUALITY);
// TODO: remove when getAppValue is type safe
if ($quality === null) {
$quality = self::DEFAULT_WEBP_QUALITY;
}
return min(100, max(10, (int) $quality));
}

protected function getAvifQuality(): int {
$quality = $this->config->getAppValue('preview', 'avif_quality', (string) self::DEFAULT_AVIF_QUALITY);
// TODO: remove when getAppValue is type safe
if ($quality === null) {
$quality = self::DEFAULT_AVIF_QUALITY;
}
$quality = $this->config->getAppValue('preview', 'avif_quality', self::DEFAULT_AVIF_QUALITY);
return min(100, max(10, (int) $quality));
}

/**
* @return int
*/
protected function getWebpQuality(): int {
$quality = $this->appConfig->getValueInt('preview', 'webp_quality', self::DEFAULT_WEBP_QUALITY);
return min(100, max(10, $quality));
Expand Down
1 change: 1 addition & 0 deletions resources/config/mimetypemapping.dist.json
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@
"webm": ["video/webm"],
"webp": ["image/webp"],
"avif": ["image/avif"],
"whiteboard": ["application/vnd.excalidraw+json"],
"wmv": ["video/x-ms-wmv"],
"woff": ["application/font-woff"],
"wpd": ["application/vnd.wordperfect"],
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.