Файл: vendor/intervention/image/src/Drivers/Imagick/Modifiers/ColorspaceModifier.php
Строк: 113
<?php
declare(strict_types=1);
namespace InterventionImageDriversImagickModifiers;
use Error;
use Imagick;
use ImagickException;
use InterventionImageColorsCmykColorspace as Cmyk;
use InterventionImageColorsHslColorspace as Hsl;
use InterventionImageColorsHsvColorspace as Hsv;
use InterventionImageColorsOklabColorspace as Oklab;
use InterventionImageColorsOklchColorspace as Oklch;
use InterventionImageColorsRgbColorspace as Rgb;
use InterventionImageExceptionsModifierException;
use InterventionImageExceptionsNotSupportedException;
use InterventionImageInterfacesColorspaceInterface;
use InterventionImageInterfacesImageInterface;
use InterventionImageInterfacesSpecializedInterface;
use InterventionImageModifiersColorspaceModifier as GenericColorspaceModifier;
class ColorspaceModifier extends GenericColorspaceModifier implements SpecializedInterface
{
/**
* @throws ModifierException
* @throws NotSupportedException
*/
public function apply(ImageInterface $image): ImageInterface
{
$colorspace = $this->targetColorspace();
$imagick = $image->core()->native();
try {
$result = $imagick->transformImageColorspace(
$this->imagickColorspaceOrFail($colorspace),
);
if ($result === false) {
throw new ModifierException(
'Failed to apply ' . self::class . ', unable to transform image colorspace',
);
}
} catch (ImagickException $e) {
throw new ModifierException(
'Failed to apply ' . self::class . ', unable to transform image colorspace',
previous: $e,
);
}
return $image;
}
/**
* @throws ModifierException
* @throws NotSupportedException
*/
private function imagickColorspaceOrFail(ColorspaceInterface $colorspace): int
{
if ($colorspace instanceof Rgb) {
return Imagick::COLORSPACE_SRGB;
}
if ($colorspace instanceof Cmyk) {
return Imagick::COLORSPACE_CMYK;
}
if ($colorspace instanceof Hsl) {
return Imagick::COLORSPACE_HSL;
}
if ($colorspace instanceof Hsv) {
return Imagick::COLORSPACE_HSB;
}
try {
if ($colorspace instanceof Oklab && defined(Imagick::class . '::COLORSPACE_OKLAB')) {
return constant(Imagick::class . '::COLORSPACE_OKLAB');
}
if ($colorspace instanceof Oklch && defined(Imagick::class . '::COLORSPACE_OKLCH')) {
return constant(Imagick::class . '::COLORSPACE_OKLCH');
}
} catch (Error $e) {
throw new ModifierException(
'Failed to convert colorspace to Imagick constant',
previous: $e,
);
}
throw new NotSupportedException('Colorspace ' . $colorspace::class . ' is not supported by driver');
}
}