Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to get the pixel height of a character in the specified font using the imagefontheight() function in PHP?
The imagefontheight() function is an inbuilt PHP function used to get the pixel height of a character in the specified font. This is particularly useful when working with GD library for dynamic image generation and text positioning.
Syntax
int imagefontheight(int $font)
Parameters
The imagefontheight() function accepts one parameter −
$font − The font identifier. This can be:
- Built-in fonts: Integer values 1, 2, 3, 4, or 5 (predefined font sizes)
-
Custom fonts: Font resource loaded using
imageloadfont()function
Return Value
Returns the pixel height of the specified font as an integer.
Example 1: Single Font Height
Here's a basic example to get the height of a built-in font −
<?php
// Get font height for built-in font size 3
echo 'Font height: ' . imagefontheight(3);
?>
Font height: 13
Example 2: All Built-in Font Heights
This example demonstrates the heights of all built-in fonts −
<?php
// Get font heights for all built-in fonts (1-5)
echo 'Font height for font value 1 is: ' . imagefontheight(1) . "<br>";
echo 'Font height for font value 2 is: ' . imagefontheight(2) . "<br>";
echo 'Font height for font value 3 is: ' . imagefontheight(3) . "<br>";
echo 'Font height for font value 4 is: ' . imagefontheight(4) . "<br>";
echo 'Font height for font value 5 is: ' . imagefontheight(5) . "<br>";
?>
Font height for font value 1 is: 8 Font height for font value 2 is: 13 Font height for font value 3 is: 13 Font height for font value 4 is: 16 Font height for font value 5 is: 15
Font Size Reference
| Font Value | Height (pixels) | Typical Use |
|---|---|---|
| 1 | 8 | Very small text |
| 2 | 13 | Small text |
| 3 | 13 | Regular text |
| 4 | 16 | Medium text |
| 5 | 15 | Large text |
Conclusion
The imagefontheight() function is essential for calculating text positioning and layout in GD image operations. Use it alongside imagefontwidth() for precise text placement in dynamically generated images.
