diff --git a/.phpstorm.meta.php b/.phpstorm.meta.php index 7810b4cc1..4d8fe7a20 100644 --- a/.phpstorm.meta.php +++ b/.phpstorm.meta.php @@ -10,8 +10,8 @@ override(\Nette\Utils\Arrays::toObject(0), type(1)); expectedArguments(\Nette\Utils\Arrays::grep(), 2, PREG_GREP_INVERT); -expectedArguments(\Nette\Utils\Image::resize(), 2, \Nette\Utils\Image::SHRINK_ONLY, \Nette\Utils\Image::STRETCH, \Nette\Utils\Image::FIT, \Nette\Utils\Image::FILL, \Nette\Utils\Image::EXACT); -expectedArguments(\Nette\Utils\Image::calculateSize(), 4, \Nette\Utils\Image::SHRINK_ONLY, \Nette\Utils\Image::STRETCH, \Nette\Utils\Image::FIT, \Nette\Utils\Image::FILL, \Nette\Utils\Image::EXACT); +expectedArguments(\Nette\Utils\Image::resize(), 2, \Nette\Utils\Image::ShrinkOnly, \Nette\Utils\Image::Stretch, \Nette\Utils\Image::Fit, \Nette\Utils\Image::Fill, \Nette\Utils\Image::Exact); +expectedArguments(\Nette\Utils\Image::calculateSize(), 4, \Nette\Utils\Image::ShrinkOnly, \Nette\Utils\Image::Stretch, \Nette\Utils\Image::Fit, \Nette\Utils\Image::Fill, \Nette\Utils\Image::Exact); expectedArguments(\Nette\Utils\Json::encode(), 1, \Nette\Utils\Json::PRETTY); expectedArguments(\Nette\Utils\Json::decode(), 1, \Nette\Utils\Json::FORCE_ARRAY); expectedArguments(\Nette\Utils\Strings::split(), 2, \PREG_SPLIT_NO_EMPTY | \PREG_OFFSET_CAPTURE); diff --git a/src/Utils/Image.php b/src/Utils/Image.php index 858629883..f62c0f311 100644 --- a/src/Utils/Image.php +++ b/src/Utils/Image.php @@ -99,19 +99,27 @@ class Image use Nette\SmartObject; /** {@link resize()} only shrinks images */ - public const SHRINK_ONLY = 0b0001; + public const ShrinkOnly = 0b0001; /** {@link resize()} will ignore aspect ratio */ - public const STRETCH = 0b0010; + public const Stretch = 0b0010; /** {@link resize()} fits in given area so its dimensions are less than or equal to the required dimensions */ - public const FIT = 0b0000; + public const Fit = 0b0000; /** {@link resize()} fills given area so its dimensions are greater than or equal to the required dimensions */ - public const FILL = 0b0100; + public const Fill = 0b0100; /** {@link resize()} fills given area exactly */ - public const EXACT = 0b1000; + public const Exact = 0b1000; + + /** deprecated */ + public const SHRINK_ONLY = self::ShrinkOnly; + public const STRETCH = self::Stretch; + public const FIT = self::Fit; + public const FILL = self::Fill; + public const EXACT = self::Exact; + public const EMPTY_GIF = self::EmptyGIF; /** image types */ public const @@ -122,7 +130,7 @@ class Image AVIF = 19, // IMAGETYPE_AVIF, BMP = IMAGETYPE_BMP; - public const EMPTY_GIF = "GIF89a\x01\x00\x01\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00!\xf9\x04\x01\x00\x00\x00\x00,\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01\x00;"; + public const EmptyGIF = "GIF89a\x01\x00\x01\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00!\xf9\x04\x01\x00\x00\x00\x00,\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01\x00;"; private const Formats = [self::JPEG => 'jpeg', self::PNG => 'png', self::GIF => 'gif', self::WEBP => 'webp', self::AVIF => 'avif', self::BMP => 'bmp']; @@ -349,10 +357,10 @@ public function getImageResource() * @param int|string|null $height in pixels or percent * @return static */ - public function resize($width, $height, int $flags = self::FIT) + public function resize($width, $height, int $flags = self::Fit) { - if ($flags & self::EXACT) { - return $this->resize($width, $height, self::FILL)->crop('50%', '50%', $width, $height); + if ($flags & self::Exact) { + return $this->resize($width, $height, self::Fill)->crop('50%', '50%', $width, $height); } [$newWidth, $newHeight] = static::calculateSize($this->getWidth(), $this->getHeight(), $width, $height, $flags); @@ -392,7 +400,7 @@ public static function calculateSize( int $srcHeight, $newWidth, $newHeight, - int $flags = self::FIT + int $flags = self::Fit ): array { if ($newWidth === null) { @@ -406,17 +414,17 @@ public static function calculateSize( if ($newHeight === null) { } elseif (self::isPercent($newHeight)) { $newHeight = (int) round($srcHeight / 100 * abs($newHeight)); - $flags |= empty($percents) ? 0 : self::STRETCH; + $flags |= empty($percents) ? 0 : self::Stretch; } else { $newHeight = abs($newHeight); } - if ($flags & self::STRETCH) { // non-proportional + if ($flags & self::Stretch) { // non-proportional if (!$newWidth || !$newHeight) { throw new Nette\InvalidArgumentException('For stretching must be both width and height specified.'); } - if ($flags & self::SHRINK_ONLY) { + if ($flags & self::ShrinkOnly) { $newWidth = (int) round($srcWidth * min(1, $newWidth / $srcWidth)); $newHeight = (int) round($srcHeight * min(1, $newHeight / $srcHeight)); } @@ -434,11 +442,11 @@ public static function calculateSize( $scale[] = $newHeight / $srcHeight; } - if ($flags & self::FILL) { + if ($flags & self::Fill) { $scale = [max($scale)]; } - if ($flags & self::SHRINK_ONLY) { + if ($flags & self::ShrinkOnly) { $scale[] = 1; } diff --git a/src/Utils/Strings.php b/src/Utils/Strings.php index 1314c62fc..a1f83bf41 100644 --- a/src/Utils/Strings.php +++ b/src/Utils/Strings.php @@ -20,7 +20,8 @@ class Strings { use Nette\StaticClass; - public const TRIM_CHARACTERS = " \t\n\r\0\x0B\u{A0}"; + public const TrimCharacters = " \t\n\r\0\x0B\u{A0}"; + public const TRIM_CHARACTERS = self::TrimCharacters; /** @@ -366,7 +367,7 @@ public static function length(string $s): int /** * Removes all left and right side spaces (or the characters passed as second argument) from a UTF-8 encoded string. */ - public static function trim(string $s, string $charlist = self::TRIM_CHARACTERS): string + public static function trim(string $s, string $charlist = self::TrimCharacters): string { $charlist = preg_quote($charlist, '#'); return self::replace($s, '#^[' . $charlist . ']+|[' . $charlist . ']+$#Du', ''); diff --git a/tests/Utils/Image.factories.phpt b/tests/Utils/Image.factories.phpt index 1ec081b29..b3d599d9d 100644 --- a/tests/Utils/Image.factories.phpt +++ b/tests/Utils/Image.factories.phpt @@ -76,12 +76,12 @@ test('', function () { test('', function () { - $image = Image::fromString(Image::EMPTY_GIF, $format); + $image = Image::fromString(Image::EmptyGIF, $format); Assert::same(1, $image->getWidth()); Assert::same(1, $image->getHeight()); Assert::same(Image::GIF, $format); - Assert::same(Image::GIF, Image::detectTypeFromString(Image::EMPTY_GIF, $w, $h)); + Assert::same(Image::GIF, Image::detectTypeFromString(Image::EmptyGIF, $w, $h)); Assert::same(1, $w); Assert::same(1, $h); }); diff --git a/tests/Utils/Image.resize.phpt b/tests/Utils/Image.resize.phpt index 474169068..a75995eb0 100644 --- a/tests/Utils/Image.resize.phpt +++ b/tests/Utils/Image.resize.phpt @@ -38,7 +38,7 @@ test('resizing X', function () use ($main) { test('resizing Y shrink', function () use ($main) { $image = clone $main; - $image->resize(null, 150, Image::SHRINK_ONLY); + $image->resize(null, 150, Image::ShrinkOnly); Assert::same(176, $image->width); Assert::same(104, $image->height); }); @@ -46,7 +46,7 @@ test('resizing Y shrink', function () use ($main) { test('resizing X Y shrink', function () use ($main) { $image = clone $main; - $image->resize(300, 150, Image::SHRINK_ONLY); + $image->resize(300, 150, Image::ShrinkOnly); Assert::same(176, $image->width); Assert::same(104, $image->height); }); @@ -62,7 +62,7 @@ test('resizing X Y', function () use ($main) { test('resizing X Y stretch', function () use ($main) { $image = clone $main; - $image->resize(300, 100, Image::STRETCH); + $image->resize(300, 100, Image::Stretch); Assert::same(300, $image->width); Assert::same(100, $image->height); }); @@ -70,7 +70,7 @@ test('resizing X Y stretch', function () use ($main) { test('resizing X Y shrink stretch', function () use ($main) { $image = clone $main; - $image->resize(300, 100, Image::SHRINK_ONLY | Image::STRETCH); + $image->resize(300, 100, Image::ShrinkOnly | Image::Stretch); Assert::same(176, $image->width); Assert::same(100, $image->height); }); @@ -102,7 +102,7 @@ test('flipping X', function () use ($main) { test('flipping Y shrink', function () use ($main) { $image = clone $main; - $image->resize(null, -150, Image::SHRINK_ONLY); + $image->resize(null, -150, Image::ShrinkOnly); Assert::same(176, $image->width); Assert::same(104, $image->height); }); @@ -110,7 +110,7 @@ test('flipping Y shrink', function () use ($main) { test('flipping X Y shrink', function () use ($main) { $image = clone $main; - $image->resize(-300, -150, Image::SHRINK_ONLY); + $image->resize(-300, -150, Image::ShrinkOnly); Assert::same(176, $image->width); Assert::same(104, $image->height); }); @@ -118,7 +118,7 @@ test('flipping X Y shrink', function () use ($main) { test('exact resize', function () use ($main) { $image = clone $main; - $image->resize(300, 150, Image::EXACT); + $image->resize(300, 150, Image::Exact); Assert::same(300, $image->width); Assert::same(150, $image->height); });