1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-31 09:31:53 +02:00

Optimize code

This commit is contained in:
Oliver Vogel
2025-08-03 14:23:17 +02:00
parent e725ab2129
commit 3fc814edf6
13 changed files with 58 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
parameters: parameters:
level: 6 level: 7
paths: paths:
- src - src
reportUnmatchedIgnoredErrors: false reportUnmatchedIgnoredErrors: false

View File

@@ -79,6 +79,8 @@ abstract class AbstractColor implements ColorInterface, Stringable
* {@inheritdoc} * {@inheritdoc}
* *
* @see ColorInterface::convertTo() * @see ColorInterface::convertTo()
*
* @throws ColorException
*/ */
public function convertTo(string|ColorspaceInterface $colorspace): ColorInterface public function convertTo(string|ColorspaceInterface $colorspace): ColorInterface
{ {
@@ -87,6 +89,10 @@ abstract class AbstractColor implements ColorInterface, Stringable
default => new $colorspace(), default => new $colorspace(),
}; };
if (!($colorspace instanceof ColorspaceInterface)) {
throw new ColorException('Unable to convert to given colorspace.');
}
return $colorspace->importColor($this); return $colorspace->importColor($this);
} }

View File

@@ -10,6 +10,7 @@ use Intervention\Image\Colors\Cmyk\Channels\Magenta;
use Intervention\Image\Colors\Cmyk\Channels\Yellow; use Intervention\Image\Colors\Cmyk\Channels\Yellow;
use Intervention\Image\Colors\Cmyk\Channels\Key; use Intervention\Image\Colors\Cmyk\Channels\Key;
use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace; use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace;
use Intervention\Image\Exceptions\ColorException;
use Intervention\Image\Exceptions\DecoderException; use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\InputHandler; use Intervention\Image\InputHandler;
use Intervention\Image\Interfaces\ColorChannelInterface; use Intervention\Image\Interfaces\ColorChannelInterface;
@@ -72,6 +73,8 @@ class Color extends AbstractColor
* {@inheritdoc} * {@inheritdoc}
* *
* @see ColorInterface::toHex() * @see ColorInterface::toHex()
*
* @throws ColorException
*/ */
public function toHex(string $prefix = ''): string public function toHex(string $prefix = ''): string
{ {

View File

@@ -9,6 +9,7 @@ use Intervention\Image\Colors\Hsl\Channels\Hue;
use Intervention\Image\Colors\Hsl\Channels\Luminance; use Intervention\Image\Colors\Hsl\Channels\Luminance;
use Intervention\Image\Colors\Hsl\Channels\Saturation; use Intervention\Image\Colors\Hsl\Channels\Saturation;
use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace; use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace;
use Intervention\Image\Exceptions\ColorException;
use Intervention\Image\Exceptions\DecoderException; use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\InputHandler; use Intervention\Image\InputHandler;
use Intervention\Image\Interfaces\ColorChannelInterface; use Intervention\Image\Interfaces\ColorChannelInterface;
@@ -93,6 +94,13 @@ class Color extends AbstractColor
return $this->channel(Luminance::class); return $this->channel(Luminance::class);
} }
/**
* {@inheritdoc}
*
* @see ColorInterface::toHex()
*
* @throws ColorException
*/
public function toHex(string $prefix = ''): string public function toHex(string $prefix = ''): string
{ {
return $this->convertTo(RgbColorspace::class)->toHex($prefix); return $this->convertTo(RgbColorspace::class)->toHex($prefix);

View File

@@ -9,6 +9,7 @@ use Intervention\Image\Colors\Hsv\Channels\Hue;
use Intervention\Image\Colors\Hsv\Channels\Saturation; use Intervention\Image\Colors\Hsv\Channels\Saturation;
use Intervention\Image\Colors\Hsv\Channels\Value; use Intervention\Image\Colors\Hsv\Channels\Value;
use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace; use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace;
use Intervention\Image\Exceptions\ColorException;
use Intervention\Image\Exceptions\DecoderException; use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\InputHandler; use Intervention\Image\InputHandler;
use Intervention\Image\Interfaces\ColorChannelInterface; use Intervention\Image\Interfaces\ColorChannelInterface;
@@ -93,6 +94,13 @@ class Color extends AbstractColor
return $this->channel(Value::class); return $this->channel(Value::class);
} }
/**
* {@inheritdoc}
*
* @see ColorInterface::toHex()
*
* @throws ColorException
*/
public function toHex(string $prefix = ''): string public function toHex(string $prefix = ''): string
{ {
return $this->convertTo(RgbColorspace::class)->toHex($prefix); return $this->convertTo(RgbColorspace::class)->toHex($prefix);

View File

@@ -33,10 +33,10 @@ class HexColorDecoder extends AbstractDecoder implements DecoderInterface
default => throw new DecoderException('Unable to decode input'), 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)) { return match (strlen($value)) {
1 => hexdec($value . $value), 1 => (int) hexdec($value . $value),
2 => hexdec($value), 2 => (int) hexdec($value),
default => throw new DecoderException('Unable to decode input'), default => throw new DecoderException('Unable to decode input'),
}; };
}, $values); }, $values);

View File

@@ -8,6 +8,7 @@ use GdImage;
use Intervention\Image\Colors\Rgb\Channels\Alpha; use Intervention\Image\Colors\Rgb\Channels\Alpha;
use Intervention\Image\Colors\Rgb\Color; use Intervention\Image\Colors\Rgb\Color;
use Intervention\Image\Exceptions\ColorException; use Intervention\Image\Exceptions\ColorException;
use Intervention\Image\Exceptions\GeometryException;
use Intervention\Image\Geometry\Rectangle; use Intervention\Image\Geometry\Rectangle;
use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\SizeInterface; use Intervention\Image\Interfaces\SizeInterface;
@@ -38,6 +39,7 @@ class Cloner
* background color. * background color.
* *
* @throws ColorException * @throws ColorException
* @throws GeometryException
*/ */
public static function cloneEmpty( public static function cloneEmpty(
GdImage $gd, GdImage $gd,
@@ -47,6 +49,10 @@ class Cloner
// define size // define size
$size = $size ?: new Rectangle(imagesx($gd), imagesy($gd)); $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 // create new gd image with same size or new given size
$clone = imagecreatetruecolor($size->width(), $size->height()); $clone = imagecreatetruecolor($size->width(), $size->height());

View File

@@ -6,6 +6,8 @@ namespace Intervention\Image\Drivers\Gd;
use Intervention\Image\Drivers\AbstractDriver; use Intervention\Image\Drivers\AbstractDriver;
use Intervention\Image\Exceptions\DriverException; use Intervention\Image\Exceptions\DriverException;
use Intervention\Image\Exceptions\GeometryException;
use Intervention\Image\Exceptions\RuntimeException;
use Intervention\Image\Format; use Intervention\Image\Format;
use Intervention\Image\FileExtension; use Intervention\Image\FileExtension;
use Intervention\Image\Image; use Intervention\Image\Image;
@@ -47,13 +49,24 @@ class Driver extends AbstractDriver
* {@inheritdoc} * {@inheritdoc}
* *
* @see DriverInterface::createImage() * @see DriverInterface::createImage()
*
* @throws RuntimeException
* @throws GeometryException
*/ */
public function createImage(int $width, int $height): ImageInterface 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 // build new transparent GDImage
$data = imagecreatetruecolor($width, $height); $data = imagecreatetruecolor($width, $height);
imagesavealpha($data, true); imagesavealpha($data, true);
$background = imagecolorallocatealpha($data, 255, 255, 255, 127); $background = imagecolorallocatealpha($data, 255, 255, 255, 127);
if ($background === false) {
throw new RuntimeException('Unable to create initial background color color.');
}
imagealphablending($data, false); imagealphablending($data, false);
imagefill($data, 0, 0, $background); imagefill($data, 0, 0, $background);
imagecolortransparent($data, $background); imagecolortransparent($data, $background);

View File

@@ -9,6 +9,7 @@ use Intervention\Image\Colors\Rgb\Channels\Green;
use Intervention\Image\Colors\Rgb\Channels\Red; use Intervention\Image\Colors\Rgb\Channels\Red;
use Intervention\Image\Drivers\Gd\Cloner; use Intervention\Image\Drivers\Gd\Cloner;
use Intervention\Image\Exceptions\ColorException; use Intervention\Image\Exceptions\ColorException;
use Intervention\Image\Exceptions\RuntimeException;
use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\FrameInterface; use Intervention\Image\Interfaces\FrameInterface;
use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\ImageInterface;
@@ -38,6 +39,7 @@ class ContainModifier extends GenericContainModifier implements SpecializedInter
/** /**
* @throws ColorException * @throws ColorException
* @throws RuntimeException
*/ */
protected function modify( protected function modify(
FrameInterface $frame, FrameInterface $frame,

View File

@@ -6,6 +6,7 @@ namespace Intervention\Image\Drivers\Gd\Modifiers;
use Intervention\Image\Drivers\Gd\Cloner; use Intervention\Image\Drivers\Gd\Cloner;
use Intervention\Image\Exceptions\ColorException; use Intervention\Image\Exceptions\ColorException;
use Intervention\Image\Exceptions\RuntimeException;
use Intervention\Image\Interfaces\FrameInterface; use Intervention\Image\Interfaces\FrameInterface;
use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\SizeInterface; use Intervention\Image\Interfaces\SizeInterface;
@@ -33,6 +34,7 @@ class CoverModifier extends GenericCoverModifier implements SpecializedInterface
/** /**
* @throws ColorException * @throws ColorException
* @throws RuntimeException
*/ */
protected function modifyFrame(FrameInterface $frame, SizeInterface $crop, SizeInterface $resize): void protected function modifyFrame(FrameInterface $frame, SizeInterface $crop, SizeInterface $resize): void
{ {

View File

@@ -6,6 +6,7 @@ namespace Intervention\Image\Drivers\Gd\Modifiers;
use Intervention\Image\Drivers\Gd\Cloner; use Intervention\Image\Drivers\Gd\Cloner;
use Intervention\Image\Exceptions\ColorException; use Intervention\Image\Exceptions\ColorException;
use Intervention\Image\Exceptions\RuntimeException;
use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\FrameInterface; use Intervention\Image\Interfaces\FrameInterface;
use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\ImageInterface;
@@ -35,6 +36,7 @@ class CropModifier extends GenericCropModifier implements SpecializedInterface
/** /**
* @throws ColorException * @throws ColorException
* @throws RuntimeException
*/ */
protected function cropFrame( protected function cropFrame(
FrameInterface $frame, FrameInterface $frame,

View File

@@ -6,6 +6,7 @@ namespace Intervention\Image\Drivers\Gd\Modifiers;
use Intervention\Image\Drivers\Gd\Cloner; use Intervention\Image\Drivers\Gd\Cloner;
use Intervention\Image\Exceptions\InputException; use Intervention\Image\Exceptions\InputException;
use Intervention\Image\Exceptions\RuntimeException;
use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\SpecializedInterface; use Intervention\Image\Interfaces\SpecializedInterface;
use Intervention\Image\Modifiers\QuantizeColorsModifier as GenericQuantizeColorsModifier; use Intervention\Image\Modifiers\QuantizeColorsModifier as GenericQuantizeColorsModifier;
@@ -16,6 +17,8 @@ class QuantizeColorsModifier extends GenericQuantizeColorsModifier implements Sp
* {@inheritdoc} * {@inheritdoc}
* *
* @see ModifierInterface::apply() * @see ModifierInterface::apply()
*
* @throws RuntimeException
*/ */
public function apply(ImageInterface $image): ImageInterface public function apply(ImageInterface $image): ImageInterface
{ {

View File

@@ -33,6 +33,7 @@ class ResizeModifier extends GenericResizeModifier implements SpecializedInterfa
/** /**
* @throws ColorException * @throws ColorException
* @throws RuntimeException
*/ */
private function resizeFrame(FrameInterface $frame, SizeInterface $resizeTo): void private function resizeFrame(FrameInterface $frame, SizeInterface $resizeTo): void
{ {