Download the PHP package teknoo/gd-text without Composer
On this page you can find all versions of the php package teknoo/gd-text. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package gd-text
Teknoo Software - GD-text library
Fork from GdText library to add texts, with some effects and alignments in
images thanks to the GdExtension.
Basic usage example
Example output:
Multilined text
Text stroke
Text background
Fit the text to the box
drawFitFontSize() looks for the biggest font size (by steps of $precision) for which the text fits in the box,
then draws it. Without $maxFontSize, the font size grows until the text fills the box. Without $minFontSize,
the font size can go down to 1. If the text never fits, it is drawn with the minimum font size and overflows.
The font size configured with setFontSize() is the starting point and is restored after drawing.
API overview
All setters of GDText\Box return the box itself and can be chained.
| Method | Description |
|---|---|
setFontFace(string $path) |
Path to the TTF/OTF font file. Required before drawing. |
setFontSize(int $size) |
Font size in pixels (>= 1), default 12. |
setFontColor(Color $color) |
Text color, default black. |
setBox(int $x, int $y, int $width, int $height) |
Area where the text is drawn, default 0, 0, 100, 100. |
setTextAlign(HorizontalAlignment $x, VerticalAlignment $y) |
Alignment of the text in the box, default Left, Top. |
setTextWrapping(TextWrapping $wrapping) |
WrapWithOverflow (default) wraps words on the box width, a word longer than the box overflows. NoWrap draws the text as is. |
setLineHeight(float $ratio) |
Line height as a ratio of the font size (> 0), default 1.25. |
setBaseline(float $ratio) |
Position of the baseline in the line, as a ratio of the line height, default 0.2. |
setAngle(int $degrees) |
Rotation of each line, counter clockwise. The alignment is computed on the unrotated text. |
setStrokeColor(Color $color), setStrokeSize(int $pixels) |
Outline of the text, default none (0). |
setTextShadow(Color $color, int $xShift, int $yShift) |
Shadow of the text, default none. |
setBackgroundColor(Color $color) |
Background behind each non empty line, default none. |
enableDebug() |
Fills the box and each line with random colors to visualize them. |
draw(string $text): Rectangle |
Draws the text and returns the area covered by it. |
drawFitFontSize(...) |
Draws the text with the biggest font size fitting the box, see above. |
calculate(string $text): Rectangle |
Returns the area that draw() would cover, without drawing anything. |
GDText\Color represents a 8-bit RGB color with an optional alpha channel (0 opaque to 127 transparent):
new Color(255, 75, 140), new Color(0, 0, 0, 50), Color::parseString('#ff4b8c'), Color::parseString('#abc'),
Color::fromHsl(0.5, 0.8, 0.3) (each component between 0 and 1). Invalid values raise an
InvalidArgumentException. On palette images whose palette is full, the closest color is used.
Security considerations
setFontFace()passes the path as is to the GD extension. Depending on how GD was built, a name that is not a file path can be resolved through fontconfig. Never pass a user controlled value to this method.- The drawing cost grows with the text length, the font size and the stroke size: a stroke of
npixels draws each line(2n + 1)²times.drawFitFontSize()measures the text once per step between the minimum and the maximum font size. Bound these values when they come from user input.
Support this project
This project is free and will remain free. It is fully supported by commercial activities of SASU Teknoo Software and EIRL Richard DELOGE. If you like it and help me maintain it and evolve it, don't hesitate to support me on Patreon or Github.
Thanks :) Richard.
Credits
EIRL Richard Déloge - https://deloge.io - Lead developer. SASU Teknoo Software - https://teknoo.software
About Teknoo Software
Teknoo Software is a PHP software editor, founded by Richard Déloge, as part of EIRL Richard Déloge. Teknoo Software's goals : Provide to our partners and to the community a set of high quality services or software, sharing knowledge and skills.
License
GdText is licensed under the 3-Clause BSD License - see the LICENSE file for details.
Installation & Requirements
To install this library with composer, run this command :
composer require teknoo/gd-text
This library requires :
* PHP 8.5+
* GD extension with Native TTF support
Demos
Line height demo:
Text alignment demo:
Text stroke demo:
Text background demo:
Debug mode enabled demo:
Contribute :)
You are welcome to contribute to this project. Fork it on Github
All versions of gd-text with dependencies
ext-gd Version *