Elenco dei parametri

The angle in degrees, with 0 degrees being left-to-right reading text.
Higher values represent a counter-clockwise rotation. For example, a
value of 90 would result in bottom-to-top reading text.

x

The coordinates given by x and
y will define the basepoint of the first
character (roughly the lower-left corner of the character). This
is different from the imagestring(), where
x and y define the
upper-left corner of the first character. For example, "top left"
is 0, 0.

y

The y-ordinate. This sets the position of the fonts baseline, not the
very bottom of the character.

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 GDputenv('GDFONTPATH=' . realpath('.'));

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

User Contributed Notes 15 notes

For a design project I am required to have spacing between characters; since imagefttext does not support this feature I have created a function which does support this.

The arguments are identical to imagefttext, accept that (array)$extrainfo now accepts the 'character_spacing' spacing parameter. The return values are as expected, and include the image boundaries of the entire string including the character spacing.

The downside is that $angle rotates each letter instead of rotating the entire word (could be seen as a feature on its own).

I wrote a bit of code to gather all the .ttf files in the directory with this script, and randomize them to write text on a header image for my site. The only catch is the font files have to be named 1.ttf, 2.ttf etc etc.

I'm not sure if this is a PHP issue or an GD issue, but after upgrading to PHP 5.3.2, text written at an angle has become top-justified (so "N" and "n" have the same top, but the bottom of the "N" is lower than the bottom of the "n". I've written a kludgy work-around, which writes the text to a non-rotated temporary image, then copies the temporary image, rotated onto the main image. The kludginess is to get around the fact that I can't seem to extract the font info, particularly the distance between the baseline and the very bottom (I've hard-coded it as 30% of the font size)I hope the bug can be fixed (if it is indeed a bug) or that others can improve this code:

I am using php 5.1.2 on a winxp machine. I was getting into the TrueType fonts and wanted to see which ones would look best incorporated into web images. So I created the following script that prints out samples of all the TrueType fonts found in my C:\Windows\Fonts directory. The script takes only one request parameter - 'fsize'. It stands for font-size and lets you see each font in any size you wish -- I limited it to values between 5 and 48. Hope this helps someone other than me :)

I apologize in advance if any of my code is not the prettiest-written php code even seen -- I have only been coding in php for the past week (I'm a perl-guy usually).

When compiling PHP with FreeType 2 support, you'll probably have some problems if - for example - you use debian and didn't compile freetype2 yourself...If configure fails after saying "If configure fails, try --with-xpm-dir..." you most likely have FreeType1 installed, but not freetype2 ...

Do this as root :apt-get install libfreetype6-dev

It took me some time to find out that apt-get install freetype2 is actually installing freetype1 ...

I found myself in need of an align right function and found one on the imagepstext manual page. I can't imagine I'm the only person who's needed to use this, so here's a slightly modified version that works with imagefttext:

After spending the evening with some work on automatically generated images, I had the idea to switch of anti-aliasing (looking, if some font would look better that way), which turned out not to be quite so easy.

Actually you have to use the negative of the desired color to switch of antialising. I include the corresponding line from my code (line split up):

// USE NEGATIVE OF DESIRED COLOR TO SWITCH OF ANTI-ALIASINGImageFTText ($neuesBild,$fontsize,$fontangle,$TextPosX,$TextPosY,-$custom_fg,$fonttype,$text,array());

Thanks for the script! I modified it to show several fonts that I was wanting to use. I am using GD-2.0.7, FreeType-2.1.3(text rotation fix,among others), and PHP-4.2.3 and had to include the array information to get it to work.

If you're interested in turning off FreeType hinting, search for the following line in the gd source (gdft.c): err = FT_Load_Glyph (face, glyph_index, FT_LOAD_DEFAULT);and replace it with err = FT_Load_Glyph (face, glyph_index, FT_LOAD_NO_HINTING);

I had trouble working out how to accurately represent fonts in point sizes when constructing charts that had a user-customisable output DPI (basically, the user could specify the size of the chart in mm - or any other physical measure - and the DPI to create arbitrarily-sized charts to work properly in real printed documents).

GD1 was OK as it used pixels for font rendering, but GD2 uses points, which only makes any sense if you know the DPI that it assumes when rendering text on the image surface. I have not been able to find this anywhere in this documentation but have examined the GD2 source code and it appears to assume a DPI of 96 internally. However, this can easily be customised in the GD2 source so it cannot be assumed that all PHP interpreters out there have a GD2 compiled using 96dpi internally.

If it does, and you are using it to construct images whose target DPI is not 96, you can calculate the point size to supply to imageftbox() and imagefttext() like this: