<commit_before><?php

namespace Brera\Image;

class ImageMagickInscribeCommand implements ImageProcessorCommand
{
    use ResizeCommandTrait;

    /**
     * @var string
     */
    private $backgroundColor;

    /**
     * @param int $width
     * @param int $height
     * @param string $backgroundColor
     */
    public function __construct($width, $height, $backgroundColor)
    {
        $this->width = $width;
        $this->height = $height;
        $this->backgroundColor = $backgroundColor;
    }

    /**
     * @param string $imageStream
     * @return string
     */
    public function execute($imageStream)
    {
        $this->validateImageDimensions();
        $this->validateBackgroundColor();

        $image = new \Imagick();

        try {
            $image->readImageBlob($imageStream);
        } catch(\ImagickException $e) {
            throw new InvalidImageStreamException($e->getMessage());
        }

        $image->resizeImage($this->width, $this->height, \Imagick::FILTER_LANCZOS, 1, true);
        $canvas = $this->inscribeImageIntoCanvas($image);

        return $canvas->getImageBlob();
    }

    /**
     * @param \Imagick $image
     * @return \Imagick
     */
    private function inscribeImageIntoCanvas(\Imagick $image)
    {
        $dimensions = $image->getImageGeometry();
        $x = ($this->width - $dimensions['width']) / 2;
        $y = ($this->height - $dimensions['height']) / 2;

        $canvas = new \Imagick();
        $canvas->newImage($this->width, $this->height, $this->backgroundColor, $image->getImageFormat());
        $canvas->compositeImage($image, \Imagick::COMPOSITE_OVER, $x, $y);

        return $canvas;
    }

    /**
     * @throws InvalidColorException
     */
    private function validateBackgroundColor()
    {
        try {
            (new \ImagickPixel())->setColor($this->backgroundColor);
        } catch (\ImagickException $e) {
            throw new InvalidColorException($e->getMessage());
        }
    }
}<commit_msg>Issue #67: Add ImagickPixelException support<commit_after><?php

namespace Brera\Image;

class ImageMagickInscribeCommand implements ImageProcessorCommand
{
    use ResizeCommandTrait;

    /**
     * @var string
     */
    private $backgroundColor;

    /**
     * @param int $width
     * @param int $height
     * @param string $backgroundColor
     */
    public function __construct($width, $height, $backgroundColor)
    {
        $this->width = $width;
        $this->height = $height;
        $this->backgroundColor = $backgroundColor;
    }

    /**
     * @param string $imageStream
     * @return string
     */
    public function execute($imageStream)
    {
        $this->validateImageDimensions();
        $this->validateBackgroundColor();

        $image = new \Imagick();

        try {
            $image->readImageBlob($imageStream);
        } catch(\ImagickException $e) {
            throw new InvalidImageStreamException($e->getMessage());
        }

        $image->resizeImage($this->width, $this->height, \Imagick::FILTER_LANCZOS, 1, true);
        $canvas = $this->inscribeImageIntoCanvas($image);

        return $canvas->getImageBlob();
    }

    /**
     * @param \Imagick $image
     * @return \Imagick
     */
    private function inscribeImageIntoCanvas(\Imagick $image)
    {
        $dimensions = $image->getImageGeometry();
        $x = ($this->width - $dimensions['width']) / 2;
        $y = ($this->height - $dimensions['height']) / 2;

        $canvas = new \Imagick();
        $canvas->newImage($this->width, $this->height, $this->backgroundColor, $image->getImageFormat());
        $canvas->compositeImage($image, \Imagick::COMPOSITE_OVER, $x, $y);

        return $canvas;
    }

    /**
     * @throws InvalidColorException
     */
    private function validateBackgroundColor()
    {
        try {
            (new \ImagickPixel())->setColor($this->backgroundColor);
        } catch (\Exception $e) {
            if ($e instanceof \ImagickException || $e instanceof \ImagickPixelException) {
                throw new InvalidColorException($e->getMessage());
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.14406574812666184, 'fraction_numerical': 0.0019337684312303602, 'mean_word_length': 2.383483237939493, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6332169', 'n_tokens_mistral': 1207, 'n_tokens_neox': 1178, 'n_words': 227}