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 
+6
source share
3 answers

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

recall of this function you will get the width and height.

0
source

Have a look at this page: http://www.imagemagick.org/ImageMagick-7.0.0/script/conjure.php (Third example)

The call binary code allows you to run XML-based scripts written in the Magic Scripting Language (MSL). They can use the IM commands of the query-font-metrics command, which do not seem to be available for other binaries.

0
source

This is very late, but from PHP.net :

Using:

Imagick :: queryFontMetrics - returns an array representing 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 - They seem to be related to the size specified for the font, and don't seem to differ from font to font (same size). In the form of such, they are not particularly useful (at least for me). They are not a reliable indicator of how much space the font will use.

Ascending - Elevator is part of the font that is above the baseline. This is not related to the character - the value of the pitch is the same for each character in the font.

trigger - Descender is part of the font that is below the baseline. It is presented as a negative figure. Adding absolute values ​​to the clamp and trigger give you ...

TextHeight - This is the overall height of the font. This is the same for each font character regardless of its case or volume the character seems to occupy. This can be used to determine the height line when displaying paragraphs, etc.

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

maxHorizontalAdvance - I'm afraid I did not quite understand the purpose of this. This is the same for each font character. For the Arial Italic font in size 67, the value is 89, which is much wider than previously reported for M or W of the same size.

BoundingBox - This returns an associative array describing the four points (x1, y1, x2, y2) of the rectangle that contain the character. These values ​​refer to the origin (i.e. the coordinates of where you are drawing the character inside the image). The returned rectangle very accurately and completely covers all parts of the printed character - but the boundingBox only works with single characters. This will not give exact numbers for several characters (in my opinion, anyway). When drawing a window, you need to add the "x" values ​​to the origin and the SUBTRACT "y" values ​​from the origin. You cannot rely on boundingBox for the SPACE character. It returns a bounding box (0,0,0,0). textWidth (see above) comes in handy here.

originX and originY - they are not accurately named. The values ​​returned in originX and originY are actually advanceX and forwardY. These values ​​give you the position of the next character relative to the current.

And after that you can generate an image with text as follows:

 // 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); 
0
source

Source: https://habr.com/ru/post/903539/


All Articles