PHP imageloadfont Function: Loading Custom Bitmap Fonts
The article explains PHP's imageloadfont function, which loads a user‑defined bitmap font and returns its identifier, details its string parameter, return values (font ID or FALSE), notes platform‑specific binary format requirements, and provides a complete code example demonstrating image creation, font loading, text rendering, and output as PNG.
Reading font
The imageloadfont function in PHP loads a user‑defined bitmap font and returns its identifier, which is always greater than 5 to avoid conflicts with built‑in fonts. If an error occurs, the function returns FALSE.
Signature: imageloadfont(string $file): int Parameter:
file – the path to the bitmap font file. The font file must be a binary format specific to the platform, so it should be generated on a machine with the same CPU architecture as the one running PHP.
Return value:
On success, the function returns the image resource identifier; on failure, it returns FALSE.
Example usage:
<?php
header("Content-type: image/png");
$im = imagecreatetruecolor(50, 20);
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 255, 255, 255);
imagefilledrectangle($im, 0, 0, 49, 19, $white);
$font = imageloadfont("04b.gdf");
imagestring($im, $font, 0, 0, "Hello", $black);
imagepng($im);
?>This script sets the content type to PNG, creates a 50×20 image, allocates black and white colors, fills the background with white, loads a custom bitmap font from 04b.gdf, draws the string "Hello" using the loaded font, and outputs the image as a PNG.
For questions about attribution, corrections or content rights, please contactand we will review it promptly.