Skip to content

Novis\Image\Image::withText

$image->withText(string $text, Font $font, {…} $options): Image$image->withText(
    string $text,
    Font $font,
    {…} $options,
): Image

Draws a text on the image with a font that your program gives it.

withText takes the text, a Font and options. size is the font size in pixels, and color is the colour of the text. Both are needed. x and y place the top-left corner of the text. Without them, gravity places the text, and the default is Image::Gravity::Center.

A line feed starts a new line. With maxWidth, lines also break at spaces. align sets how the lines line up, and the default is Image::Align::Left. Image::measureText returns the size of the text.

There are no system fonts. Make a Font with Font::fromBytes. Text in any script is drawn as the font defines it. Arabic letters join, and a line of Arabic or Hebrew runs from right to left. The font must contain the letters of the text. Text with nothing to draw throws a LogicError, and a font file with broken tables throws a ParseError. Both happen when encode or raw runs.

The examples below draw a label on a badge, and place a letter at x and y. The last one writes a caption at the bottom of a photo.

Parameters

  • $textstring
  • $fontFont
  • $options{ size: float, color: Color, gravity?: Gravity, x?: int, y?: int, maxWidth?: uint, align?: Align }

Return value

Returns Image.

Changelog

VersionChange
0.0.6Each Novis\Image\Image method that returns a new image is named for it, for example resized(), blurred() and withText(). keepingMetadata() replaces metadata({ keep: true }), and encodeVariants() replaces variants(). Image::compare returns a Comparison with the property differenceImage, which was Diff with diff.

Examples

Draw a badge

<?nvs
// Shows how to draw a short label on a coloured badge and save it as a PNG file.
use Novis\Image\Color;
use Novis\Image\Format;
use Novis\Image\Image;
require 'fonts/box-font.nvs';
// Without `x` and `y`, the text is in the center of the image.
tainted bytes $png = Image::create(40, 20, Color::hex('#2e7d32'))
->withText('Sale', BoxFont::load(), { size: 10.0, color: Color::hex('#ffffff') })
->withFormat(Format::Png)
->encode();
// This prints 40 and 20.
var $info = Image::info($png);
echo $info->width, "\n", $info->height, "\n";
Output
40
20

Where the text goes

<?nvs
// Shows how `x` and `y` place the top-left corner of the line of text.
use Novis\Image\Color;
use Novis\Image\Image;
require 'fonts/box-font.nvs';
// Draws "S" in black at `x` 5 and `y` 2 on a white image.
var $raw = Image::create(20, 20, Color::hex('#ffffff'))
->withText('S', BoxFont::load(), { size: 10.0, color: Color::hex('#000000'), x: 5, y: 2 })
->raw();
// Finds the first black pixel. Each pixel is 4 bytes, red first.
int $at = 0;
while (Core\Encoding::toHex(Core\Bytes::slice($raw->pixels, $at * 4, 1)) == 'ff') {
$at = $at + 1;
}
// The letter is a little lower than `y`, because its top is below the top of the line.
// This prints 5 and 3.
echo $at % 20, "\n", Core\Math::quotient($at, 20), "\n";
Output
5
3

Caption on a photo

<?nvs
// Shows how to write a caption at the bottom of a photo, in lines that fit its width.
use Novis\Image\Color;
use Novis\Image\Image;
require 'fonts/box-font.nvs';
tainted bytes $photo = Image::create(80, 60, Color::hex('#336699'))->encode();
// `maxWidth` breaks the caption into lines at spaces.
// `Image::Gravity::South` puts the text at the bottom, in the middle.
tainted bytes $card = Image::open($photo)
->withText('New shoes in the shop', BoxFont::load(), {
size: 10.0,
color: Color::hex('#ffffff'),
gravity: Image::Gravity::South,
maxWidth: 70,
})
->encode();
// The caption changes pixels, and the size of the photo stays the same. This prints 80 and true.
echo Image::info($card)->width, "\n";
echo Image::compare($photo, $card)->differingPixels > 0 ? 'true' : 'false', "\n";
Output
80
true