PHP 8.3.4 Released!

imageloadfont

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

imageloadfont新しいフォントを読み込む

説明

imageloadfont(string $filename): GdFont|false

imageloadfont() はユーザーが定義したビットマップを読み込み、 その ID を返します。

パラメータ

filename

フォントファイル形式は現在はバイナリで、アーキクチャに依存します。 このため、PHP を実行するマシーンと同一の型の CPU 上でフォントファイルを 生成する必要があります。

フォントファイルのフォーマット
バイト位置 C 言語のデータ型 説明
0-3 バイト目 int フォント中の文字の数
4-7 バイト目 int フォント中の最初の文字の値(しばしば 空白を表す 32 となります)
8-11 バイト目 int 各文字のピクセル幅
12-15 バイト目 int 各文字のピクセル高さ
16 バイト目から char 文字データの配列、各文字のピクセルにつき1バイトで、 総数は(文字数*幅*高さ)バイトです。

戻り値

GdFont クラスのインスタンスを返します。 失敗した場合に false を返します

変更履歴

バージョン 説明
8.1.0 GdFont クラスのインスタンスを返すようになりました。 これより前のバージョンでは、数値を返していました。

例1 imageloadfont() の使用例

<?php
// 新しい画像インスタンスを作成します
$im = imagecreatetruecolor(50, 20);
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 255, 255, 255);

// 背景を白に設定します
imagefilledrectangle($im, 0, 0, 49, 19, $white);

// gd フォントを読み込み 'Hello' を書き込みます
$font = imageloadfont('./04b.gdf');
imagestring($im, $font, 0, 0, 'Hello', $black);

// ブラウザに出力します
header('Content-type: image/png');

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

参考

add a note

User Contributed Notes 4 notes

up
5
siker at norwinter dot com
18 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
18 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
18 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.
up
-16
angryziber at mail dot com
23 years ago
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/
To Top