Skip to content

Commit

Permalink
Merge branch 'develop' into feature/php-8.4
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Apr 23, 2024
2 parents 2d5acba + 33cbb21 commit c7fc478
Show file tree
Hide file tree
Showing 11 changed files with 287 additions and 124 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yml
Expand Up @@ -18,7 +18,7 @@ services:
analysis:
build: ./
working_dir: /project
command: bash -c "composer install && ./vendor/bin/phpstan analyze --memory-limit=512M --level=4 ./src"
command: bash -c "composer install && ./vendor/bin/phpstan analyze --memory-limit=512M ./src"
volumes:
- ./:/project
standards:
Expand Down
2 changes: 1 addition & 1 deletion phpstan.dist.neon
@@ -1,5 +1,5 @@
parameters:
level: 4
level: 5
paths:
- src
exceptions:
Expand Down
18 changes: 14 additions & 4 deletions src/Colors/Cmyk/Colorspace.php
Expand Up @@ -9,6 +9,7 @@
use Intervention\Image\Colors\Hsv\Color as HsvColor;
use Intervention\Image\Colors\Hsl\Color as HslColor;
use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace;
use Intervention\Image\Exceptions\ColorException;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\ColorspaceInterface;

Expand Down Expand Up @@ -36,9 +37,9 @@ public function colorFromNormalized(array $normalized): ColorInterface
}

/**
* {@inheritdoc}
*
* @see ColorspaceInterface::importColor()
* @param ColorInterface $color
* @return ColorInterface
* @throws ColorException
*/
public function importColor(ColorInterface $color): ColorInterface
{
Expand All @@ -50,8 +51,17 @@ public function importColor(ColorInterface $color): ColorInterface
};
}

protected function importRgbColor(RgbColor $color): CmykColor
/**
* @param ColorInterface $color
* @return Color
* @throws ColorException
*/
protected function importRgbColor(ColorInterface $color): CmykColor
{
if (!($color instanceof RgbColor)) {
throw new ColorException('Unabled to import color of type ' . $color::class . '.');
}

$c = (255 - $color->red()->value()) / 255.0 * 100;
$m = (255 - $color->green()->value()) / 255.0 * 100;
$y = (255 - $color->blue()->value()) / 255.0 * 100;
Expand Down
28 changes: 26 additions & 2 deletions src/Colors/Hsl/Colorspace.php
Expand Up @@ -8,6 +8,7 @@
use Intervention\Image\Colors\Rgb\Color as RgbColor;
use Intervention\Image\Colors\Hsv\Color as HsvColor;
use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace;
use Intervention\Image\Exceptions\ColorException;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\ColorspaceInterface;

Expand All @@ -33,6 +34,11 @@ public function colorFromNormalized(array $normalized): ColorInterface
return new Color(...$values);
}

/**
* @param ColorInterface $color
* @return ColorInterface
* @throws ColorException
*/
public function importColor(ColorInterface $color): ColorInterface
{
return match ($color::class) {
Expand All @@ -43,8 +49,17 @@ public function importColor(ColorInterface $color): ColorInterface
};
}

protected function importRgbColor(RgbColor $color): ColorInterface
/**
* @param ColorInterface $color
* @return ColorInterface
* @throws ColorException
*/
protected function importRgbColor(ColorInterface $color): ColorInterface
{
if (!($color instanceof RgbColor)) {
throw new ColorException('Unabled to import color of type ' . $color::class . '.');
}

// normalized values of rgb channels
$values = array_map(function ($channel) {
return $channel->normalize();
Expand Down Expand Up @@ -84,8 +99,17 @@ protected function importRgbColor(RgbColor $color): ColorInterface
);
}

protected function importHsvColor(HsvColor $color): ColorInterface
/**
* @param ColorInterface $color
* @return ColorInterface
* @throws ColorException
*/
protected function importHsvColor(ColorInterface $color): ColorInterface
{
if (!($color instanceof HsvColor)) {
throw new ColorException('Unabled to import color of type ' . $color::class . '.');
}

// normalized values of hsv channels
list($h, $s, $v) = array_map(function ($channel) {
return $channel->normalize();
Expand Down
29 changes: 24 additions & 5 deletions src/Colors/Hsv/Colorspace.php
Expand Up @@ -8,6 +8,7 @@
use Intervention\Image\Colors\Rgb\Color as RgbColor;
use Intervention\Image\Colors\Hsl\Color as HslColor;
use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace;
use Intervention\Image\Exceptions\ColorException;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\ColorspaceInterface;

Expand All @@ -34,9 +35,9 @@ public function colorFromNormalized(array $normalized): ColorInterface
}

/**
* {@inheritdoc}
*
* @see ColorspaceInterface::importColor()
* @param ColorInterface $color
* @return ColorInterface
* @throws ColorException
*/
public function importColor(ColorInterface $color): ColorInterface
{
Expand All @@ -48,8 +49,17 @@ public function importColor(ColorInterface $color): ColorInterface
};
}

protected function importRgbColor(RgbColor $color): ColorInterface
/**
* @param ColorInterface $color
* @return ColorInterface
* @throws ColorException
*/
protected function importRgbColor(ColorInterface $color): ColorInterface
{
if (!($color instanceof RgbColor)) {
throw new ColorException('Unabled to import color of type ' . $color::class . '.');
}

// normalized values of rgb channels
$values = array_map(function ($channel) {
return $channel->normalize();
Expand Down Expand Up @@ -89,8 +99,17 @@ protected function importRgbColor(RgbColor $color): ColorInterface
);
}

protected function importHslColor(HslColor $color): ColorInterface
/**
* @param ColorInterface $color
* @return ColorInterface
* @throws ColorException
*/
protected function importHslColor(ColorInterface $color): ColorInterface
{
if (!($color instanceof HslColor)) {
throw new ColorException('Unabled to import color of type ' . $color::class . '.');
}

// normalized values of hsl channels
list($h, $s, $l) = array_map(function ($channel) {
return $channel->normalize();
Expand Down
40 changes: 34 additions & 6 deletions src/Colors/Rgb/Colorspace.php
Expand Up @@ -7,6 +7,7 @@
use Intervention\Image\Colors\Hsv\Color as HsvColor;
use Intervention\Image\Colors\Hsl\Color as HslColor;
use Intervention\Image\Colors\Cmyk\Color as CmykColor;
use Intervention\Image\Exceptions\ColorException;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\ColorspaceInterface;

Expand Down Expand Up @@ -34,9 +35,9 @@ public function colorFromNormalized(array $normalized): ColorInterface
}

/**
* {@inheritdoc}
*
* @see ColorspaceInterface::importColor()
* @param ColorInterface $color
* @return ColorInterface
* @throws ColorException
*/
public function importColor(ColorInterface $color): ColorInterface
{
Expand All @@ -48,17 +49,35 @@ public function importColor(ColorInterface $color): ColorInterface
};
}

protected function importCmykColor(CmykColor $color): ColorInterface
/**
* @param ColorInterface $color
* @return ColorInterface
* @throws ColorException
*/
protected function importCmykColor(ColorInterface $color): ColorInterface
{
if (!($color instanceof CmykColor)) {
throw new ColorException('Unabled to import color of type ' . $color::class . '.');
}

return new Color(
(int) (255 * (1 - $color->cyan()->normalize()) * (1 - $color->key()->normalize())),
(int) (255 * (1 - $color->magenta()->normalize()) * (1 - $color->key()->normalize())),
(int) (255 * (1 - $color->yellow()->normalize()) * (1 - $color->key()->normalize())),
);
}

protected function importHsvColor(HsvColor $color): ColorInterface
/**
* @param ColorInterface $color
* @return ColorInterface
* @throws ColorException
*/
protected function importHsvColor(ColorInterface $color): ColorInterface
{
if (!($color instanceof HsvColor)) {
throw new ColorException('Unabled to import color of type ' . $color::class . '.');
}

$chroma = $color->value()->normalize() * $color->saturation()->normalize();
$hue = $color->hue()->normalize() * 6;
$x = $chroma * (1 - abs(fmod($hue, 2) - 1));
Expand All @@ -83,8 +102,17 @@ protected function importHsvColor(HsvColor $color): ColorInterface
return $this->colorFromNormalized($values);
}

protected function importHslColor(HslColor $color): ColorInterface
/**
* @param ColorInterface $color
* @return ColorInterface
* @throws ColorException
*/
protected function importHslColor(ColorInterface $color): ColorInterface
{
if (!($color instanceof HslColor)) {
throw new ColorException('Unabled to import color of type ' . $color::class . '.');
}

// normalized values of hsl channels
list($h, $s, $l) = array_map(function ($channel) {
return $channel->normalize();
Expand Down
45 changes: 33 additions & 12 deletions src/Format.php
Expand Up @@ -16,6 +16,7 @@
use Intervention\Image\Encoders\WebpEncoder;
use Intervention\Image\Exceptions\NotSupportedException;
use Intervention\Image\Interfaces\EncoderInterface;
use ReflectionClass;

enum Format
{
Expand Down Expand Up @@ -88,23 +89,43 @@ public function fileExtensions(): array
}

/**
* Create an encoder instance that matches the format
* Create an encoder instance with given options that matches the format
*
* @param array $options
* @param mixed $options
* @return EncoderInterface
*/
public function encoder(mixed ...$options): EncoderInterface
{
return match ($this) {
self::AVIF => new AvifEncoder(...$options),
self::BMP => new BmpEncoder(...$options),
self::GIF => new GifEncoder(...$options),
self::HEIC => new HeicEncoder(...$options),
self::JP2 => new Jpeg2000Encoder(...$options),
self::JPEG => new JpegEncoder(...$options),
self::PNG => new PngEncoder(...$options),
self::TIFF => new TiffEncoder(...$options),
self::WEBP => new WebpEncoder(...$options),
// get classname of target encoder from current format
$classname = match ($this) {
self::AVIF => AvifEncoder::class,
self::BMP => BmpEncoder::class,
self::GIF => GifEncoder::class,
self::HEIC => HeicEncoder::class,
self::JP2 => Jpeg2000Encoder::class,
self::JPEG => JpegEncoder::class,
self::PNG => PngEncoder::class,
self::TIFF => TiffEncoder::class,
self::WEBP => WebpEncoder::class,
};

// get parameters of target encoder
$parameters = [];
$reflectionClass = new ReflectionClass($classname);
if ($constructor = $reflectionClass->getConstructor()) {
$parameters = array_map(
fn ($parameter) => $parameter->getName(),
$constructor->getParameters(),
);
}

// filter out unavailable options of target encoder
$options = array_filter(
$options,
fn ($key) => in_array($key, $parameters),
ARRAY_FILTER_USE_KEY,
);

return new $classname(...$options);
}
}
8 changes: 4 additions & 4 deletions src/Geometry/Point.php
Expand Up @@ -113,17 +113,17 @@ public function setPosition(int $x, int $y): self
* Rotate point ccw around pivot
*
* @param float $angle
* @param Point $pivot
* @param PointInterface $pivot
* @return Point
*/
public function rotate(float $angle, self $pivot): self
public function rotate(float $angle, PointInterface $pivot): self
{
$sin = round(sin(deg2rad($angle)), 6);
$cos = round(cos(deg2rad($angle)), 6);

return $this->setPosition(
intval($cos * ($this->x - $pivot->x) - $sin * ($this->y - $pivot->y) + $pivot->x),
intval($sin * ($this->x - $pivot->x) + $cos * ($this->y - $pivot->y) + $pivot->y)
intval($cos * ($this->x() - $pivot->x()) - $sin * ($this->y() - $pivot->y()) + $pivot->x()),
intval($sin * ($this->x() - $pivot->x()) + $cos * ($this->y() - $pivot->y()) + $pivot->y())
);
}
}
2 changes: 1 addition & 1 deletion src/Modifiers/CropModifier.php
Expand Up @@ -17,7 +17,7 @@ public function __construct(
public int $height,
public int $offset_x = 0,
public int $offset_y = 0,
public mixed $background = 'f00',
public mixed $background = 'ffffff',
public string $position = 'top-left'
) {
}
Expand Down

0 comments on commit c7fc478

Please sign in to comment.