How to Determine the Width of a Text in Pixels of a String PHP
PHP is a server-side scripting language that works with HMTL to produce web pages. Knowing the size of a particular font can be useful when designing a page layout. The imagefontwidth function returns the width of one of PHP's built-in fonts. The format of the function is "int imagefontwidth ( int $font )" where 'font' is a digit from 1 to 5, representing the size of the latin2 encoded font. For example, "1" is the smallest font and "5" is the largest.
Instructions
-
-
1
Click on the line of code where you want to place the imagefontwidth function.
-
2
Place the imagefontwidth into the code, along with a function or language construct that states what you want to happen to the output. For example, combine imagefontwidth with the echo language construct to output the result in pixels. The full line of code for the echo language construct combined with imagefontwidth is:
<?php
echo 'Font width: ' . imagefontwidth();
?> -
-
3
Insert a number in parentheses that represents a built-in font. For example:
imagefontwidth(4).
-
1
Tips & Warnings
You can also insert another font identifier if you have previously registered the font with the imageloadfont() function.
References
- Photo Credit Hemera Technologies/Photos.com/Getty Images