Example #1
0
 /**
  * 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;
 }