Beispiel #1
0
 /**
  * {@inheritdoc}
  */
 public function text($string, AbstractFont $font, PointInterface $position, $angle = 0, $width = null)
 {
     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);
         if ($width !== null) {
             $string = $this->wrapText($string, $text, $angle, $width);
         }
         $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;
 }
Beispiel #2
0
 /**
  * {@inheritdoc}
  */
 public function getDistance(PointInterface $position)
 {
     return $position->getX();
 }
Beispiel #3
0
 /**
  * {@inheritdoc}
  */
 public function getColorAt(PointInterface $point)
 {
     if (!$point->in($this->getSize())) {
         throw new RuntimeException(sprintf('Error getting color at point [%s,%s]. The point must be inside the image of size [%s,%s]', $point->getX(), $point->getY(), $this->getSize()->getWidth(), $this->getSize()->getHeight()));
     }
     $index = imagecolorat($this->resource, $point->getX(), $point->getY());
     $info = imagecolorsforindex($this->resource, $index);
     return $this->palette->color(array($info['red'], $info['green'], $info['blue']), max(min(100 - (int) round($info['alpha'] / 127 * 100), 100), 0));
 }
Beispiel #4
0
 /**
  * {@inheritdoc}
  */
 public function text($string, AbstractFont $font, PointInterface $position, $angle = 0, $width = null)
 {
     if (!$this->info['FreeType Support']) {
         throw new RuntimeException('GD is not compiled with FreeType support');
     }
     $angle = -1 * $angle;
     $fontsize = $font->getSize();
     $fontfile = $font->getFile();
     $x = $position->getX();
     $y = $position->getY() + $fontsize;
     if ($width !== null) {
         $string = $this->wrapText($string, $font, $angle, $width);
     }
     if (false === imagealphablending($this->resource, true)) {
         throw new RuntimeException('Font mask operation failed');
     }
     if (false === imagefttext($this->resource, $fontsize, $angle, $x, $y, $this->getColor($font->getColor()), $fontfile, $string)) {
         imagealphablending($this->resource, false);
         throw new RuntimeException('Font mask operation failed');
     }
     if (false === imagealphablending($this->resource, false)) {
         throw new RuntimeException('Font mask operation failed');
     }
     return $this;
 }
Beispiel #5
0
 /**
  * {@inheritdoc}
  */
 public function getColorAt(PointInterface $point)
 {
     if (!$point->in($this->getSize())) {
         throw new InvalidArgumentException(sprintf('Error getting color at point [%s,%s]. The point must be inside the image of size [%s,%s]', $point->getX(), $point->getY(), $this->getSize()->getWidth(), $this->getSize()->getHeight()));
     }
     try {
         $cropped = clone $this->gmagick;
         $histogram = $cropped->cropImage(1, 1, $point->getX(), $point->getY())->getImageHistogram();
     } catch (\GmagickException $e) {
         throw new RuntimeException('Unable to get the pixel');
     }
     $pixel = array_shift($histogram);
     unset($histogram, $cropped);
     return $this->pixelToColor($pixel);
 }
Beispiel #6
0
 /**
  * {@inheritdoc}
  */
 public function getColorAt(PointInterface $point)
 {
     if (!$point->in($this->getSize())) {
         throw new RuntimeException(sprintf('Error getting color at point [%s,%s]. The point must be inside the image of size [%s,%s]', $point->getX(), $point->getY(), $this->getSize()->getWidth(), $this->getSize()->getHeight()));
     }
     try {
         $pixel = $this->imagick->getImagePixelColor($point->getX(), $point->getY());
     } catch (\ImagickException $e) {
         throw new RuntimeException('Error while getting image pixel color', $e->getCode(), $e);
     }
     return $this->pixelToColor($pixel);
 }
Beispiel #7
0
 /**
  * {@inheritdoc}
  */
 public function text($string, AbstractFont $font, PointInterface $position, $angle = 0, $width = null)
 {
     try {
         $pixel = $this->getColor($font->getColor());
         $text = new \GmagickDraw();
         $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
          */
         $text->setfontsize((int) ($font->getSize() * (96 / 72)));
         $text->setfillcolor($pixel);
         $info = $this->gmagick->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);
         if ($width !== null) {
             throw new NotSupportedException('Gmagick doesn\'t support queryfontmetrics function for multiline text', 1);
         }
         $this->gmagick->annotateimage($text, $position->getX() + $x1 + $xdiff, $position->getY() + $y2 + $ydiff, $angle, $string);
         unset($pixel, $text);
     } catch (\GmagickException $e) {
         throw new RuntimeException('Draw text operation failed', $e->getCode(), $e);
     }
     return $this;
 }