imagettftext

(PHP 4, PHP 5, PHP 7, PHP 8)

imagettftext β€” РисуСт тСкст Π½Π° ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΈ ΡˆΡ€ΠΈΡ„Ρ‚ΠΎΠΌ TrueType

ОписаниС

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

Ѐункция наносит тСкст text ΠΏΠΎΠ²Π΅Ρ€Ρ… изобраТСния ΡˆΡ€ΠΈΡ„Ρ‚ΠΎΠΌ TrueType.

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

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

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

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

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

angle

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

x

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

y

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

color

ИндСкс Ρ†Π²Π΅Ρ‚Π°. ΠžΡ‚Ρ€ΠΈΡ†Π°Ρ‚Π΅Π»ΡŒΠ½Ρ‹ΠΉ индСкс Ρ†Π²Π΅Ρ‚Π° ΠΎΡ‚ΠΊΠ»ΡŽΡ‡Π°Π΅Ρ‚ сглаТиваниС. Π‘ΠΌΠΎΡ‚Ρ€ΠΈΡ‚Π΅ описаниС Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ imagecolorallocate().

fontfile
ΠŸΡƒΡ‚ΡŒ ΠΊ ΡˆΡ€ΠΈΡ„Ρ‚Ρƒ TrueType, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Π²Ρ‹ Ρ…ΠΎΡ‚ΠΈΡ‚Π΅ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒ. Π’ зависимости ΠΎΡ‚ Ρ‚ΠΎΠ³ΠΎ, какая Π±ΠΈΠ±Π»ΠΈΠΎΡ‚Π΅ΠΊΠ° GD Π·Π°Π³Ρ€ΡƒΠΆΠ΅Π½Π° Π² PHP, Ссли ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ fontfile Π½Π΅ начинаСтся с символа /, Ρ‚ΠΎ ΠΊ ΠΈΠΌΠ΅Π½ΠΈ Ρ„Π°ΠΉΠ»Π° Π±ΡƒΠ΄Π΅Ρ‚ Π΄ΠΎΠ±Π°Π²Π»Π΅Π½ΠΎ Ρ€Π°ΡΡˆΠΈΡ€Π΅Π½ΠΈΠ΅ .ttf ΠΈ Π±ΠΈΠ±Π»ΠΈΠΎΡ‚Π΅ΠΊΠ° Π±ΡƒΠ΄Π΅Ρ‚ ΠΏΡ‹Ρ‚Π°Ρ‚ΡŒΡΡ ΠΈΡΠΊΠ°Ρ‚ΡŒ это имя Ρ„Π°ΠΉΠ»Π° ΠΏΠΎ ΠΎΠΏΡ€Π΅Π΄Π΅Π»Ρ‘Π½Π½ΠΎΠΌΡƒ Π±ΠΈΠ±Π»ΠΈΠΎΡ‚Π΅ΠΊΠΎΠΉ ΠΏΡƒΡ‚ΠΈ ΡˆΡ€ΠΈΡ„Ρ‚ΠΎΠ². ΠŸΡ€ΠΈ Ρ€Π°Π±ΠΎΡ‚Π΅ с вСрсиями Π±ΠΈΠ±Π»ΠΈΠΎΡ‚Π΅ΠΊΠΈ GD Π½ΠΈΠΆΠ΅ 2.0.18 ΠΊΠ°ΠΊ Β«Ρ€Π°Π·Π΄Π΅Π»ΠΈΡ‚Π΅Π»ΡŒ ΠΏΡƒΡ‚Π΅ΠΉΒ» для ΠΎΡ‚Π΄Π΅Π»ΡŒΠ½Ρ‹Ρ… Ρ„Π°ΠΉΠ»ΠΎΠ² ΡˆΡ€ΠΈΡ„Ρ‚ΠΎΠ² использовался символ ΠΏΡ€ΠΎΠ±Π΅Π»Π°, Π° Π½Π΅ Ρ‚ΠΎΡ‡ΠΊΠ° с запятой. НСпрСднамСрСнноС использованиС этой особСнности ΠΏΡ€ΠΈΠ²Π΅Π΄Ρ‘Ρ‚ ΠΊ ΠΏΡ€Π΅Π΄ΡƒΠΏΡ€Π΅ΠΆΠ΄Π΅Π½ΠΈΡŽ: Warning: Could not find/open font. ЕдинствСнноС Ρ€Π΅ΡˆΠ΅Π½ΠΈΠ΅ для этих вСрсий Π±ΠΈΠ±Π»ΠΈΠΎΡ‚Π΅ΠΊ β€” ΠΏΠ΅Ρ€Π΅ΠΌΠ΅ΡΡ‚ΠΈΡ‚ΡŒ Ρ„Π°ΠΉΠ»Ρ‹ ΡˆΡ€ΠΈΡ„Ρ‚ΠΎΠ² Π² Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΎΡ€ΠΈΡŽ, имя ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΉ Π½Π΅ содСрТит ΠΏΡ€ΠΎΠ±Π΅Π»ΠΎΠ².

Часто, ΠΊΠΎΠ³Π΄Π° ΡˆΡ€ΠΈΡ„Ρ‚ Π»Π΅ΠΆΠΈΡ‚ Π² Ρ‚ΠΎΠΌ ΠΆΠ΅ ΠΊΠ°Ρ‚Π°Π»ΠΎΠ³Π΅, Ρ‡Ρ‚ΠΎ ΠΈ скрипт PHP, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Ρ€Π°Π±ΠΎΡ‚Π°Π΅Ρ‚ с этим ΡˆΡ€ΠΈΡ„Ρ‚ΠΎΠΌ, ΠΏΠΎΠΌΠΎΠ³Π°Π΅Ρ‚ ΡΠ»Π΅Π΄ΡƒΡŽΡ‰ΠΈΠΉ Ρ‚Ρ€ΡŽΠΊ.

<?php

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

// Имя ΡˆΡ€ΠΈΡ„Ρ‚Π° для использования (ΠΎΠ±Ρ€Π°Ρ‚ΠΈΡ‚Π΅ Π²Π½ΠΈΠΌΠ°Π½ΠΈΠ΅, Ρ‡Ρ‚ΠΎ Ρ€Π°ΡΡˆΠΈΡ€Π΅Π½ΠΈΠ΅ .ttf Π½Π΅ указываСтся)
$font = 'SomeFont';

Π—Π°ΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅: ΠžΠ±Ρ€Π°Ρ‚ΠΈΡ‚Π΅ Π²Π½ΠΈΠΌΠ°Π½ΠΈΠ΅, Ρ‡Ρ‚ΠΎ open_basedir Π½Π΅ примСняСтся ΠΊ fontfile.

text

ВСкстовая строка Π² ΠΊΠΎΠ΄ΠΈΡ€ΠΎΠ²ΠΊΠ΅ UTF-8.

Для доступа ΠΊ символам ΡˆΡ€ΠΈΡ„Ρ‚Π° Π·Π° ΠΏΡ€Π΅Π΄Π΅Π»ΠΎΠΌ 127-ΠΉ ΠΏΠΎΠ·ΠΈΡ†ΠΈΠΈ Π² Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚ Π²ΠΊΠ»ΡŽΡ‡Π°ΡŽΡ‚ ссылки Π½Π° дСсятичныС Ρ†ΠΈΡ„Ρ€ΠΎΠ²Ρ‹Π΅ символы Π² Ρ„ΠΎΡ€ΠΌΠ΅ &#8364;. ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΈΠ²Π°Π΅Ρ‚ ΡˆΠ΅ΡΡ‚Π½Π°Π΄Ρ†Π°Ρ‚Π΅Ρ€ΠΈΡ‡Π½Ρ‹ΠΉ Ρ„ΠΎΡ€ΠΌΠ°Ρ‚ Π½Π°ΠΏΠΎΠ΄ΠΎΠ±ΠΈΠ΅ &#xA9;. Π‘Ρ‚Ρ€ΠΎΠΊΠΈ Π² ΠΊΠΎΠ΄ΠΈΡ€ΠΎΠ²ΠΊΠ΅ UTF-8 ΠΏΠ΅Ρ€Π΅Π΄Π°ΡŽΡ‚ΡΡ Π±Π΅Π· прСобразования.

Π˜ΠΌΠ΅Π½ΠΎΠ²Π°Π½Π½Ρ‹Π΅ сущности Π½Π°ΠΏΠΎΠ΄ΠΎΠ±ΠΈΠ΅ &copy; Π½Π΅ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΈΠ²Π°ΡŽΡ‚ΡΡ. Π’Π°ΠΊΠΈΠ΅ ΠΈΠΌΠ΅Π½ΠΎΠ²Π°Π½Π½Ρ‹Π΅ сущности Π΄Π΅ΠΊΠΎΠ΄ΠΈΡ€ΡƒΡŽΡ‚ Π² строку Π² ΠΊΠΎΠ΄ΠΈΡ€ΠΎΠ²ΠΊΠ΅ UTF-8 Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠ΅ΠΉ html_entity_decode().

Ѐункция нарисуСт Π½Π΅Π·Π°ΠΊΡ€Π°ΡˆΠ΅Π½Π½Ρ‹ΠΉ ΠΏΡ€ΡΠΌΠΎΡƒΠ³ΠΎΠ»ΡŒΠ½ΠΈΠΊ вмСсто символа, Ссли ΡˆΡ€ΠΈΡ„Ρ‚ Π½Π΅ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΈΠ²Π°Π΅Ρ‚ символ Π² строкС.

options

Массив с ΠΊΠ»ΡŽΡ‡ΠΎΠΌ linespacing, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ содСрТит Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ float.

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

Ѐункция Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ массив ΠΈΠ· 8 элСмСнтов, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ ΠΏΡ€Π΅Π΄ΡΡ‚Π°Π²Π»ΡΡŽΡ‚ ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Ρ‹ Ρ‡Π΅Ρ‚Ρ‹Ρ€Ρ‘Ρ… Ρ‚ΠΎΡ‡Π΅ΠΊ β€” Π²Π΅Ρ€ΡˆΠΈΠ½ Ρ€Π°ΠΌΠΊΠΈ обрамлСния тСкста. Массив прСдставляСт Ρ‚ΠΎΡ‡ΠΊΠΈ Π² ΡΠ»Π΅Π΄ΡƒΡŽΡ‰Π΅ΠΌ порядкС: Π½ΠΈΠΆΠ½ΠΈΠΉ Π»Π΅Π²Ρ‹ΠΉ, Π½ΠΈΠΆΠ½ΠΈΠΉ ΠΏΡ€Π°Π²Ρ‹ΠΉ, Π²Π΅Ρ€Ρ…Π½ΠΈΠΉ ΠΏΡ€Π°Π²Ρ‹ΠΉ, Π²Π΅Ρ€Ρ…Π½ΠΈΠΉ Π»Π΅Π²Ρ‹ΠΉ ΡƒΠ³ΠΎΠ». Π’ΠΎΡ‡ΠΊΠΈ относятся ΠΊ тСксту нСзависимо ΠΎΡ‚ ΡƒΠ³Π»Π°, поэтому Β«Π²Π΅Ρ€Ρ…Π½ΠΈΠΉ Π»Π΅Π²Ρ‹ΠΉΒ» ΠΎΠ±ΠΎΠ·Π½Π°Ρ‡Π°Π΅Ρ‚ Π²Π΅Ρ€Ρ…Π½ΠΈΠΉ Π»Π΅Π²Ρ‹ΠΉ ΡƒΠ³ΠΎΠ», Ссли ΡΠΌΠΎΡ‚Ρ€Π΅Ρ‚ΡŒ Π½Π° тСкст Π³ΠΎΡ€ΠΈΠ·ΠΎΠ½Ρ‚Π°Π»ΡŒΠ½ΠΎ. Ѐункция Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ false, Ссли Π²ΠΎΠ·Π½ΠΈΠΊΠ»Π° ошибка.

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

ВСрсия ОписаниС
8.0.0 Π”ΠΎΠ±Π°Π²ΠΈΠ»ΠΈ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ options.

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

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ отрисовки тСкста Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠ΅ΠΉ imagettftext()

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ создаст ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ Π² Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π΅ PNG с Π±Π΅Π»Ρ‹ΠΌ Ρ„ΠΎΠ½ΠΎΠΌ Ρ€Π°Π·ΠΌΠ΅Ρ€ΠΎΠΌ 400x30 пиксСлСй ΠΈ надписью "ВСст..." Ρ‡Π΅Ρ€Π½Ρ‹ΠΌ Ρ†Π²Π΅Ρ‚ΠΎΠΌ (с сСрой Ρ‚Π΅Π½ΡŒΡŽ) ΡˆΡ€ΠΈΡ„Ρ‚ΠΎΠΌ Arial.

<?php

// Π’ΠΈΠΏ содСрТимого
header('Content-Type: image/png');

// Π‘ΠΎΠ·Π΄Π°Π½ΠΈΠ΅ изобраТСния
$im = imagecreatetruecolor(400, 30);

// Π‘ΠΎΠ·Π΄Π°Π½ΠΈΠ΅ Ρ†Π²Π΅Ρ‚ΠΎΠ²
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);

// ВСкст надписи
$text = 'ВСст...';

// Π—Π°ΠΌΠ΅Π½Π° ΠΏΡƒΡ‚ΠΈ ΠΊ ΡˆΡ€ΠΈΡ„Ρ‚Ρƒ Π½Π° ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒΡΠΊΠΈΠΉ
$font = 'arial.ttf';

// ВСнь
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);

// ВСкст
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);

imagepng($im);

?>

Π’Ρ‹Π²ΠΎΠ΄ ΠΏΡ€ΠΈΠ²Π΅Π΄Ρ‘Π½Π½ΠΎΠ³ΠΎ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π° Π±ΡƒΠ΄Π΅Ρ‚ ΠΏΠΎΡ…ΠΎΠΆ Π½Π°:

Π’Ρ‹Π²ΠΎΠ΄ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π°: imagettftext()

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

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

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

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

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

up
52
Valentijn de Pagter ΒΆ
18 years ago
If you're looking for easy text alignment, you need to use the imagettfbbox() command. When given the correct parameters, it will return the boundaries of your to-be-made text field in an array, which will allow you to calculate the x and y coordinate that you need to use for centering or aligning your text.

A horizontal centering example:

<?php

$tb = imagettfbbox(17, 0, 'airlock.ttf', 'Hello world!');

?>

$tb would contain:

Array
(
    [0] => 0 // lower left X coordinate
    [1] => -1 // lower left Y coordinate
    [2] => 198 // lower right X coordinate
    [3] => -1 // lower right Y coordinate
    [4] => 198 // upper right X coordinate
    [5] => -20 // upper right Y coordinate
    [6] => 0 // upper left X coordinate
    [7] => -20 // upper left Y coordinate
)

For horizontal alignment, we need to substract the "text box's" width { $tb[2] or $tb[4] } from the image's width and then substract by two.

Saying you have a 200px wide image, you could do something like this:

<?php

$x = ceil((200 - $tb[2]) / 2); // lower left X coordinate for text
imagettftext($im, 17, 0, $x, $y, $tc, 'airlock.ttf', 'Hello world!'); // write text to image

?>

This'll give you perfect horizontal center alignment for your text, give or take 1 pixel. Have fun!
up
12
suyog at suyogdixit dot com ΒΆ
12 years ago
For your general edification: The following drop-in function will place a block of fully justified text onto a GD image. It is a little CPU heavy, so I suggest caching the output rather than doing it on-the-fly. 

Arguments: 

$image - the GD handle of the target canvas 
$size - text size 
$angle - slope of text (does not work very well), leave at 0 for horizontal text 
$left - no. of pixels from left to start block 
$top - no. of pixels from top to start block 
$color - handle for colour (imagecolorallocate result) 
$font - path to .ttf font 
$text - the text to wrap and justify 
$max_width - the width of the text block within which the text should be wrapped and fully justified 
$minspacing - the minimum number of pixels between words 
$linespacing - a multiplier of line height (1 for normal spacing; 1.5 for line-and-a-half etc.)

eg.
$image = ImageCreateFromJPEG( "sample.jpg" );
$cor = imagecolorallocate($image, 0, 0, 0);
$font = 'arial.ttf';
$a = imagettftextjustified($image, 20, 0, 50, 50, $color, $font, "Shree", 500, $minspacing=3,$linespacing=1);
header('Content-type: image/jpeg');
imagejpeg($image,NULL,100);

function imagettftextjustified(&$image, $size, $angle, $left, $top, $color, $font, $text, $max_width, $minspacing=3,$linespacing=1)
{
$wordwidth = array();
$linewidth = array();
$linewordcount = array();
$largest_line_height = 0;
$lineno=0;
$words=explode(" ",$text);
$wln=0;
$linewidth[$lineno]=0;
$linewordcount[$lineno]=0;
foreach ($words as $word)
{
$dimensions = imagettfbbox($size, $angle, $font, $word);
$line_width = $dimensions[2] - $dimensions[0];
$line_height = $dimensions[1] - $dimensions[7];
if ($line_height>$largest_line_height) $largest_line_height=$line_height;
if (($linewidth[$lineno]+$line_width+$minspacing)>$max_width)
{
$lineno++;
$linewidth[$lineno]=0;
$linewordcount[$lineno]=0;
$wln=0;
}
$linewidth[$lineno]+=$line_width+$minspacing;
$wordwidth[$lineno][$wln]=$line_width;
$wordtext[$lineno][$wln]=$word;
$linewordcount[$lineno]++;
$wln++;
}
for ($ln=0;$ln<=$lineno;$ln++)
{
$slack=$max_width-$linewidth[$ln];
if (($linewordcount[$ln]>1)&&($ln!=$lineno)) $spacing=($slack/($linewordcount[$ln]-1));
else $spacing=$minspacing;
$x=0;
for ($w=0;$w<$linewordcount[$ln];$w++)
{
imagettftext($image, $size, $angle, $left + intval($x), $top + $largest_line_height + ($largest_line_height * $ln * $linespacing), $color, $font, $wordtext[$ln][$w]);
$x+=$wordwidth[$ln][$w]+$spacing+$minspacing;
}
}
return true;
}
up
5
gav-alex at bk dot ru ΒΆ
20 years ago
Hi all!
When my hoster updated his php's libs at first minutes i've got the same problem as some of you.
Php couldn't find the path to true type fonts.
The solution in my case was to make the path look like this
<?php
imagettftext($im, 20, 0, 620, 260, $secondary_color, "./tahoma.ttf" , "NEWS");
?>
so as you can see i simply added "./"

another tip that i wanted to add here is how to write in RUssian on image using imagettftext
you simply have to change the function argument like this
<?php
imagettftext($im, 15, 0, 575, 300, $secondary_color, "./tahoma.ttf" , win2uni("some word in russian"));
 ?>
where win2uni is the function that converts win1251 to unicode. here is the code of it
<?php 

  //  Windows 1251 -> Unicode
  function win2uni($s)
  {
    $s = convert_cyr_string($s,'w','i'); //  win1251 -> iso8859-5
    //  iso8859-5 -> unicode:
    for ($result='', $i=0; $i<strlen($s); $i++) {
      $charcode = ord($s[$i]);
      $result .= ($charcode>175)?"&#".(1040+($charcode-176)).";":$s[$i];
    }
    return $result;
  }
?>

That's all today! Thanks for your attention!
Alex
up
2
mitch at electricpulp dot com ΒΆ
18 years ago
If you're having issues with fonts not working... (Could not find/open font) check your permissions on the folder/font files and make sure they're 775, especially if you've just pulled them from a windows box. Hope this helps!
up
2
s.pynenburg _at_ gm ail dotcom ΒΆ
17 years ago
I had an image generator where the user could position where they wanted the text to begin - however it kept going off the side of an image. So I made this basic function: it measures if the inputted text and x-position will cause the string to go off the edge, and if so, it will fit as much as it can on the first line, then go down to the next one.
Limitations:
-It only performs this once (i.e. it won't split into three lines)
-I'm pretty sure it won't work with angled text.

<?PHP

function imagettftextwrap($im, $size, $angle, $x_pos, $y_pos, $color, $font, $instr)
{
    $box = @imagettfbbox($size, 0, $font, $instr);
    $width = abs($box[4] - $box[0]);
    $height = abs($box[3] - $box[5]);
    $overlap = (($x_pos + $width) - imagesx($im));
    if($overlap > 0) //if the text doesn't fit on the image
    {
        $chars = str_split($instr);
        $str = "";
        $pstr = "";
        for($m=0; $m < sizeof($chars); $m++)
        {
            $bo = imagettfbbox($fsize1, 0, $font1, $str);
            $wid = abs($bo[4] - $bo[0]);
            if(($x_pos + $wid) < imagesx($im)) //add one char from the string as long as it's not overflowing
            {
                $pstr .= $chars[$m];
                $bo2 = imagettfbbox($fsize1, 0, $font1, $pstr);
                $wid2 = abs($bo2[4] - $bo2[0]);
                if(($x_pos + $wid2) < imagesx($im))
                {
                    $str .= $chars[$m];
                }    
                else
                {
                    break;
                }
            }
            else
            {
                break;
            }
        }
        $restof = "";
        for($l=$m; $l < sizeof($chars); $l++)
        {
            $restof .= $chars[$l]; //add the rest of the string to a new line
        }
        imagettftext($im, $size, $angle, $x_pos, $y_pos, $color, $font, $str); // print out the smaller line
        imagettftext($im, $size, $angle, 0, $y_pos + $height, $color, $font, $restof); //and the rest of it
    }
    else
    {
        imagettftext($im, $size, $angle, $x_pos, $y_pos, $color, $font, $instr); //otherwise just do normally
    }

}

?>
up
3
pillepop2003 at nospam dot yahoo dot de ΒΆ
21 years ago
Hey guys,

check this function if you want to rotate the text around its center and not its "lower left" pivot-point:

<?php
        // Put center-rotated ttf-text into image
        // Same signature as imagettftext();
        function imagettftext_cr(&$im, $size, $angle, $x, $y, $color, $fontfile, $text)
        {
            // retrieve boundingbox
            $bbox = imagettfbbox($size, $angle, $fontfile, $text);
            
            // calculate deviation
            $dx = ($bbox[2]-$bbox[0])/2.0 - ($bbox[2]-$bbox[4])/2.0;         // deviation left-right
            $dy = ($bbox[3]-$bbox[1])/2.0 + ($bbox[7]-$bbox[1])/2.0;        // deviation top-bottom
            
            // new pivotpoint
            $px = $x-$dx;
            $py = $y-$dy;
            
            return imagettftext($im, $size, $angle, $px, $py, $color, $fontfile, $text);
        }

?>

Big up
Phil
up
3
philip at webdesco dot com ΒΆ
17 years ago
Hi,
for the dummies (like myself) if you are having problems including your font file, prefix the file name with ./

On my development server the following worked fine
$myfont = "coolfont.ttf";

on my hosting server the only way i could get the font to work was as follows
$myfont = "./coolfont.ttf";

hope this helps someone out!