When using imagepsbbox, keep in mind, that meaning of y-coordinates is slightly different here. Y-coordinates returned by this function are related to the baseline of the text starting at point [0,0]. Positive values represent points ABOVE the baseline, negative values represent points BELOW the baseline. That is why the lower left y-coordinate is always smaller here than the upper right y-coordinate (these two coordinates are actualy values of metrics.descent and metrics.ascent - see T1Lib docs).
So when you want to place some text using coordinates of the top left corner (for example [100,100]), use this:
<?php
$x = 100;
$y = 100;
$text = "Dodge this";
$fontsize=18;
$font=imagepsloadfont("somefont.pfb");
list($lx,$ly,$rx,$ry) = imagepsbbox($text,$font,$fontsize);
imagepstext ($someimage, $text, $font, $fontsize, $somecolor, $somecolor, $x, $y + $ry);
?>
Hope it helps someone, I got stuck with this for a while.
imagepsbbox
(PHP 4, PHP 5)
imagepsbbox — PostScript Type1 フォントを用いてテキスト矩形のバウンディングボックス を指定する
説明
array imagepsbbox ( string $text, int $font, int $size [, int $space, int $tightness, float $angle] )PostScript Type1 フォントを用いてテキスト矩形のバウンディングボックス を指定します。
バウンディングボックスは文字メトリックスから得られる情報を用いて 計算されますが、残念なことに実際に描画される文字列の描画結果とは わずかに異なる傾向があります。角度が0度の場合、全ての方向に1ピク セル分多く必要であると予想することができます。
パラメータ
- text
- font
latin2 エンコーディングの組み込みのフォントの場合は 1, 2, 3, 4, 5 のいずれか (数字が大きなほうが、より大きいフォントに対応します)、 あるいは imageloadfont() で登録したフォントの識別子のいずれか。
- size
size はピクセルで表します。
- space
フォントが占める空間のデフォルト値を変更することが可能です。 この値が元の値に付加されます。また、負の値とすることも可能です。 文字間隔の単位で表されます。 1 単位は文字の矩形の 1/1000 です。
- tightness
tightness により文字間の空白の量を制御できます。 この量は元の文字幅に追加され、負の値とすることも可能です。 文字間隔の単位で表されます。 1 単位は文字の矩形の 1/1000 です。
- angle
angle は、度で指定します。
返り値
以下の要素を持つ配列を返します。
| 0 | 左側の x 座標 |
| 1 | 上側の y 座標 |
| 2 | 右側の x 座標 |
| 3 | 下側の y 座標 |
注意
注意: この関数は、PHP が --with-t1lib を指定してコンパイルされている場合のみ使用可能です。
参考
| imagepstext() |
imagepsbbox
24-Oct-2006 06:25
18-Apr-2002 01:23
When using imagepsbbox, you are probably trying to do something like creating a button with text, so that the button is large enough for the text...
Below is a very simple example of making a black button just big enough to display white text on it.
<?php
//if text is no variable set sample text
if (!$text)
$text = "This is a sample text";
// set the font size
$fontsize=14;
// load the font to use
$font=ImagePsLoadFont("/fonts/ariam___.pfb");
//get the left lower corner and the right upper
list($lx,$ly,$rx,$ry) = imagepsbbox($text,$font,$fontsize,0,0,0);
// calculate the size of the text
$textwidth = $rx - $lx;
$textheight = $ry - $ly;
// make an image 40 pixels wider and 20 pixels higher than the text
$imh = $textheight + 20;
$imw = $textwidth + 40;
$im = imageCreate( $imw, $imh );
//define colors, first color is used as background color!
$black = ImageColorAllocate ($im, 0, 0, 0);
$white = ImageColorAllocate ($im, 255, 255, 255);
//create the text (with the same parameters as imagepsbbox!)
ImagePSText ($im, "$text", $font, $fontsize, $white, $white, 20, 20,'','','',4);
//send the header
header("Content-type: image/jpeg");
// create the image
ImageJPEG ($im,"",100);
//destroy the image & font to free memory
Imagepsfreefont ( $font );
ImageDestroy ( $im );
?>