Exemple #1
1
 /**
  * (non-PHPdoc)
  * @see Imagine\ImagineInterface::load()
  */
 public function load($string)
 {
     try {
         $imagick = new \Imagick();
         $imagick->readImageBlob($string);
         $imagick->setImageMatte(true);
         return new Image($imagick);
     } catch (\ImagickException $e) {
         throw new RuntimeException('Could not load image from string', $e->getCode(), $e);
     }
 }
 /**
  * (non-PHPdoc)
  * @see Imagine\Image\FontInterface::box()
  */
 public function box($string, $angle = 0)
 {
     $text = new \ImagickDraw();
     $text->setFont($this->file);
     $text->setFontSize($this->size);
     $info = $this->imagick->queryFontMetrics($text, $string);
     $box = new Box($info['textWidth'], $info['textHeight']);
     return $box;
 }
Exemple #3
0
 /**
  * (non-PHPdoc)
  * @see Imagine\Draw\DrawerInterface::text()
  */
 public function text($string, AbstractFont $font, PointInterface $position, $angle = 0)
 {
     try {
         $pixel = $this->getColor($font->getColor());
         $text = new \ImagickDraw();
         $text->setFont($font->getFile());
         $text->setFontSize($font->getSize());
         $text->setFillColor($pixel);
         $text->setTextAntialias(true);
         $info = $this->imagick->queryFontMetrics($text, $string);
         $rad = deg2rad($angle);
         $cos = cos($rad);
         $sin = sin($rad);
         $x1 = round(0 * $cos - 0 * $sin);
         $x2 = round($info['textWidth'] * $cos - $info['textHeight'] * $sin);
         $y1 = round(0 * $sin + 0 * $cos);
         $y2 = round($info['textWidth'] * $sin + $info['textHeight'] * $cos);
         $xdiff = 0 - min($x1, $x2);
         $ydiff = 0 - min($y1, $y2);
         $this->imagick->annotateImage($text, $position->getX() + $x1 + $xdiff, $position->getY() + $y2 + $ydiff, $angle, $string);
         $pixel->clear();
         $pixel->destroy();
         $text->clear();
         $text->destroy();
     } catch (\ImagickException $e) {
         throw new RuntimeException('Draw text operation failed', $e->getCode(), $e);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function count()
 {
     try {
         return $this->resource->getNumberImages();
     } catch (\ImagickException $e) {
         throw new RuntimeException('Failed to count the number of layers', $e->getCode(), $e);
     }
 }
Exemple #5
0
 /**
  * {@inheritdoc}
  */
 public function scale(BoxInterface $size)
 {
     try {
         $this->imagick->scaleImage($size->getWidth(), $size->getHeight());
     } catch (\ImagickException $e) {
         throw new RuntimeException('Scale operation failed', $e->getCode(), $e);
     }
     return $this;
 }
Exemple #6
0
 /**
  * Sets colorspace and image type, assigns the palette.
  *
  * @param PaletteInterface $palette
  *
  * @throws InvalidArgumentException
  */
 private function setColorspace(PaletteInterface $palette)
 {
     static $typeMapping = array(PaletteInterface::PALETTE_CMYK => \Imagick::IMGTYPE_TRUECOLORMATTE, PaletteInterface::PALETTE_RGB => \Imagick::IMGTYPE_TRUECOLORMATTE, PaletteInterface::PALETTE_GRAYSCALE => \Imagick::IMGTYPE_GRAYSCALEMATTE);
     if (!isset(static::$colorspaceMapping[$palette->name()])) {
         throw new InvalidArgumentException(sprintf('The palette %s is not supported by Imagick driver', $palette->name()));
     }
     $this->imagick->setType($typeMapping[$palette->name()]);
     $this->imagick->setColorspace(static::$colorspaceMapping[$palette->name()]);
     $this->palette = $palette;
 }
Exemple #7
0
 /**
  * {@inheritdoc}
  */
 public function box($string, $angle = 0)
 {
     $text = new \ImagickDraw();
     $text->setFont($this->file);
     /**
      * @see http://www.php.net/manual/en/imagick.queryfontmetrics.php#101027
      *
      * ensure font resolution is the same as GD's hard-coded 96
      */
     if (version_compare(phpversion("imagick"), "3.0.2", ">=")) {
         $text->setResolution(96, 96);
         $text->setFontSize($this->size);
     } else {
         $text->setFontSize((int) ($this->size * (96 / 72)));
     }
     $info = $this->imagick->queryFontMetrics($text, $string);
     $box = new Box($info['textWidth'], $info['textHeight']);
     return $box;
 }
Exemple #8
0
 /**
  * {@inheritdoc}
  */
 public function offsetUnset($offset)
 {
     try {
         $this->extractAt($offset);
     } catch (RuntimeException $e) {
         return;
     }
     try {
         $this->resource->setIteratorIndex($offset);
         $this->resource->removeImage();
     } catch (\ImagickException $e) {
         throw new RuntimeException('Unable to remove layer', $e->getCode(), $e);
     }
 }
Exemple #9
0
 public function testCoalesce()
 {
     $width = null;
     $height = null;
     $resource = new \Imagick();
     $resource->newImage(20, 10, new \ImagickPixel("black"));
     $resource->newImage(10, 10, new \ImagickPixel("black"));
     $layers = new Layers(new Image($resource), $resource);
     $layers->coalesce();
     foreach ($layers as $layer) {
         $size = $layer->getSize();
         if ($width === null) {
             $width = $size->getWidth();
         } else {
             $this->assertEquals($width, $size->getWidth());
         }
         if ($height === null) {
             $height = $size->getHeight();
         } else {
             $this->assertEquals($height, $size->getHeight());
         }
     }
 }
Exemple #10
0
 /**
  * Internal
  *
  * Fits a string into box with given width
  */
 private function wrapText($string, $text, $angle, $width)
 {
     $result = '';
     $words = explode(' ', $string);
     foreach ($words as $word) {
         $teststring = $result . ' ' . $word;
         $testbox = $this->imagick->queryFontMetrics($text, $teststring, true);
         if ($testbox['textWidth'] > $width) {
             $result .= ($result == '' ? '' : "\n") . $word;
         } else {
             $result .= ($result == '' ? '' : ' ') . $word;
         }
     }
     return $result;
 }
Exemple #11
0
 /**
  * Performs optimized gradient fill for non-opaque linear gradients
  *
  * @param Linear $fill
  */
 private function applyFastLinear(Linear $fill)
 {
     $gradient = new \Imagick();
     $size = $this->getSize();
     $color = sprintf('gradient:%s-%s', (string) $fill->getStart(), (string) $fill->getEnd());
     if ($fill instanceof Horizontal) {
         $gradient->newPseudoImage($size->getHeight(), $size->getWidth(), $color);
         $gradient->rotateImage(new \ImagickPixel(), 90);
     } else {
         $gradient->newPseudoImage($size->getWidth(), $size->getHeight(), $color);
     }
     $this->imagick->compositeImage($gradient, \Imagick::COMPOSITE_OVER, 0, 0);
     $gradient->clear();
     $gradient->destroy();
 }
Exemple #12
0
 /**
  * {@inheritdoc}
  */
 public function text($string, AbstractFont $font, PointInterface $position, $angle = 0)
 {
     try {
         $pixel = $this->getColor($font->getColor());
         $text = new \ImagickDraw();
         $text->setFont($font->getFile());
         /**
          * @see http://www.php.net/manual/en/imagick.queryfontmetrics.php#101027
          *
          * ensure font resolution is the same as GD's hard-coded 96
          */
         if (version_compare(phpversion("imagick"), "3.0.2", ">=")) {
             $text->setResolution(96, 96);
             $text->setFontSize($font->getSize());
         } else {
             $text->setFontSize((int) ($font->getSize() * (96 / 72)));
         }
         $text->setFillColor($pixel);
         $text->setTextAntialias(true);
         $info = $this->imagick->queryFontMetrics($text, $string);
         $rad = deg2rad($angle);
         $cos = cos($rad);
         $sin = sin($rad);
         // round(0 * $cos - 0 * $sin)
         $x1 = 0;
         $x2 = round($info['characterWidth'] * $cos - $info['characterHeight'] * $sin);
         // round(0 * $sin + 0 * $cos)
         $y1 = 0;
         $y2 = round($info['characterWidth'] * $sin + $info['characterHeight'] * $cos);
         $xdiff = 0 - min($x1, $x2);
         $ydiff = 0 - min($y1, $y2);
         $this->imagick->annotateImage($text, $position->getX() + $x1 + $xdiff, $position->getY() + $y2 + $ydiff, $angle, $string);
         $pixel->clear();
         $pixel->destroy();
         $text->clear();
         $text->destroy();
     } catch (\ImagickException $e) {
         throw new RuntimeException('Draw text operation failed', $e->getCode(), $e);
     }
     return $this;
 }
Exemple #13
0
 /**
  * Returns ImageMagick version
  *
  * @param Imagick $imagick
  *
  * @return string
  */
 private function getVersion(Imagick $imagick)
 {
     $v = $imagick->getVersion();
     list($version) = sscanf($v['versionString'], 'ImageMagick %s %04d-%02d-%02d %s %s');
     return $version;
 }
Exemple #14
0
 /**
  * {@inheritdoc}
  */
 public function read($resource)
 {
     if (!is_resource($resource)) {
         throw new InvalidArgumentException('Variable does not contain a stream resource');
     }
     try {
         $imagick = new \Imagick();
         $imagick->readImageFile($resource);
     } catch (\ImagickException $e) {
         throw new RuntimeException('Could not read image from resource', $e->getCode(), $e);
     }
     return new Image($imagick);
 }
Exemple #15
0
 private function createPalette(\Imagick $imagick)
 {
     switch ($imagick->getImageColorspace()) {
         case \Imagick::COLORSPACE_RGB:
         case \Imagick::COLORSPACE_SRGB:
             return new RGB();
         case \Imagick::COLORSPACE_CMYK:
             return new CMYK();
         case \Imagick::COLORSPACE_GRAY:
             return new Grayscale();
         default:
             throw new NotSupportedException('Only RGB and CMYK colorspace are curently supported');
     }
 }
Exemple #16
0
 /**
  * Internal
  *
  * Applies options before save or output
  *
  * @param \Imagick $image
  * @param array $options
  */
 private function applyImageOptions(\Imagick $image, array $options)
 {
     if (isset($options['quality'])) {
         $image->setImageCompressionQuality($options['quality']);
     }
 }