PHPマニュアル/PEARマニュアル | ユーザフォーラムで議論/質問 | マニュアル検索 | ハイライト | ハイライトオフ | ポータル | php spot

imagettfbbox - TypeType フォントを使用したテキストの bounding box を生成する | JavaScript入門&応用&リファレンスなら「JavaScriptist」

  

imagettfbbox

(PHP 4, PHP 5, PHP 7)

imagettfbboxTypeType フォントを使用したテキストの bounding box を生成する

説明

array imagettfbbox ( float $size , float $angle , string $fontfile , string $text )

この関数は TrueType テキストの bounding box をピクセル単位で計算して 返します。

パラメータ

size

フォントサイズ。

注意: GD 1 ではピクセル数、GD 2 ではポイント数で指定します。

angle

測定する text の角度(度単位)。

fontfile

The path to the TrueType font you wish to use.

Depending on which version of the GD library PHP is using, when fontfile does not begin with a leading / then .ttf will be appended to the filename and the library will attempt to search for that filename along a library-defined font path.

When using versions of the GD library lower than 2.0.18, a space character, rather than a semicolon, was used as the 'path separator' for different font files. Unintentional use of this feature will result in the warning message: Warning: Could not find/open font. For these affected versions, the only solution is moving the font to a path which does not contain spaces.

In many cases where a font resides in the same directory as the script using it the following trick will alleviate any include problems.

<?php
// Set the enviroment variable for GD
putenv('GDFONTPATH=' realpath('.'));

// Name the font to be used (note the lack of the .ttf extension)
$font 'SomeFont';
?>

注意:

Note that open_basedir does not apply to fontfile.

text

測定する文字列。

返り値

imagettfbbox() は、テキストの bounding box を 作成するための 4 点を表現する 8 個の要素からなる配列を返します。 エラー時には FALSE を返します。
キー 内容
0 左下角の X 座標
1 左下角の Y 座標
2 右下角の X 座標
3 右下角の Y 座標
4 右上角の X 座標
5 右上角の Y 座標
6 左上角の X 座標
7 左上角の Y 座標

各点の位置は、 angle にかかわらず text からの相対位置で表されます。 つまり、"左上"はテキストを水平に見た場合の左上の角を意味します。

例1 imagettfbbox() の例

<?php
// 300x150 の画像を作成します
$im imagecreatetruecolor(300150);
$black imagecolorallocate($im000);
$white imagecolorallocate($im255255255);

// 背景を白に設定します
imagefilledrectangle($im00299299$white);

// フォントファイルへのパス
$font './arial.ttf';

// まず最初のテキスト用のバウンディングボックスを作成します
$bbox imagettfbbox(1045$font'Powered by PHP ' phpversion());

// X 座標と Y 座標
$x $bbox[0] + (imagesx($im) / 2) - ($bbox[4] / 2) - 25;
$y $bbox[1] + (imagesy($im) / 2) - ($bbox[5] / 2) - 5;

// 書き込みます
imagettftext($im1045$x$y$black$font'Powered by PHP ' phpversion());

// 次に 2 番目のテキスト用のバウンディングボックスを作成します
$bbox imagettfbbox(1045$font'and Zend Engine ' zend_version());

// 最初のテキストに続ける座標を設定します
$x $bbox[0] + (imagesx($im) / 2) - ($bbox[4] / 2) + 10;
$y $bbox[1] + (imagesy($im) / 2) - ($bbox[5] / 2) - 5;

// 書き込みます
imagettftext($im1045$x$y$black$font'and Zend Engine ' zend_version());

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

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

注意

注意: この関数は、PHP が FreeType サポート (--with-freetype-dir=DIR ) を有効にしてコンパイルされている場合のみ使用可能です。

参考

  • imagettftext() - TrueType フォントを使用してテキストを画像に書き込む
  • imageftbbox() - freetype2 によるフォントを用いたテキストを囲む箱を取得する


忘却曲線を使ってこの関数を確実に記憶に残す

フォーラムで「imagettfbbox - TypeType フォントを使用したテキストの bounding box を生成する」について話す
各種マニュアル: PHPマニュアル | PEARマニュアル | Smarty(英語)マニュアル | PHP-GTKマニュアル | imagettfbbox - TypeType フォントを使用したテキストの bounding box を生成する」をGoogle検索
copyright © 1997-2024 PHP ドキュメント作成グループ(ライセンス). provided by php spot. マニュアル: