GD and Image 函数
在线手册:中文  英文

imageloadfont

(PHP 4, PHP 5)

imageloadfont载入一新字体

说明

int imageloadfont ( string $file )

imageloadfont() 加载一个用户定义的位图字体并返回该字体的标识符(其值总是大于 5,因此不会和内置字体冲突)。 在产生错误的情况下,该函数返回 FALSE

字体文件格式目前是二进制的且和平台有关。这意味着应该用和你运行 PHP 的机器相同类型 CPU 的机器生成字体。

字体文件格式
字节位置 C 数据类型 说明
byte 0-3 int 字体中的字符数目
byte 4-7 int 字体中第一个字符的值(通常是 32 代表空格)
byte 8-11 int 每个字符宽度的像素值
byte 12-15 int 每个字符高度的像素值
byte 16- char 字符数据的数组,每字符中每像素一字节,一共 (nchars*width*height) 字节。

Example #1 使用 imageloadfont

<?php
header
("Content-type: image/png");
$im imagecreatetruecolor(5020);
$black imagecolorallocate($im000);
$white imagecolorallocate($im255255255);
imagefilledrectangle($im004919$white);
$font imageloadfont("04b.gdf");
imagestring($im$font00"Hello"$black);
imagepng($im);
?>

参见 imagefontwidth()imagefontheight()


GD and Image 函数
在线手册:中文  英文

用户评论:

siker at norwinter dot com (2005-08-28 18:37:30)

Working under the assumption that the only 'architecture dependant' part of the font files is endianness, I wrote a quick and dirty Python script to convert between the two. It has only been tested on a single font on a single machine so don't bet your life on it working. All it does is swap the byte order of the first four ints.
#!/usr/bin/env python
f = open("myfont.gdf", "rb");
d = open("myconvertedfont.gdf", "wb");
for i in xrange(4):
b = [f.read(1) for j in xrange(4)];
b.reverse();
d.write(''.join(b));
d.write(f.read());
I successfully used this script to convert anonymous.gdf, from one of the font links below, into something useable on Mac OS X.

matthew at exanimo dot com (2005-08-15 10:28:47)

Remember - GD fonts aren't antialiased. If you're planning on using a pre-existing (TrueType) font, you may want to consider using imagettftext() instead of phillip's function.

alex at bestgames dot ro (2005-07-02 05:01:56)

Confirmation code generation for preventing automated registrations on a website.

Function arguments are:
$code - the code that you shall random generate
$location - relative location of the image that shall be generated
$fonts_dir - relative location for the GDF fonts directory

This function will create an image with the code given by you and will save it in the directory specified with the name formed by MD5 hash of the code.

You may insert as many font types in the fonts directory as you wish, with random names.

<?php
function generate_image($code$location$fonts_dir)
{
     
$image  imagecreate(15060);           
     
imagecolorallocate($imagerand(0100), rand(100150), rand(150250));
     
$fonts scandir($fonts_dir);
     
     
$max count($fonts) - 2;
     
     
$width 10;
     for (
$i 0$i <= strlen($code); $i++)
     {     
         
$textcolor imagecolorallocate($image255255255);
         
$rand rand(2$max);
         
$font imageloadfont($fonts_dir."/".$fonts[$rand]);
         
         
$fh imagefontheight($font);
         
$fw imagefontwidth($font);

         
imagechar($image$font$widthrand(1050 $fh), $code[$i], $textcolor);     
          
$width $width $fw;
        
     }
             
     
imagejpeg($image$location."/".md5($code).".jpg"100);
     
imagedestroy($image);       
    
     return 
$code;
     
}

?>

puremango dot co dot uk at gmail dot com (2005-04-22 05:54:16)

I've written an online tool in PHP that allows you to create GD fonts from PNG images.
much usefulness for custom font creation, I feel.
see the tool@
http://puremango.co.uk/cm_fontmaker_114.php
(source available online)

null at phpmix dot com (2004-12-22 09:59:30)

Sometime ago I wrote a small tutorial on how to create dynamic signatures using gd_fonts. I also uploaded some gd_fonts...
You can check it out here, if you wish:
http://www.phpmix.com/phpBB2/viewtopic.php?t=328
Hope that helps

widget at oneblacksheep dot com (2004-06-05 05:41:41)

After noting the gd fonts page from dryes58 above was down I contacted the him and have put the pages up at http://www.widgnet.com/gdf_fonts/ hows about that then =)

angryziber at mail dot com (2000-08-23 07:23:55)

You all should look at the GD image library site for information on extra fonts, it can be found at http://www.boutell.com/gd/

易百教程