Text & Fonts
Write Text on an Image
2.8M Downloads / Month
Open Source MIT License
2.8M Downloads / Month
Open Source MIT License
Learn how to add text and fonts on images with PHP and Intervention Image. Discover methods for setting font size, font color, alignment, rotation, line height, and text wrapping.
public Image::text(string $text, int $x, int $y, callable|FontInterface $font): ImageInterface
Write a text string at the basepoint position of x, y to the current image. You can define more details like font-size, font-file and alignment via a callback as the fourth parameter.
Name | Type | Description |
---|---|---|
text | string | The text that will be written to the image. |
x | integer | Coordinate on x-axis defining the base point of the first character. |
y | integer | Coordinate on y-axis defining the base point of the first character. |
font | callable or FontInterface | Callback function to configure the font appearance or Typography\Font instance. |
use Intervention\Image\ImageManager;
// create new image instance
$image = ImageManager::gd()->read('images/example.jpg');
// write text at a certain position
$image->text('The quick brown fox', 120, 100);
To define the overall appearance of the text and set more details you can pass a callback as an optional parameter. The callback places the calls on the FontInterface and listens for following methods.
use Intervention\Image\ImageManager;
use Intervention\Image\Typography\FontFactory;
// create test image
$image = ImageManager::imagick()->read('images/example.jpg');
// write text to image
$image->text('The quick brown fox', 120, 100, function (FontFactory $font) {
$font->filename('./fonts/comic-sans.ttf');
$font->size(70);
$font->color('fff');
$font->stroke('ff5500', 2);
$font->align('center');
$font->valign('middle');
$font->lineHeight(1.6);
$font->angle(10);
$font->wrap(250);
});
public FontFactory::size(float $size): FontFactory
Define a font size. By default, a value of 12
will be applied.
Name | Type | Description |
---|---|---|
size | float | Font size to be applied |
public FontFactory::filename(string $filename): FontFactory
Set a path to a font file in the file system for the text.
Name | Type | Description |
---|---|---|
filename | string | Path to a valid font file |
public FontFactory::color(mixed $color): FontFactory
Define the text color in one of the valid color formats.
Name | Type | Description |
---|---|---|
color | mixed | Desired color of text |
public FontFactory::stroke(mixed $color, int $width = 1): FontFactory
Add an outline effect in the desired color to the text to be written. You can also determine the width of the strokes for text characters.
Please note that if the stroke function is used, for technical reasons it is only possible to use fully opaque colors for text and strokes.
Name | Type | Description |
---|---|---|
color | mixed | Color of strokes in valid color format |
width | int | Optional width of the stroke effect in the range from 0 to 10 (default 1 ) |
public FontFactory::align(string $align): FontFactory
Define the horizontal alignment of the text to be written starting from the
base point. Possible values are left, right and center. Default: left
Name | Type | Description |
---|---|---|
align | string | Horizontal text alignment |
public FontFactory::valign(string $valign): FontFactory
Define the vertical alignment of the text to be written starting from the base
point. Possible values are top, bottom and middle. Default: bottom
Name | Type | Description |
---|---|---|
valign | string | Vertical text alignment |
public FontFactory::angle(float $angle): FontFactory
Rotate the text block clockwise with a desired angle.
Name | Type | Description |
---|---|---|
angle | float | Rotation angle |
public FontFactory::lineHeight(float $height): FontFactory
Define the line height of the text block. Applies only to multi-line text.
Default value is 1.25
.
Name | Type | Description |
---|---|---|
height | float | Line height for multi-line text block |
public FontFactory::wrap(int $width): FontFactory
Specifies the maximum width of the text in pixels. The text to be rendered is parsed using the specified font options, and each line is automatically wrapped to the maximum width. Hyphenation is not performed.
Name | Type | Description |
---|---|---|
width | int | Maximum width of the text block |