/** * {@inheritdoc} */ public function create(BoxInterface $size, ColorInterface $color = null) { $width = $size->getWidth(); $height = $size->getHeight(); $palette = null !== $color ? $color->getPalette() : new RGB(); $color = null !== $color ? $color : $palette->color('fff'); try { $gmagick = new \Gmagick(); // Gmagick does not support creation of CMYK GmagickPixel // see https://bugs.php.net/bug.php?id=64466 if ($color instanceof CMYKColor) { $switchPalette = $palette; $palette = new RGB(); $pixel = new \GmagickPixel($palette->color((string) $color)); } else { $switchPalette = null; $pixel = new \GmagickPixel((string) $color); } if ($color->getPalette()->supportsAlpha() && $color->getAlpha() < 100) { throw new NotSupportedException('alpha transparency is not supported'); } $gmagick->newimage($width, $height, $pixel->getcolor(false)); $gmagick->setimagecolorspace(\Gmagick::COLORSPACE_TRANSPARENT); $gmagick->setimagebackgroundcolor($pixel); $image = new Image($gmagick, $palette, new MetadataBag()); if ($switchPalette) { $image->usePalette($switchPalette); } return $image; } catch (\GmagickException $e) { throw new RuntimeException('Could not create empty image', $e->getCode(), $e); } }
/** * {@inheritdoc} */ public function create(BoxInterface $size, ColorInterface $color = null) { $width = $size->getWidth(); $height = $size->getHeight(); $resource = imagecreatetruecolor($width, $height); if (false === $resource) { throw new RuntimeException('Create operation failed'); } $palette = null !== $color ? $color->getPalette() : new RGB(); $color = $color ? $color : $palette->color('fff'); if (!$color instanceof RGBColor) { throw new InvalidArgumentException('GD driver only supports RGB colors'); } $index = imagecolorallocatealpha($resource, $color->getRed(), $color->getGreen(), $color->getBlue(), round(127 * $color->getAlpha() / 100)); if (false === $index) { throw new RuntimeException('Unable to allocate color'); } if (false === imagefill($resource, 0, 0, $index)) { throw new RuntimeException('Could not set background color fill'); } if ($color->getAlpha() >= 95) { imagecolortransparent($resource, $index); } return $this->wrap($resource, $palette); }
/** * {@inheritdoc} */ public function blend(ColorInterface $color1, ColorInterface $color2, $amount) { if (!$color1 instanceof CMYKColor || !$color2 instanceof CMYKColor) { throw new RuntimeException('CMYK palette can only blend CMYK colors'); } return $this->color(array(min(100, $color1->getCyan() + $color2->getCyan() * $amount), min(100, $color1->getMagenta() + $color2->getMagenta() * $amount), min(100, $color1->getYellow() + $color2->getYellow() * $amount), min(100, $color1->getKeyline() + $color2->getKeyline() * $amount))); }
/** * {@inheritdoc} */ public function colorize(ColorInterface $color) { if (!$color instanceof RGBColor) { throw new RuntimeException('Colorize effects only accepts RGB color in GD context'); } if (false === imagefilter($this->resource, IMG_FILTER_COLORIZE, $color->getRed(), $color->getGreen(), $color->getBlue())) { throw new RuntimeException('Failed to colorize the image'); } return $this; }
/** * {@inheritdoc} */ public final function getColor(PointInterface $position) { $l = $this->getDistance($position); if ($l >= $this->length) { return $this->end; } if ($l < 0) { return $this->start; } return $this->start->getPalette()->blend($this->start, $this->end, $l / $this->length); }
/** * {@inheritdoc} */ public function colorize(ColorInterface $color) { if (!$color instanceof RGB) { throw new NotSupportedException('Colorize with non-rgb color is not supported'); } try { $this->imagick->colorizeImage((string) $color, new \ImagickPixel(sprintf('rgba(%d, %d, %d, 1)', $color->getRed(), $color->getGreen(), $color->getBlue()))); } catch (\ImagickException $e) { throw new RuntimeException('Failed to colorize the image'); } return $this; }
/** * {@inheritdoc} */ public function create(BoxInterface $size, ColorInterface $color = null) { $width = $size->getWidth(); $height = $size->getHeight(); $palette = null !== $color ? $color->getPalette() : new RGB(); $color = null !== $color ? $color : $palette->color('fff'); try { $pixel = new \ImagickPixel((string) $color); $pixel->setColorValue(\Imagick::COLOR_ALPHA, number_format(round($color->getAlpha() / 100, 2), 1)); $imagick = new \Imagick(); $imagick->newImage($width, $height, $pixel); $imagick->setImageMatte(true); $imagick->setImageBackgroundColor($pixel); $pixel->clear(); $pixel->destroy(); return new Image($imagick, $palette, new MetadataBag()); } catch (\ImagickException $e) { throw new RuntimeException('Could not create empty image', $e->getCode(), $e); } }
/** * {@inheritdoc} */ public function create(BoxInterface $size, ColorInterface $color = null) { $width = $size->getWidth(); $height = $size->getHeight(); $palette = null !== $color ? $color->getPalette() : new RGB(); $color = null !== $color ? $color : $palette->color('fff'); try { $pixel = new \ImagickPixel((string) $color); $pixel->setColorValue(Imagick::COLOR_ALPHA, $color->getAlpha() / 100); $imagick = new Imagick(); $imagick->newImage($width, $height, $pixel); $imagick->setImageMatte(true); $imagick->setImageBackgroundColor($pixel); if (version_compare('6.3.1', $this->getVersion($imagick)) < 0 && version_compare('7.0', $this->getVersion($imagick)) > 0) { $imagick->setImageOpacity($pixel->getColorValue(Imagick::COLOR_ALPHA)); } $pixel->clear(); $pixel->destroy(); return new Image($imagick, $palette, new MetadataBag()); } catch (\ImagickException $e) { throw new RuntimeException('Could not create empty image', $e->getCode(), $e); } }
/** * Gets specifically formatted color string from Color instance * * @param ColorInterface $color * * @return \ImagickPixel */ private function getColor(ColorInterface $color) { $pixel = new \ImagickPixel((string) $color); $pixel->setColorValue(\Imagick::COLOR_ALPHA, $color->getAlpha() / 100); return $pixel; }
/** * Gets specifically formatted color string from Color instance * * @param ColorInterface $color * * @return \ImagickPixel */ private function getColor(ColorInterface $color) { $pixel = new \ImagickPixel((string) $color); $pixel->setColorValue(\Imagick::COLOR_ALPHA, number_format(round($color->getAlpha() / 100, 2), 1)); return $pixel; }
/** * Gets specifically formatted color string from Color instance * * @param ColorInterface $color * * @return string */ private function getColor(ColorInterface $color) { if (!$color->isOpaque()) { throw new InvalidArgumentException('Gmagick doesn\'t support transparency'); } $pixel = new \GmagickPixel((string) $color); $pixel->setColorValue(\Gmagick::COLOR_OPACITY, number_format(abs(round($color->getAlpha() / 100, 1)), 1)); return $pixel; }
/** * Gets specifically formatted color string from Color instance * * @param ColorInterface $color * * @return \GmagickPixel * * @throws InvalidArgumentException */ private function getColor(ColorInterface $color) { if (!$color->isOpaque()) { throw new InvalidArgumentException('Gmagick doesn\'t support transparency'); } return new \GmagickPixel((string) $color); }
/** * {@inheritdoc} */ public function blend(ColorInterface $color1, ColorInterface $color2, $amount) { if (!$color1 instanceof GrayColor || !$color2 instanceof GrayColor) { throw new RuntimeException('Grayscale palette can only blend Grayscale colors'); } return $this->color(array((int) min(255, min($color1->getGray(), $color2->getGray()) + round(abs($color2->getGray() - $color1->getGray()) * $amount))), (int) min(100, min($color1->getAlpha(), $color2->getAlpha()) + round(abs($color2->getAlpha() - $color1->getAlpha()) * $amount))); }
/** * Internal * * Generates a GD color from Color instance * * @param ColorInterface $color * * @return integer A color identifier * * @throws RuntimeException * @throws InvalidArgumentException */ private function getColor(ColorInterface $color) { if (!$color instanceof RGBColor) { throw new InvalidArgumentException('GD driver only supports RGB colors'); } $index = imagecolorallocatealpha($this->resource, $color->getRed(), $color->getGreen(), $color->getBlue(), round(127 * (100 - $color->getAlpha()) / 100)); if (false === $index) { throw new RuntimeException(sprintf('Unable to allocate color "RGB(%s, %s, %s)" with transparency of %d percent', $color->getRed(), $color->getGreen(), $color->getBlue(), $color->getAlpha())); } return $index; }