From 3c89c7d4f4796323e4ec63a9d50f3ae6d6bf5fe7 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Mon, 13 Oct 2025 18:43:13 +0200 Subject: [PATCH] Add ImageQuality class to hold quality values --- Zotlabs/Photo/ImageQuality.php | 49 +++++++++++++++++++++++++++ tests/unit/Photo/ImageQualityTest.php | 49 +++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 Zotlabs/Photo/ImageQuality.php create mode 100644 tests/unit/Photo/ImageQualityTest.php diff --git a/Zotlabs/Photo/ImageQuality.php b/Zotlabs/Photo/ImageQuality.php new file mode 100644 index 000000000..9078a269a --- /dev/null +++ b/Zotlabs/Photo/ImageQuality.php @@ -0,0 +1,49 @@ + + * + * SPDX-License-Identifier: MIT + */ + +namespace Zotlabs\Photo; + +/** + * A class to represent image quality values. + */ +class ImageQuality +{ + readonly string $mimeType; + readonly int $value; + + private const DEFAULT_VALUE = [ + 'image/jpg' => JPEG_QUALITY, + 'image/png' => PNG_QUALITY, + 'image/avif' => AVIF_QUALITY, + 'image/webp' => WEBP_QUALITY, + ]; + + public function __construct(string $mimeType, int $value) { + [ $min, $max ] = $this->validRange($mimeType); + + if ($value < $min || $value > $max) { + $value = self::DEFAULT_VALUE[$mimeType]; + } + + $this->value = $value; + $this->mimeType = $mimeType; + } + + /** + * Return the valid quality setting range for the given mime type. + * + * @param string $mimeType The mime type whose range to return. + * + * @return The valid range as an array with two elements as + * [ $min, $max ]. + */ + public static function validRange(string $mimeType): array { + // Max quality for PNG is 10, for all others it's 100. + return $mimeType === 'image/png' ? [ 0, 10 ] : [ 1, 100 ]; + } +} diff --git a/tests/unit/Photo/ImageQualityTest.php b/tests/unit/Photo/ImageQualityTest.php new file mode 100644 index 000000000..cb4b9994f --- /dev/null +++ b/tests/unit/Photo/ImageQualityTest.php @@ -0,0 +1,49 @@ + + * + * SPDX-License-Identifier: MIT + */ + +namespace Zotlabs\Tests\Unit\Photo; + +use PHPUnit\Framework\Attributes\TestWith; +use Zotlabs\Photo\ImageQuality; +use Zotlabs\Tests\Unit\UnitTestCase; + +class ImageQualityTest extends UnitTestCase +{ + private const DEFAULT_VALUE = [ + 'image/jpg' => JPEG_QUALITY, + 'image/png' => PNG_QUALITY, + 'image/avif' => AVIF_QUALITY, + 'image/webp' => WEBP_QUALITY, + ]; + + #[TestWith(['image/jpg', 55])] + #[TestWith(['image/jpg', 100])] + #[TestWith(['image/png', 8])] + #[TestWith(['image/png', 0])] + #[TestWith(['image/avif', 99])] + #[TestWith(['image/webp', 1])] + public function testConstructWithValidValues(string $mimeType, int $value): void { + $q = new ImageQuality($mimeType, $value); + $this->assertEquals($value, $q->value); + } + + #[TestWith(['image/jpg', 0])] + #[TestWith(['image/jpg', 101])] + #[TestWith(['image/png', -1])] + #[TestWith(['image/png', 11])] + #[TestWith(['image/avif', 0])] + #[TestWith(['image/avif', 101])] + #[TestWith(['image/webp', 0])] + #[TestWith(['image/webp', 101])] + public function testConstructWithOutOfBoundsValueReplacedWithDefault( + string $mimeType, int $value + ): void { + $q = new ImageQuality($mimeType, $value); + $this->assertEquals(self::DEFAULT_VALUE[$mimeType], $q->value); + } +}