imagefttext

(PHP 4 >= 4.0.7, PHP 5, PHP 7, PHP 8)

imagefttext β€” Наносит тСкст Π½Π° ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ ΠΏΡƒΡ‚Ρ‘ΠΌ Ρ€Π΅Π½Π΄Π΅Ρ€ΠΈΠ½Π³Π° ΡˆΡ€ΠΈΡ„Ρ‚Π° Π±ΠΈΠ±Π»ΠΈΠΎΡ‚Π΅ΠΊΠΎΠΉ FreeType 2

ОписаниС

function imagefttext(
Β Β Β Β GdImage $image,
Β Β Β Β float $size,
Β Β Β Β float $angle,
Β Β Β Β int $x,
Β Β Β Β int $y,
Β Β Β Β int $color,
Β Β Β Β string $font_filename,
Β Β Β Β string $text,
Β Β Β Β array $options = []
): array|false

Π—Π°ΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅:

Π”ΠΎ PHP 8.0.0 функция imagefttext() Π±Ρ‹Π»Π° Ρ€Π°ΡΡˆΠΈΡ€Π΅Π½Π½ΠΎΠΉ вСрсиСй Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ imagettftext(), которая Π΄ΠΎΠΏΠΎΠ»Π½ΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΈΠ²Π°Π»Π° ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ options. Начиная с PHP 8.0.0 функция imagettftext() стала псСвдонимом Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ imagefttext().

Бписок ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²

image
ΠžΠ±ΡŠΠ΅ΠΊΡ‚ GdImage, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Π²Π΅Ρ€Π½ΡƒΠ»Π° функция imagecreatetruecolor() ΠΈΠ»ΠΈ другая функция Π³Π΅Π½Π΅Ρ€Π°Ρ†ΠΈΠΈ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΉ.
size

Π Π°Π·ΠΌΠ΅Ρ€ ΡˆΡ€ΠΈΡ„Ρ‚Π° Π² типографских ΠΏΡƒΠ½ΠΊΡ‚Π°Ρ….

angle

Π£Π³ΠΎΠ» Π² градусах, 0 градусов ΠΎΠ±ΠΎΠ·Π½Π°Ρ‡Π°Π΅Ρ‚ располоТСниС тСкста слСва Π½Π°ΠΏΡ€Π°Π²ΠΎ. ΠŸΠΎΠ»ΠΎΠΆΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Π΅ значСния ΠΎΠ±ΠΎΠ·Π½Π°Ρ‡Π°ΡŽΡ‚ ΠΏΠΎΠ²ΠΎΡ€ΠΎΡ‚ тСкста ΠΏΡ€ΠΎΡ‚ΠΈΠ² часовой стрСлки. НапримСр, тСкст с ΠΏΠΎΠ²ΠΎΡ€ΠΎΡ‚ΠΎΠΌ Π½Π° 90 градусов читаСтся снизу Π²Π²Π΅Ρ€Ρ….

x

ΠšΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Ρ‹ x ΠΈ y ΠΎΠΏΡ€Π΅Π΄Π΅Π»ΡΡŽΡ‚ ΠΎΡ‚ΠΏΡ€Π°Π²Π½ΡƒΡŽ Ρ‚ΠΎΡ‡ΠΊΡƒ ΠΏΠ΅Ρ€Π²ΠΎΠ³ΠΎ символа тСкста β€” ΠΏΡ€ΠΈΠ±Π»ΠΈΠ·ΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎ Π»Π΅Π²Ρ‹ΠΉ Π½ΠΈΠΆΠ½ΠΈΠΉ ΡƒΠ³ΠΎΠ» символа. ΠžΠΏΡ€Π΅Π΄Π΅Π»Π΅Π½ΠΈΠ΅ Π±Π°Π·ΠΎΠ²ΠΎΠΉ Ρ‚ΠΎΡ‡ΠΊΠΈ этой Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠ΅ΠΉ отличаСтся ΠΎΡ‚ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ imagestring(), Π² ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΉ ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Ρ‹ x ΠΈ y ΠΎΠΏΡ€Π΅Π΄Π΅Π»ΡΡŽΡ‚ Π²Π΅Ρ€Ρ…Π½ΠΈΠΉ Π»Π΅Π²Ρ‹ΠΉ ΡƒΠ³ΠΎΠ» ΠΏΠ΅Ρ€Π²ΠΎΠ³ΠΎ символа. НапримСр, Β«Π²Π΅Ρ€Ρ…Π½ΡŽΡŽ Π»Π΅Π²ΡƒΡŽΒ» Ρ‚ΠΎΡ‡ΠΊΡƒ ΠΎΠΏΡ€Π΅Π΄Π΅Π»ΡΡŽΡ‚ ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Ρ‹ 0, 0.

y

y-ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Π°. Π­Ρ‚ΠΎ позиция Π±Π°Π·ΠΎΠ²ΠΎΠΉ Π»ΠΈΠ½ΠΈΠΈ ΡˆΡ€ΠΈΡ„Ρ‚Π°, Π² ΠΎΠ±Ρ‰Π΅ΠΌ случаС ΠΎΠ½Π° Π½Π΅ совпадаСт с низшСй Ρ‚ΠΎΡ‡ΠΊΠΎΠΉ Π² символС.

color

ИндСкс Ρ†Π²Π΅Ρ‚Π° тСкста, см. описаниС Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ imagecolorexact().

font_filename

ΠŸΡƒΡ‚ΡŒ ΠΊ TrueType-ΡˆΡ€ΠΈΡ„Ρ‚Ρƒ.

Π’ зависимости ΠΎΡ‚ вСрсии Π±ΠΈΠ±Π»ΠΈΠΎΡ‚Π΅ΠΊΠΈ GD, с ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΉ Ρ€Π°Π±ΠΎΡ‚Π°Π΅Ρ‚ PHP, Ссли Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚ font_filename Π½Π΅ начинаСтся с символа /, функция Π΄ΠΎΠ±Π°Π²ΠΈΡ‚ ΠΊ названию Ρ„Π°ΠΉΠ»Π° Ρ€Π°ΡΡˆΠΈΡ€Π΅Π½ΠΈΠ΅ .ttf, ΠΈ Π±ΠΈΠ±Π»ΠΈΠΎΡ‚Π΅ΠΊΠ° попытаСтся Π½Π°ΠΉΡ‚ΠΈ это Π½Π°Π·Π²Π°Π½ΠΈΠ΅ Ρ„Π°ΠΉΠ»Π° ΠΏΠΎ ΠΏΡƒΡ‚ΠΈ, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ опрСдСляСт Π±ΠΈΠ±Π»ΠΈΠΎΡ‚Π΅ΠΊΠ°.

Часто располоТСниС Ρ„Π°ΠΉΠ»Π° ΡˆΡ€ΠΈΡ„Ρ‚Π° Π² ΠΊΠ°Ρ‚Π°Π»ΠΎΠ³Π΅ скрипта ΠΈΡΠΊΠ»ΡŽΡ‡Π°Π΅Ρ‚ ΠΏΡ€ΠΎΠ±Π»Π΅ΠΌΡ‹ с Π²ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ΠΌ Ρ„Π°ΠΉΠ»ΠΎΠ².

<?php

// Установка ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ окруТСния для модуля GD
putenv('GDFONTPATH=' . realpath('.'));

// НазваниС ΡˆΡ€ΠΈΡ„Ρ‚Π° для написания тСкста. ΠžΠ±Ρ€Π°Ρ‚ΠΈΡ‚Π΅ Π²Π½ΠΈΠΌΠ°Π½ΠΈΠ΅, Ρ‡Ρ‚ΠΎ Π½Π°Π·Π²Π°Π½ΠΈΠ΅ Ρ„Π°ΠΉΠ»Π° Π½Π΅ содСрТит Ρ€Π°ΡΡˆΠΈΡ€Π΅Π½ΠΈΡ .ttf
$font = 'SomeFont';

?>
text

ВСкст для вставки Π² ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅.

options

Π’ΠΎΠ·ΠΌΠΎΠΆΠ½Ρ‹Π΅ значСния ΠΊΠ»ΡŽΡ‡Π΅ΠΉ массива options
ΠšΠ»ΡŽΡ‡ Π’ΠΈΠΏ Π—Π½Π°Ρ‡Π΅Π½ΠΈΠ΅
linespacing float ΠžΠΏΡ€Π΅Π΄Π΅Π»ΡΠ΅Ρ‚ мСТстрочный ΠΈΠ½Ρ‚Π΅Ρ€Π²Π°Π» ΠΏΡ€ΠΈ отрисовкС

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ значСния

Ѐункция Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ массив, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ опрСдСляСт Ρ‡Π΅Ρ‚Ρ‹Ρ€Π΅ Ρ‚ΠΎΡ‡ΠΊΠΈ обрамлСния начиная с Π½ΠΈΠΆΠ½Π΅Π³ΠΎ Π»Π΅Π²ΠΎΠ³ΠΎ ΡƒΠ³Π»Π° ΠΈ двигаясь ΠΏΡ€ΠΎΡ‚ΠΈΠ² часовой стрСлки:

0 ниТняя лСвая x-ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Π°
1 ниТняя лСвая y-ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Π°
2 ниТняя правая x-ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Π°
3 ниТняя правая y-ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Π°
4 вСрхняя правая x-ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Π°
5 вСрхняя правая y-ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Π°
6 вСрхняя лСвая x-ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Π°
7 вСрхняя лСвая y-ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Π°

Ѐункция Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ false, Ссли Π²ΠΎΠ·Π½ΠΈΠΊΠ»Π° ошибка.

Бписок измСнСний

ВСрсия ОписаниС
8.0.0 ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ image Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ ΠΏΡ€ΠΈΠ½ΠΈΠΌΠ°Π΅Ρ‚ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ GdImage; Ρ€Π°Π½ΡŒΡˆΠ΅ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ ΠΏΡ€ΠΈΠ½ΠΈΠΌΠ°Π» ΠΊΠΎΡ€Ρ€Π΅ΠΊΡ‚Π½Ρ‹ΠΉ gd-рСсурс (resource).

ΠŸΡ€ΠΈΠΌΠ΅Ρ€Ρ‹

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ отрисовки тСкста Π½Π° ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΈ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠ΅ΠΉ imagefttext()

<?php

// Π‘ΠΎΠ·Π΄Π°Π½ΠΈΠ΅ изобраТСния Ρ€Π°Π·ΠΌΠ΅Ρ€ΠΎΠΌ 300x100
$im = imagecreatetruecolor(300, 100);
$red = imagecolorallocate($im, 0xFF, 0x00, 0x00);
$black = imagecolorallocate($im, 0x00, 0x00, 0x00);

// Установка красного Ρ„ΠΎΠ½Π°
imagefilledrectangle($im, 0, 0, 299, 99, $red);

// Установка ΠΏΡƒΡ‚ΠΈ ΠΊ ttf-Ρ„Π°ΠΉΠ»Ρƒ ΡˆΡ€ΠΈΡ„Ρ‚Π°
$font_file = './arial.ttf';

// ΠžΡ‚Ρ€ΠΈΡΠΎΠ²ΠΊΠ° тСкста 'PHP Manual' ΡˆΡ€ΠΈΡ„Ρ‚ΠΎΠΌ 13-Π³ΠΎ Ρ€Π°Π·ΠΌΠ΅Ρ€Π°
imagefttext($im, 13, 0, 105, 55, $black, $font_file, 'PHP Manual');

// Π’Ρ‹Π²ΠΎΠ΄ изобраТСния
header('Content-Type: image/png');

imagepng($im);

?>

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ

Π—Π°ΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅: Π­Ρ‚Π° функция доступна Ρ‚ΠΎΠ»ΡŒΠΊΠΎ Π² случаС, Ссли PHP Π±Ρ‹Π» скомпилирован с ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠΎΠΉ freetype (--with-freetype-dir=DIR)

Π‘ΠΌΠΎΡ‚Ρ€ΠΈΡ‚Π΅ Ρ‚Π°ΠΊΠΆΠ΅

  • imageftbbox() - ΠžΠΏΡ€Π΅Π΄Π΅Π»ΡΠ΅Ρ‚ Π³Ρ€Π°Π½ΠΈΡ†Ρ‹ ΠΏΡ€ΡΠΌΠΎΡƒΠ³ΠΎΠ»ΡŒΠ½ΠΎΠ³ΠΎ обрамлСния для тСкста, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Π±ΠΈΠ±Π»ΠΈΠΎΡ‚Π΅ΠΊΠ° freetype2 Π²Ρ‹Π²ΠΎΠ΄ΠΈΡ‚ Π½Π° экран ΠΏΡƒΡ‚Ρ‘ΠΌ Ρ€Π΅Π½Π΄Π΅Ρ€ΠΈΠ½Π³Π° ΡˆΡ€ΠΈΡ„Ρ‚Π°
  • imagettftext() - РисуСт тСкст Π½Π° ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΈ ΡˆΡ€ΠΈΡ„Ρ‚ΠΎΠΌ TrueType
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 15 notes

up
2
MagicalTux at FF dot st ΒΆ
21 years ago
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 ...
up
2
ben at tNOSPAManjNOSPAMo dot cnospamordots dot om ΒΆ
23 years ago
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);

Recompile GD, and vo?la: beauteous antialiasing.
up
2
KeepSake at crimebloc dot com ΒΆ
16 years ago
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 hope this is of some use to someone.
- KeepSake

<?php
// Required header (assuming we use png images)
header("Content-type: image/png");

// Create a basic image with a dark background.
$image = imagecreatetruecolor(300, 20);
imagefill($image, 0, 0, imagecolorallocate($image, 21, 21, 21));

// Function call, arguments are the same as imagefttext, expect that (array)$extrainfo takes a new paramenter called character_spacing.
$imageBox = imagefttext2($image, 9, 0, 2, 15, imagecolorallocate($image, 255, 255, 255), 'tahomabold.ttf', 'The quick brown fox...', array('character_spacing' => 5));

// Output the file, and clear the resources
imagepng($image);
imagedestroy($image);

function imagefttext2($imageResource, $font_size, $text_angle, $start_x, $start_y, $color, $font_file, $text, $extra_info = array()) {
    if($extra_info['character_spacing'] == NULL || !is_numeric($extra_info['character_spacing'])) {
        $extra_info['character_spacing'] = 0;
    }
    $lastX = $start_x - $extra_info['character_spacing'];
    foreach(str_split($text) as $v) {
        $coordinates = imagefttext($imageResource, $font_size, $text_angle, $lastX + $extra_info['character_spacing'], $start_y, $color, $font_file, $v, $extra_info);
        $lastX = max($coordinates[2], $coordinates[4]);
    }
    // Return the newly generated image box coordinates:
    return array($start_x, $start_y, $coordinates[2], $coordinates[3], $coordinates[4], $coordinates[5], $start_x, $coordinates[7]);
}

?>
up
1
darren at badpun dot co dot uk ΒΆ
19 years ago
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:

<?php
/* 100mm x 100mm image */
$imageWidth = 100;
$imageHeight = 100;

/* 300 dpi image, therefore image is 1181 x 1181 pixels */
$imageDPI = 300;

/* unless we do this, text will be about 3 times too small */
$realFontSize = ($fontPt * $targetDPI) / 96;
?>
up
2
aidan at php dot net ΒΆ
21 years ago
This function is very simular to imageffttext(), you may find the information provided on its manual page helpful:

http://php.net/imagettftext
up
1
sebastiand at gmx dot de ΒΆ
22 years ago
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-ALIASING
ImageFTText ($neuesBild,$fontsize,$fontangle,$TextPosX,$TextPosY,
-$custom_fg,$fonttype,$text,array());
up
0
d underscore brown x at hotmail dot ΒΆ
18 years ago
realpath(".")
realpath(getenv("SCRIPT_FILENAME"));

could be different.  This helped when setting GDFONTPATH.
up
0
Anonymous ΒΆ
20 years ago
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).

<?php
    list($x, $y, $maxwidth) = array(0, 0, 0);

    $fsize = (int)$_REQUEST['fsize'];
    if ($fsize < 5 or $fsize > 48) $fsize = 8;

    header("Content-type: image/jpeg");

    // don't know how wide or tall the font samples will be.
    // create a huge image for now, we'll copy it smaller
    // later when we know how large the image needs to be.
    $im = imagecreate(1000, 20000) or die('could not create!');
    $clr_white = imagecolorallocate($im, 255, 255, 255);
    $clr_black = imagecolorallocate($im, 0, 0, 0);

    $font_path = "C:/Windows/Fonts/";
    $dh = opendir($font_path);
    while (($file = readdir($dh)) !== FALSE) {
        // we're only dealing with TTY fonts here.
        if (substr(strtolower($file), -4) != '.ttf') continue;

        $str = "Sample text for '$file'";
        $bbox = imagettfbbox(
            $fsize, 0, "{$font_path}{$file}", $str
        );
        $ww = $bbox[4] - $bbox[6];
        $hh = $bbox[1] - $bbox[7];

        imagettftext(
            $im, $fsize, 0, $x, $y,
            $clr_black, "{$font_path}{$file}", $str
        );

        $y += $hh + 20;
        if ($ww > $maxwidth) $maxwidth = $ww;
    }

    closedir($dh);

    // ok, now we can chop off the extra space from the
    // 1000 x 20000 image.
    $im2 = imagecreate($maxwidth + 20, $y);
    imagecopyresized(
        $im2, $im, 0, 0, 0, 0, $maxwidth + 20,
        $y, $maxwidth + 20, $y
    );
    imagejpeg($im2);
    imagedestroy($im);
    imagedestroy($im2);
?>
up
0
vsazel at atlas dot cz ΒΆ
20 years ago
If you want to get the best result in monochrome font rendering, change render_mode to FT_LOAD_RENDER. It's the last parameter of FT_Load_Glyph() function (in gdft.c).
up
0
dnf at seznam dot cz ΒΆ
20 years ago
For negative image you must add one line after the $grayColor computation:

$grayColor =  ~ $grayColor & 0x7FFFFFF;
up
0
kagaku at gmail dot com ΒΆ
21 years ago
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:

<?
  function align_right($string, $fontfile, $imgwidth, $fontsize){
    $spacing = 0;
    $line = array("linespacing" => $spacing);
    list($lx,$ly,$rx,$ry) = imageftbbox($fontsize,0,$fontfile,$string,$line);
    $textwidth = $rx - $lx;
    $imw = ($imgwidth-10-$textwidth);
    return $imw;
  }
?>
up
0
eshenk at comcast dot net ΒΆ
22 years ago
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.

<?php

srand((double)microtime()*1234567); // Start the random gizmo
$image = imagecreatefromjpeg(rand(1,exec('ls *.jpg | wc -l')) . ".jpg"); // Get a background
$font = rand(1,exec('ls *.ttf | wc -l')) . ".ttf"; // Get a font
$textcolor = imagecolorallocate($image,0,0,0); // Set text color

$text1 = "shenko.homedns.org"; // Here is our text

imagettftext($image, 50, 0, 20, 50, $textcolor, $font, $text1); // Write the text with a font

header("Content-type: image/jpeg"); // Its a JPEG
imagejpeg($image,'',90); // Zap it to the browser
imagedestroy($image); // Memory Freeupage

?>
up
-1
cory at lavacube dot com ΒΆ
19 years ago
Since this function is not documented, I felt it was best that I shed some light on the extrainfo parameter.

You can see the full documentation at the GD reference manual:
http://www.boutell.com/gd/manual2.0.33.html#gdImageStringFTEx

Basically it accepts an array containing the following options as keys and an associated value:
(int) flags [more info in the GD reference manual]
(double/float) linespacing
(int) charmap
(int) hdpi
(int) vdpi
(string) xshow
(string) fontpath

My C/C++ is not very good but this is the best I can explain. Read the documentation for more information. :-)

A very simple example of usage would be:

<?php

imagefttext( $img_pointer, 12, 0, 10, 10, [-insertsomecolour-], '/path/to/font.ttf', "THIS IS A TEST\nTHIS IS LINE 2\nTHIS IS LINE3", array('lineheight'=>2.0) );

?>
up
-1
jwilliam at kcr dot uky dot edu ΒΆ
23 years ago
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.

Code change follows:
$fontfile="/usr/local/fonts/ttf/bookantbd.ttf";
// Waterfall of point sizes to see what Freetype 2's autohinting looks like:
//
for($i=4;$i<=12;$i++){
  ImageFtText($image,$i,0,10,(280+$i*14),$forecolor,$fontfile, bookantbd . $i . ". " . $string, array("linespacing" => 1.0));
}

John
up
-1
php@davehirschD0TK0MM ΒΆ
16 years ago
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:

<?php
    // Function that draws rotated text by creating a temporary image and rotating it, since rotated text appears to be busted
    function imageTextRotated($image, $size, $angle, $x, $y, $inColor, $fontfile, $text, $info=array()) {
//        Force some demo text that contains risers and descenders:
//        $text = "Nlfbacejygq!";

        $bbox = imageftbbox($size, 0, $fontfile, $text, $info);
        $dropdown = $size*0.3;
        $xsize = abs($bbox[2] - $bbox[0]);
        $ysize = abs($bbox[5] - $bbox[3]);
        $tmpImage = imagecreatetruecolor($xsize*1.25, $ysize*1.25);        // need the extra space to accommodate risers and descenders
        $transparent = imagecolorallocate($tmpImage, 255, 255, 154);
        if (!$transparent) {
            error_log("Color allocate failed");
        }
        imagecolortransparent($tmpImage, $transparent);
        if (!imagefill($tmpImage, 0, $ysize, $transparent)) {
            error_log("Fill failed");
        }
        $rgb = imagecolorsforindex($image, $inColor);
        $color = imagecolorexact($tmpImage, $rgb['red'], $rgb['green'], $rgb['blue']);
        if ($color == -1) {
            $color = imagecolorallocate($tmpImage, $rgb['red'], $rgb['green'], $rgb['blue']); 
            if (!$color) {
                error_log("Color allocate 2 failed");
            }
        }

        $newbbox = imagefttext($tmpImage, $size, 0, 0, $ysize*1.0, $color, $fontfile, $text, $info);
        $tmpImage = imagerotate($tmpImage, $angle, $transparent);
        $newWidth = imagesx($tmpImage);
        $newHt = imagesy($tmpImage);
        imagecopymerge($image, $tmpImage, $x-$newWidth+$dropdown, $y-$newHt, 0, 0, $newWidth, $newHt, 100);
        
//        Highlight the desired starting point (baseline) with a green dot:
//        $green = imagecolorallocate($image, 0, 251, 0);
//        imagefilledellipse($image, $x, $y, 10, 10, $green);
        imagedestroy($tmpImage);
?>

-Dave