imagepstext

(PHP 3 >= 3.0.9, PHP 4, PHP 5)

imagepstext -- 用 PostScript Type1 字体把文本字符串画在图像上

说明

array imagepstext ( resource image, string text, resource font, int size, int foreground, int background, int x, int y [, int space, int tightness, float angle, int antialias_steps] )

foreground 是文本的颜色,background 是文本以防锯齿(antialiasing)方式尝试淡入的颜色。以 background 为颜色的像素实际上不会被画上去,所以背景图像不需要是实心的颜色。

xy 给出的坐标定义了第一个字符的起点(或参考点,基本是字符的左下角)。这和 imagestring() 不同,其 xy 坐标定义的是第一个字符的右上角。如果不很理解请参考 PostScript 文档中关于字体及其度量系统的部分。

space 可以用来改变字体中默认间距的值。此值将被加到通常的值上,可以为负值。

tightness 可以控制字符之间的间距。此值将被加到通常字符宽度上,可以为负值。

angle 以角度表示。

size 以像素表示。

antialias_steps 可以控制防混色文本使用的颜色的数目。允许值为 4 和 16。大的数值推荐用于大小小于 20 的文本,对文本质量的效果相当明显。对更大的大小,用 4,计算强度小一些。

spacetightness 以字符间距单元表示,1 个单元为 1 em-square 的一千分之一。

spacetightnessangleantialias_steps 参数为可选项。

注: 本函数仅在 PHP 编译时指定了 --enable-t1lib 时可用。

本函数返回一个包含下列单元的数组:

0左下角的 X 坐标
1左下角的 Y 坐标
2右上角的 X 坐标
3右上角的 Y 坐标

参见 imagepsbbox()


add a note add a note User Contributed Notes
honza dot bartos at gmail dot com
24-Oct-2006 07:38
The coordinates given by x, y represent actually a starting point of the text baseline. They represent the lower left corner of the first character only in case that any part of the character doesn't lie below baseline (it works for "Hello" but for "Howdy" it does not - because of letter y). There may be some small differences according to the font and size chosen.
heckp at uni-trier.de
10-Jul-2003 08:11
It is important so make shure that the "text" really is a string.

imagepstext ($im, $text, $font, $textsize, $black, $white, 10, 10);

won't work if $text is undefined, so PHP will quit with an error.

so always write it like this:

imagepstext ($im, "$text", $font, $textsize, $black, $white, 10, 10);
m.confalonieri(at)mclink.it
04-Jun-2003 09:00
I found a way to let imagepstext understand 32-bit colors (RGBA) by replacing in gd.c:PHP_FUNCTION(imagepstext)

int _fg, _bg, x, y, size, space = 0, aa_steps = 4, width = 0;

with

unsigned int _fg, _bg, x, y, size, space = 0, aa_steps = 4, width = 0;
Jeroen dot Straahof at newego dot nl
07-Feb-2003 10:12
I made a function that makes it easy to align text to the right
of an image. Below you can find the code because for me it
works great. You can also use it to center text as well, if you
like to have that simply remove the -10 and split the result
of $imgwidth and $texwidth

function AlignRight($string, $font, $imgwidth, $fontsize) {
list($lx,$ly,$rx,$ry) = imagepsbbox($string,$font,$fontsize,0,0,0);
$textwidth = $rx - $lx;
$imw = ($imgwidth-10-$textwidth);
return $imw;
}
johan (at) 1way2print.net
06-Nov-2002 06:17
If you use fonts with special chars, remeber to read in the encoded file *.enc with imagepsencodefont ... etc. for Danish, Swedish, German.....
a at url dot de
18-Feb-2002 10:16
a note on kernnig:
t1lib tries to load a corresponding afm file in the directory of the font file.
it does this by replacing the extension (.PFB .pfb) with ".afm". note that this has to be a lowercase afm! usually windows-ps-fonts have file names in all-uppercase, so try renaming the *.AFM file to *.afm.

i also noticed that sometimes this gives an error -2. it seems like t1lib chokes on windows-linebreaks in the afm file.

try 'recode dos..lat1 fontfile.afm' and check again.

if it all works, combinations like "Ta" or "Te" should show the second letter slightly moved to the capital T (on normal fonts like Times anyway).
npdavis at hotmail dot com
25-May-2001 12:09
If you have a programming error in your code, using ImagePsText, sometimes t1lib crashes with an unrecoverable error. Because of this, httpd needs to be restarted.

This problem only occurs if there is a programming error, but can drive you crazy when debugging, if you don't know about it. By killing the parent process(httpd) you "reset" t1lib. When debugging, if you get an error then make a code change, restart httpd before testing it again. This will save hours of frustration. Make it a habit to just restart httpd after every error, and you will be much happier.

Also, to see debugging messages, (if you are rendering x's) comment the jpeg header, and the ImageJpeg statement. You will get beautiful error messages. When you get a blank page, your ImagePsText code is working correctly. Uncomment the Header() and ImageJpeg() and see what you have. You might be passing parameters that render a white image with no text, but the code is working!

Numeric t1lib error messages can be decoded using the t1lib_doc.pdf file that comes with t1lib. PHP simply relays these errors from t1lib to the page. Please don't ask the PHP people about these errors... t1lib has beautiful documentation.... use it: )

thx,
Neil
npdavis at hotmail dot com
24-May-2001 11:56
One other thing... variables. It is important to convert variables to the proper type before they hit this function. In this respect, ImageTtfText is easier to work with. IE, if you pass a font size from one page to the next, via GPC, it becomes a string type. Sooo... use IntVal() to convert it to an integer type that ImagePsText can digest. In addition you must convert HTML special characters or use chr() to represent special characters. ImagePsFont will not decode   to represent a space, use Chr(32) or a space:

$fontsize=IntVal($fontsize);
$font=ImagePsLoadFont("fonts/IntR.pfb");
ImagePsText($im, $textstring, $font, $fontsize, $textcolor, $background, 0, $fontsize,'','','',16); //note antialias is set
ImagePsFreeFont($font);

thx,
Neil
npdavis at hotmail dot com
22-May-2001 08:54
with a font included in t1lib:

<?PHP
Header
("Content-type: image/jpeg");
$im = ImageCreate (350, 45);
$black = ImageColorAllocate ($im, 0, 0, 0);
$white = ImageColorAllocate ($im, 255, 255, 255);
$font=ImagePsLoadFont("bchbi.pfb");
ImagePsText($im, "Testing... It worked!",
$font, 32, $white, $black, 32, 32);
ImagePsFreeFont($font);
ImageJpeg($im, "", 100);//for best quality... your mileage may vary
ImageDestroy ($im);
?>

Better than using freetype, but with freetype2, the difference is marginal. To flip backround and foreground colors, alternate the order of ImageColorAllocate statements.
 
If you get outlines (the antialiasing produces these) reverse the $black and $white color identifiers in the ImagePsText function.

Happy PostScripting!

thx,
Neil