/** * {@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; }
/** * 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; }
/** * @param \Imagick $imagick * @param string $file * @param integer $size * @param ColorInterface $color */ public function __construct(\Imagick $imagick, $file, $size, ColorInterface $color) { $this->imagick = $imagick; parent::__construct($file, $size, $color); }
/** * {@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; }