Esempio n. 1
0
 /**
  * {@inheritdoc}
  */
 public function text($string, AbstractFont $font, PointInterface $position, $angle = 0)
 {
     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);
         $this->gmagick->annotateimage($text, $position->getX() + $x1 + $xdiff, $position->getY() + $y2 + $ydiff, $angle, $string);
         $pixel = null;
         $text = null;
     } catch (\GmagickException $e) {
         throw new RuntimeException('Draw text operation failed', $e->getCode(), $e);
     }
     return $this;
 }
Esempio n. 2
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;
 }
Esempio n. 3
0
 /**
  * {@inheritdoc}
  */
 public function text($string, AbstractFont $font, PointInterface $position, $angle = 0)
 {
     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 (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)) {
         throw new RuntimeException('Font mask operation failed');
     }
     if (false === imagealphablending($this->resource, false)) {
         throw new RuntimeException('Font mask operation failed');
     }
     return $this;
 }
Esempio n. 4
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);
     }
 }
Esempio n. 5
0
 /**
  * (non-PHPdoc)
  * @see Imagine\Draw\DrawerInterface::text()
  */
 public function text($string, AbstractFont $font, PointInterface $position, $angle = 0)
 {
     $angle = -1 * $angle;
     $fontsize = $font->getSize();
     $fontfile = $font->getFile();
     $info = imageftbbox($fontsize, $angle, $fontfile, $string);
     $xs = array($info[0], $info[2], $info[4], $info[6]);
     $ys = array($info[1], $info[3], $info[5], $info[7]);
     $width = abs(max($xs) - min($xs));
     $height = abs(max($ys) - min($ys));
     $xdiff = 0 - min($xs) + $position->getX();
     $ydiff = 0 - min($ys) + $position->getY();
     foreach ($xs as &$x) {
         $x += $xdiff;
     }
     foreach ($ys as &$y) {
         $y += $ydiff;
     }
     if (false === imagealphablending($this->resource, true)) {
         throw new RuntimeException('Font mask operation failed');
     }
     if (false === imagefttext($this->resource, $fontsize, $angle, $xs[0], $ys[0], $this->getColor($font->getColor()), $fontfile, $string)) {
         throw new RuntimeException('Font mask operation failed');
     }
     if (false === imagealphablending($this->resource, false)) {
         throw new RuntimeException('Font mask operation failed');
     }
     return $this;
 }
Esempio n. 6
0
 /**
  * @param \Gmagick       $gmagick
  * @param string         $file
  * @param integer        $size
  * @param ColorInterface $color
  */
 public function __construct(\Gmagick $gmagick, $file, $size, ColorInterface $color)
 {
     $this->gmagick = $gmagick;
     parent::__construct($file, $size, $color);
 }
Esempio n. 7
0
 /**
  * @param \Imagick $imagick
  * @param string   $file
  * @param integer  $size
  * @param Color    $color
  */
 public function __construct(\Imagick $imagick, $file, $size, Color $color)
 {
     $this->imagick = $imagick;
     parent::__construct($file, $size, $color);
 }
Esempio n. 8
0
 /**
  * Internal
  *
  * Fits a string into box with given width
  */
 private function wrapText($string, AbstractFont $font, $angle, $width)
 {
     $result = '';
     $words = explode(' ', $string);
     foreach ($words as $word) {
         $teststring = $result . ' ' . $word;
         $testbox = imagettfbbox($font->getSize(), $angle, $font->getFile(), $teststring);
         if ($testbox[2] > $width) {
             $result .= ($result == '' ? '' : "\n") . $word;
         } else {
             $result .= ($result == '' ? '' : ' ') . $word;
         }
     }
     return $result;
 }
Esempio n. 9
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);
         $deltaX = $info['characterWidth'] * sin(deg2rad($angle));
         $deltaY = $info['characterHeight'];
         $posX = $position->getX() - $deltaX;
         if ($posX < 0) {
             $posX = 0;
         }
         $this->gmagick->annotateimage($text, $posX, $position->getY() + $deltaY, $angle, $string);
         unset($pixel, $text);
     } catch (\GmagickException $e) {
         throw new RuntimeException('Draw text operation failed', $e->getCode(), $e);
     }
     return $this;
 }