Question

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

convert -font 'tahoma.ttf' -draw "text 10,10 'the text'" img.png
Was it helpful?

Solution

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.

OTHER TIPS

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);

Look at this page: http://www.imagemagick.org/ImageMagick-7.0.0/script/conjure.php (3rd example)

The conjure binary lets you run XML based scripts written in the Magick Scripting Language (MSL). Those can make use of IMs "query-font-metrics"-command, that doesn't seem to be available by the other binaries.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top