/** * Calculates size for bounding box of string written in given font. * * @param string $string * @param FontInterface $font * * @return bool|Box Instance of Box object, false on error */ public static function calculateTextSize($string, FontInterface $font) { $imagine = Tygh::$app['image']; if ($imagine instanceof \Imagine\Imagick\Imagine && class_exists('ImagickDraw')) { $text = new \ImagickDraw(); $text->setFont($font->getFile()); if (version_compare(phpversion("imagick"), "3.0.2", ">=")) { $text->setResolution(96, 96); $text->setFontSize($font->getSize()); } else { $text->setFontSize((int) ($font->getSize() * (96 / 72))); } $imagick = new \Imagick(); $info = $imagick->queryFontMetrics($text, $string); $text->clear(); $text->destroy(); $imagick->clear(); $imagick->destroy(); return new Box($info['textWidth'], $info['textHeight']); } if ($imagine instanceof \Imagine\Gd\Imagine && function_exists('imagettfbbox')) { $ttfbbox = imagettfbbox($font->getSize(), 0, $font->getFile(), $string); return new Box(abs($ttfbbox[2]), abs($ttfbbox[7])); } return false; }
/** * {@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; }
/** * {@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; }
<?php $im = new Imagick(); $im->newImage(1000, 1000, "white", "png"); $draw = new ImagickDraw(); $draw->setFont(__DIR__ . '/anonymous_pro_minus.ttf'); $draw->setFontSize(72); $draw->setResolution(10, 10); $small = $im->queryFontMetrics($draw, "Hello World"); $draw->setResolution(300, 300); $large = $im->queryFontMetrics($draw, "Hello World"); var_dump($small['textWidth'] < $large['textWidth']); echo "OK\n";