PHP 8.3.4 Released!

imagefontheight

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

imagefontheightフォントの高さを取得する

説明

imagefontheight(GdFont|int $font): int

指定されたフォントの文字の高さをピクセル単位で返します。

パラメータ

font

latin2 エンコーディングの組み込みのフォントの場合は 1, 2, 3, 4, 5 のいずれか (数字が大きなほうが、より大きいフォントに対応します)、あるいは imageloadfont() が返した、GdFont クラスのインスタンスのいずれか。

戻り値

フォントの高さをピクセルで返します。

変更履歴

バージョン 説明
8.1.0 引数 font は、GdFont クラスのインスタンスと数値を両方受け入れるようになりました。これより前のバージョンでは、数値のみを受け入れていました。

例1 組み込みフォントでの imagefontheight() の使用法

<?php
echo 'フォントの高さ: ' . imagefontheight(4);
?>

上の例の出力は、 たとえば以下のようになります。

フォントの高さ: 16

例2 imagefontheight()imageloadfont() を組み合わせた使用法

<?php
// .gdf フォントを読み込みます
$font = imageloadfont('anonymous.gdf');

echo
'フォントの高さ: ' . imagefontheight($font);
?>

上の例の出力は、 たとえば以下のようになります。

フォントの高さ: 43

参考

add a note

User Contributed Notes 1 note

up
1
dev at numist dot net
19 years ago
This library function is very useful for variable-sized images that only contain text, like this function that I use to output error messages that accumulate and cause a fatal error in my thumbnailer:

<?php
function errimg($error) {
// $error is an array of error messages, each taking up one line
// initialization
$font_size = 2;
$text_width = imagefontwidth($font_size);
$text_height = imagefontheight($font_size);
$width = 0;
// the height of the image will be the number of items in $error
$height = count($error);

// this gets the length of the longest string, in characters to determine
// the width of the output image
for($x = 0; $x < count($error); $x++) {
if(
strlen($error[$x]) > $width) {
$width = strlen($error[$x]);
}
}

// next we turn the height and width into pixel values
$width = $width * $text_width;
$height = $height * $text_height;

// create image with dimensions to fit text, plus two extra rows and
// two extra columns for border
$im = imagecreatetruecolor($width + ( 2 * $text_width ),
$height + ( 2 * $text_height ) );
if(
$im) {
// image creation success
$text_color = imagecolorallocate($im, 233, 14, 91);
// this loop outputs the error message to the image
for($x = 0; $x < count($error); $x++) {
// imagestring(image, font, x, y, msg, color);
imagestring($im, $font_size, $text_width,
$text_height + $x * $text_height, $error[$x],
$text_color);
}
// now, render your image using your favorite image* function
// (imagejpeg, for instance)
out($im, array(), $error);
} else {
// image creation failed, so just dump the array along with extra error
$error[] = "Is GD Installed?";
die(
var_dump($error));
}
}
?>

The function expects an array of error messages to be passed in, and then outputs an image containing the contents of the array. This is especially useful if your code is contained in an html page that will display rexes if the images do not render correctly.

This function displays the array in image form with index 0 at the top, and the highest index at the bottom.

You have to write out() yourself though, see imagejpeg, imagepng, etc for good ideas on how to write a decent output function.
To Top