User-defined Bitmap Fonts
According to the PHP Manual, the reference for function imageloadfont() is described in list 1.
The font used within function imagestring() is a Monospaced font (fixed width), which you can assign a new font during run-time
by using this function, imageloadfont(). However, as I experienced an unconvenience from trying to find the font files
that can be loaded by this function, and as the result ... I found nothing. So, I end up trying to understand this 'user-defined bitmap font'
described in the function reference.
Honestly, these files are pretty simple to create and very easy to use.
However, as described in the manual, these font files are 'architecture dependent' (different machines inteprete numbers in different manners - e.g.
integer 15000 can be described in 4-bytes data as '00 00 3A 98' or '98 3A 00 00'). And the size of these files are pretty 'large' because of
each pixel takes one byte, so if create a larger size font it can takes about 500kb of space as a result.
However, if you love the simplicity of the function imagestring() and prefer to use this function rather than imagettftext(), now you can enjoy
using some different font face beyond the default fonts (1-5). I convert some truetype fonts into these 'bitmap fonts', so you can load it directly from the
imageloadfont(). An example of using this font file is shown in list 2.
You can goto fonts download page to download my available fonts.
There is a great example of implementing gdf fonts and further resources avaialable here.
Notes: Some fonts are converted from variabled width fonts, it may looks a little bit weird.
Notes#2: Some fonts contain only upper case or lower case. Most fonts contains standard ascii character set (0x20 to 0x7E), however some fonts may contain full character range (0x20 to 0xFF). However, the font preview shows only standard character set (0x20-0x7E).
Notes#3: Sorry for my poor English grammar, I'm in an unstable condition writing this page :-P
list 1. imageloadfont function reference.
imageloadfont
(PHP 3, PHP 4 )
imageloadfont -- Load a new font
Description
int imageloadfont ( string file)
imageloadfont() loads a user-defined bitmap font and returns an identifier for the font (that is always greater than 5, so it will not conflict with the built-in fonts).
The font file format is currently binary and architecture dependent. This means you should generate the font files on the same type of CPU as the machine you are running PHP on.
Table 1. Font file format
| byte position |
C data type |
description |
| byte 0-3 |
int |
number of characters in the font |
| byte 4-7 |
int |
value of first character in the font (often 32 for space) |
| byte 8-11 |
int |
pixel width of each character |
| byte 12-15 |
int |
pixel height of each character |
| byte 16- |
char |
array with character data, one byte per pixel in each character, for a total of (nchars*width*height) bytes. |
list 2. an imageloadfont() example.
<?php
$font = imageloadfont('bmreceipt.gdf');
/* I use .gdf as its extension to represents "GD Fonts" */
/* But, if your server not allow .gdf, you can change it*/
/* to any extension .jpg, .bmp, .txt, .bmf, .etc */
$fontWidth = imagefontwidth($font);
$fontHeight = imagefontheight($font);
$text = 'Jackdaws Love My Big Sphinx Of Quartz 0123456789';
$im = imagecreate(strlen($text) * $fontWidth, $fontHeight);
$bgColor = imagecolorallocate($im, 255, 255, 255);
$fgColor = imagecolorallocate($im, 0, 0, 255);
imagestring($im, $font, 0, 0, $text, $fgColor);
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
?>
and the result of this example is
If you wish to make a comment to the developer: Dr.Yes or mirror this page: widget
Updated: 6th September 2004
|
|