Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get width/height of text

How to get width/height of written text in Imagemagick?

convert -font 'tahoma.ttf' -draw "text 10,10 'the text'" img.png
like image 851
clarkk Avatar asked Dec 11 '11 01:12

clarkk


People also ask

How do you find the width of text?

The dimensions of the text are calculated using the measureText() method. The text to be measured is passed to this method. It returns a TextMetrics object that contains information about the measured text. The width property of this TextMetrics object is used to get the width of the text.

How do you calculate font size?

A font is often measured in pt (points). Points dictate the height of the lettering. There are approximately 72 (72.272) points in one inch or 2.54 cm. For example, the font size 72 would be about one inch tall, and 36 would be about a half of an inch.


2 Answers

we can use queryFontMetrics function of imagick. see this link (http://php.net/manual/en/imagick.queryfontmetrics.php)

checkout response of this function you will get width and height.

like image 165
Ajay Avatar answered Sep 27 '22 23:09

Ajay


This is really late, but, from PHP.net:

Using:

Imagick::queryFontMetrics — Returns an array representing the font metrics

like this:

   // Set font.
      $font = PATH/TO/YOUR/CUSTOM/FONT
   // Get dimensions.
   // Create a new Imagick object.
      $imTest = new Imagick();

   // Create an ImagickDraw object.
      $drawTest = new ImagickDraw();

   // Set the font.
      $drawTest->setFont($font);

   // Set the local X and Y.
      $localX = 0;
      $localY = 0;

   // Dump the font metrics, autodetect multiline 
      for ($i = 0; $i < strlen($yourText); $i++) {
         if ($yourText[$i] === ' ') {
            $tempX += $imTest->queryFontMetrics($drawTest, $yourText[$i])['textWidth'];
         } else {
            $tempX += $imTest->queryFontMetrics($drawTest, $yourText[$i])['originX'] + $imTest->queryFontMetrics($drawTest, $yourText[$i])['boundingBox']['x2'] + $imTest->queryFontMetrics($drawTest, $yourText[$i])['boundingBox']['x1'];
         }    
      }

Where:

characterWidth and characterHeight - These seem to be related to the size you have specified for the font and don't seem to differ from font to font (at the same size). As such, they are not especially useful (to me, at least). They are not a reliable indicator of how much space the font will use.

ascender - The ascender is the part of the font that is above the baseline. It is not character related - the ascender value is the same for every character in the font.

descender - The descender is the part of the font that is below the baseline. It is represented as a negative figure. Adding the absolute values of the ascender and the descender gives you the...

textHeight - This is the total height available to the font. It is the same for every character in the font irrespective of its case or how much space the character seems to occupy. This can be used to determined the line height when outputting paragraphs, etc.

textWidth - This value varies from character to character and is the width of the character. This is useful if the boundingBox does not provide usable values (see boundingBox below). When positioning characters one by one - don't use textWidth, use originX (see below).

maxHorizontalAdvance - I'm afraid I haven't quite figured out the purpose of this. It is the same for every character in the font. For the font Arial Italic at size 67, the value is 89 which is much wider than the advance reported for the M or the W at the same size.

boundingBox - This returns an associative array describing the four points (x1, y1, x2, y2) of a rectangle that contain the character. These values are relative to the origin (i.e. the coordinates of where you are drawing the character within an image). The returned rectangle is very accurate and encloses all parts of the printed character completely - but the boundingBox only works on single characters. It will not give accurate figures for multiple characters (in my experience anyway). When drawing a box you need to ADD "x" values to the origin and SUBTRACT "y" values from the origin. You cannot rely on the boundingBox for the SPACE character. It returns a boundingBox of (0,0,0,0). textWidth (see above) comes in handy here.

originX and originY - these are inaccurately titled. The values returned in originX and originY are actually advanceX and advanceY. These values give you the position of the next character relative to the current one.

And afterwards you can generate the image with your text like this:

// Generate image.
   $cmd = 'convert -size ' . $localX . 'x' . $maxY . ' xc:none -gravity Center -font ' . $font . ' -pointsize ' . $fontSize . ' -annotate +0+0 "' . $yourText . '" ' . FINAL/PATH/TO/YOUR/FILE.png';
   $r = shell_exec($cmd);
like image 22
thvs86 Avatar answered Sep 27 '22 23:09

thvs86