Файл: vendor/intervention/image/src/Drivers/Gd/Modifiers/ReduceColorsModifier.php
Строк: 119
<?php
declare(strict_types=1);
namespace InterventionImageDriversGdModifiers;
use InterventionImageDriversGdCloner;
use InterventionImageExceptionsInvalidArgumentException;
use InterventionImageExceptionsModifierException;
use InterventionImageExceptionsStateException;
use InterventionImageInterfacesImageInterface;
use InterventionImageInterfacesSpecializedInterface;
use InterventionImageModifiersReduceColorsModifier as GenericReduceColorsModifier;
use InterventionImageColorsRgbColor as RgbColor;
use InterventionImageExceptionsDriverException;
class ReduceColorsModifier extends GenericReduceColorsModifier implements SpecializedInterface
{
/**
* {@inheritdoc}
*
* @see ModifierInterface::apply()
*
* @throws InvalidArgumentException
* @throws StateException
* @throws ModifierException
* @throws DriverException
*/
public function apply(ImageInterface $image): ImageInterface
{
if ($this->limit <= 0) {
throw new InvalidArgumentException('Quantization limit must be greater than 0');
}
// no color reduction if the limit is higher than the colors in the img
$colorCount = imagecolorstotal($image->core()->native());
if ($colorCount > 0 && $this->limit > $colorCount) {
return $image;
}
$width = $image->width();
$height = $image->height();
$backgroundColor = $this->backgroundColor($image);
if (!$backgroundColor instanceof RgbColor) {
throw new ModifierException('Failed to convert background color to RGB color space');
}
$nativeBackgroundColor = $this->driver()
->colorProcessor($image)
->export($backgroundColor);
foreach ($image as $frame) {
// create new image for color quantization
$reduced = Cloner::cloneEmpty($frame->native(), background: $backgroundColor);
// fill with background
imagefill($reduced, 0, 0, $nativeBackgroundColor);
// set transparency
imagecolortransparent($reduced, $nativeBackgroundColor);
// copy original image (colors are limited automatically in the copy process)
imagecopy($reduced, $frame->native(), 0, 0, 0, 0, $width, $height);
// gd library does not support color quantization directly therefore the
// colors are decrease by transforming the image to a palette version
imagetruecolortopalette($reduced, true, $this->limit);
$frame->setNative($reduced);
}
return $image;
}
}