imageloadfont

(PHP 4, PHP 5, PHP 7, PHP 8)

imageloadfont载入新字体

说明

imageloadfont(string $filename): GdFont|false

imageloadfont() 加载用户定义的点阵字体并返回其标识符。

参数

filename

字体文件格式目前是二进制且依赖于体系结构。这意味着应该用跟运行 PHP 相同类型 CPU 的机器生成字体。

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

返回值

返回 GdFont 实例, 或者在失败时返回 false

更新日志

版本 说明
8.1.0 现在返回 GdFont 实例;之前返回 int

示例

示例 #1 imageloadfont() 用法示例

<?php
// Create a new image instance
$im = imagecreatetruecolor(50, 20);
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 255, 255, 255);

// Make the background white
imagefilledrectangle($im, 0, 0, 49, 19, $white);

// Load the gd font and write 'Hello'
$font = imageloadfont('./04b.gdf');
imagestring($im, $font, 0, 0, 'Hello', $black);

// Output to browser
header('Content-type: image/png');

imagepng($im);
imagedestroy($im);
?>

参见

添加备注

用户贡献的备注 3 notes

up
5
siker at norwinter dot com
19 years ago
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.
up
1
alex at bestgames dot ro
19 years ago
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(150, 60);
imagecolorallocate($image, rand(0, 100), rand(100, 150), rand(150, 250));
$fonts = scandir($fonts_dir);

$max = count($fonts) - 2;

$width = 10;
for (
$i = 0; $i <= strlen($code); $i++)
{
$textcolor = imagecolorallocate($image, 255, 255, 255);
$rand = rand(2, $max);
$font = imageloadfont($fonts_dir."/".$fonts[$rand]);

$fh = imagefontheight($font);
$fw = imagefontwidth($font);

imagechar($image, $font, $width, rand(10, 50 - $fh), $code[$i], $textcolor);
$width = $width + $fw;

}

imagejpeg($image, $location."/".md5($code).".jpg", 100);
imagedestroy($image);

return
$code;

}

?>
up
0
matthew at exanimo dot com
19 years ago
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.
To Top