From 3fc814edf6ea45251a2afd6a72e2d55784137be8 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Sun, 3 Aug 2025 14:23:17 +0200 Subject: [PATCH] Optimize code --- phpstan.dist.neon | 2 +- src/Colors/AbstractColor.php | 6 ++++++ src/Colors/Cmyk/Color.php | 3 +++ src/Colors/Hsl/Color.php | 8 ++++++++ src/Colors/Hsv/Color.php | 8 ++++++++ src/Colors/Rgb/Decoders/HexColorDecoder.php | 6 +++--- src/Drivers/Gd/Cloner.php | 6 ++++++ src/Drivers/Gd/Driver.php | 13 +++++++++++++ src/Drivers/Gd/Modifiers/ContainModifier.php | 2 ++ src/Drivers/Gd/Modifiers/CoverModifier.php | 2 ++ src/Drivers/Gd/Modifiers/CropModifier.php | 2 ++ src/Drivers/Gd/Modifiers/QuantizeColorsModifier.php | 3 +++ src/Drivers/Gd/Modifiers/ResizeModifier.php | 1 + 13 files changed, 58 insertions(+), 4 deletions(-) diff --git a/phpstan.dist.neon b/phpstan.dist.neon index d4687cf4..fc55fa09 100644 --- a/phpstan.dist.neon +++ b/phpstan.dist.neon @@ -1,5 +1,5 @@ parameters: - level: 6 + level: 7 paths: - src reportUnmatchedIgnoredErrors: false diff --git a/src/Colors/AbstractColor.php b/src/Colors/AbstractColor.php index 9628795e..50d8bec5 100644 --- a/src/Colors/AbstractColor.php +++ b/src/Colors/AbstractColor.php @@ -79,6 +79,8 @@ abstract class AbstractColor implements ColorInterface, Stringable * {@inheritdoc} * * @see ColorInterface::convertTo() + * + * @throws ColorException */ public function convertTo(string|ColorspaceInterface $colorspace): ColorInterface { @@ -87,6 +89,10 @@ abstract class AbstractColor implements ColorInterface, Stringable default => new $colorspace(), }; + if (!($colorspace instanceof ColorspaceInterface)) { + throw new ColorException('Unable to convert to given colorspace.'); + } + return $colorspace->importColor($this); } diff --git a/src/Colors/Cmyk/Color.php b/src/Colors/Cmyk/Color.php index 902e62fe..b077da2a 100644 --- a/src/Colors/Cmyk/Color.php +++ b/src/Colors/Cmyk/Color.php @@ -10,6 +10,7 @@ use Intervention\Image\Colors\Cmyk\Channels\Magenta; use Intervention\Image\Colors\Cmyk\Channels\Yellow; use Intervention\Image\Colors\Cmyk\Channels\Key; use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace; +use Intervention\Image\Exceptions\ColorException; use Intervention\Image\Exceptions\DecoderException; use Intervention\Image\InputHandler; use Intervention\Image\Interfaces\ColorChannelInterface; @@ -72,6 +73,8 @@ class Color extends AbstractColor * {@inheritdoc} * * @see ColorInterface::toHex() + * + * @throws ColorException */ public function toHex(string $prefix = ''): string { diff --git a/src/Colors/Hsl/Color.php b/src/Colors/Hsl/Color.php index 59f57a3b..aaa5810a 100644 --- a/src/Colors/Hsl/Color.php +++ b/src/Colors/Hsl/Color.php @@ -9,6 +9,7 @@ use Intervention\Image\Colors\Hsl\Channels\Hue; use Intervention\Image\Colors\Hsl\Channels\Luminance; use Intervention\Image\Colors\Hsl\Channels\Saturation; use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace; +use Intervention\Image\Exceptions\ColorException; use Intervention\Image\Exceptions\DecoderException; use Intervention\Image\InputHandler; use Intervention\Image\Interfaces\ColorChannelInterface; @@ -93,6 +94,13 @@ class Color extends AbstractColor return $this->channel(Luminance::class); } + /** + * {@inheritdoc} + * + * @see ColorInterface::toHex() + * + * @throws ColorException + */ public function toHex(string $prefix = ''): string { return $this->convertTo(RgbColorspace::class)->toHex($prefix); diff --git a/src/Colors/Hsv/Color.php b/src/Colors/Hsv/Color.php index 14b69607..32e446f1 100644 --- a/src/Colors/Hsv/Color.php +++ b/src/Colors/Hsv/Color.php @@ -9,6 +9,7 @@ use Intervention\Image\Colors\Hsv\Channels\Hue; use Intervention\Image\Colors\Hsv\Channels\Saturation; use Intervention\Image\Colors\Hsv\Channels\Value; use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace; +use Intervention\Image\Exceptions\ColorException; use Intervention\Image\Exceptions\DecoderException; use Intervention\Image\InputHandler; use Intervention\Image\Interfaces\ColorChannelInterface; @@ -93,6 +94,13 @@ class Color extends AbstractColor return $this->channel(Value::class); } + /** + * {@inheritdoc} + * + * @see ColorInterface::toHex() + * + * @throws ColorException + */ public function toHex(string $prefix = ''): string { return $this->convertTo(RgbColorspace::class)->toHex($prefix); diff --git a/src/Colors/Rgb/Decoders/HexColorDecoder.php b/src/Colors/Rgb/Decoders/HexColorDecoder.php index b19eee2a..93eaecfc 100644 --- a/src/Colors/Rgb/Decoders/HexColorDecoder.php +++ b/src/Colors/Rgb/Decoders/HexColorDecoder.php @@ -33,10 +33,10 @@ class HexColorDecoder extends AbstractDecoder implements DecoderInterface default => throw new DecoderException('Unable to decode input'), }; - $values = array_map(function (string $value): float|int { + $values = array_map(function (string $value): int { return match (strlen($value)) { - 1 => hexdec($value . $value), - 2 => hexdec($value), + 1 => (int) hexdec($value . $value), + 2 => (int) hexdec($value), default => throw new DecoderException('Unable to decode input'), }; }, $values); diff --git a/src/Drivers/Gd/Cloner.php b/src/Drivers/Gd/Cloner.php index e8719963..5cd6a636 100644 --- a/src/Drivers/Gd/Cloner.php +++ b/src/Drivers/Gd/Cloner.php @@ -8,6 +8,7 @@ use GdImage; use Intervention\Image\Colors\Rgb\Channels\Alpha; use Intervention\Image\Colors\Rgb\Color; use Intervention\Image\Exceptions\ColorException; +use Intervention\Image\Exceptions\GeometryException; use Intervention\Image\Geometry\Rectangle; use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\SizeInterface; @@ -38,6 +39,7 @@ class Cloner * background color. * * @throws ColorException + * @throws GeometryException */ public static function cloneEmpty( GdImage $gd, @@ -47,6 +49,10 @@ class Cloner // define size $size = $size ?: new Rectangle(imagesx($gd), imagesy($gd)); + if ($size->width() < 1 || $size->height() < 1) { + throw new GeometryException('Invalid image size.'); + } + // create new gd image with same size or new given size $clone = imagecreatetruecolor($size->width(), $size->height()); diff --git a/src/Drivers/Gd/Driver.php b/src/Drivers/Gd/Driver.php index 6d20ef8a..a502bc36 100644 --- a/src/Drivers/Gd/Driver.php +++ b/src/Drivers/Gd/Driver.php @@ -6,6 +6,8 @@ namespace Intervention\Image\Drivers\Gd; use Intervention\Image\Drivers\AbstractDriver; use Intervention\Image\Exceptions\DriverException; +use Intervention\Image\Exceptions\GeometryException; +use Intervention\Image\Exceptions\RuntimeException; use Intervention\Image\Format; use Intervention\Image\FileExtension; use Intervention\Image\Image; @@ -47,13 +49,24 @@ class Driver extends AbstractDriver * {@inheritdoc} * * @see DriverInterface::createImage() + * + * @throws RuntimeException + * @throws GeometryException */ public function createImage(int $width, int $height): ImageInterface { + if ($width < 1 || $height < 1) { + throw new GeometryException('Invalid image size. Only use int<1, max>.'); + } + // build new transparent GDImage $data = imagecreatetruecolor($width, $height); imagesavealpha($data, true); $background = imagecolorallocatealpha($data, 255, 255, 255, 127); + if ($background === false) { + throw new RuntimeException('Unable to create initial background color color.'); + } + imagealphablending($data, false); imagefill($data, 0, 0, $background); imagecolortransparent($data, $background); diff --git a/src/Drivers/Gd/Modifiers/ContainModifier.php b/src/Drivers/Gd/Modifiers/ContainModifier.php index e79f8710..526186c0 100644 --- a/src/Drivers/Gd/Modifiers/ContainModifier.php +++ b/src/Drivers/Gd/Modifiers/ContainModifier.php @@ -9,6 +9,7 @@ use Intervention\Image\Colors\Rgb\Channels\Green; use Intervention\Image\Colors\Rgb\Channels\Red; use Intervention\Image\Drivers\Gd\Cloner; use Intervention\Image\Exceptions\ColorException; +use Intervention\Image\Exceptions\RuntimeException; use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\FrameInterface; use Intervention\Image\Interfaces\ImageInterface; @@ -38,6 +39,7 @@ class ContainModifier extends GenericContainModifier implements SpecializedInter /** * @throws ColorException + * @throws RuntimeException */ protected function modify( FrameInterface $frame, diff --git a/src/Drivers/Gd/Modifiers/CoverModifier.php b/src/Drivers/Gd/Modifiers/CoverModifier.php index e676e663..3295fd7a 100644 --- a/src/Drivers/Gd/Modifiers/CoverModifier.php +++ b/src/Drivers/Gd/Modifiers/CoverModifier.php @@ -6,6 +6,7 @@ namespace Intervention\Image\Drivers\Gd\Modifiers; use Intervention\Image\Drivers\Gd\Cloner; use Intervention\Image\Exceptions\ColorException; +use Intervention\Image\Exceptions\RuntimeException; use Intervention\Image\Interfaces\FrameInterface; use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\SizeInterface; @@ -33,6 +34,7 @@ class CoverModifier extends GenericCoverModifier implements SpecializedInterface /** * @throws ColorException + * @throws RuntimeException */ protected function modifyFrame(FrameInterface $frame, SizeInterface $crop, SizeInterface $resize): void { diff --git a/src/Drivers/Gd/Modifiers/CropModifier.php b/src/Drivers/Gd/Modifiers/CropModifier.php index 78fed75d..2e0357fe 100644 --- a/src/Drivers/Gd/Modifiers/CropModifier.php +++ b/src/Drivers/Gd/Modifiers/CropModifier.php @@ -6,6 +6,7 @@ namespace Intervention\Image\Drivers\Gd\Modifiers; use Intervention\Image\Drivers\Gd\Cloner; use Intervention\Image\Exceptions\ColorException; +use Intervention\Image\Exceptions\RuntimeException; use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\FrameInterface; use Intervention\Image\Interfaces\ImageInterface; @@ -35,6 +36,7 @@ class CropModifier extends GenericCropModifier implements SpecializedInterface /** * @throws ColorException + * @throws RuntimeException */ protected function cropFrame( FrameInterface $frame, diff --git a/src/Drivers/Gd/Modifiers/QuantizeColorsModifier.php b/src/Drivers/Gd/Modifiers/QuantizeColorsModifier.php index a04af703..9c8637d2 100644 --- a/src/Drivers/Gd/Modifiers/QuantizeColorsModifier.php +++ b/src/Drivers/Gd/Modifiers/QuantizeColorsModifier.php @@ -6,6 +6,7 @@ namespace Intervention\Image\Drivers\Gd\Modifiers; use Intervention\Image\Drivers\Gd\Cloner; use Intervention\Image\Exceptions\InputException; +use Intervention\Image\Exceptions\RuntimeException; use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\SpecializedInterface; use Intervention\Image\Modifiers\QuantizeColorsModifier as GenericQuantizeColorsModifier; @@ -16,6 +17,8 @@ class QuantizeColorsModifier extends GenericQuantizeColorsModifier implements Sp * {@inheritdoc} * * @see ModifierInterface::apply() + * + * @throws RuntimeException */ public function apply(ImageInterface $image): ImageInterface { diff --git a/src/Drivers/Gd/Modifiers/ResizeModifier.php b/src/Drivers/Gd/Modifiers/ResizeModifier.php index cfaa295d..5466551f 100644 --- a/src/Drivers/Gd/Modifiers/ResizeModifier.php +++ b/src/Drivers/Gd/Modifiers/ResizeModifier.php @@ -33,6 +33,7 @@ class ResizeModifier extends GenericResizeModifier implements SpecializedInterfa /** * @throws ColorException + * @throws RuntimeException */ private function resizeFrame(FrameInterface $frame, SizeInterface $resizeTo): void {