Skip to content

Loading TTF fonts at runtime #254

Closed
Closed
@olivervogel

Description

@olivervogel

I have problems rendering text with a TTF font onto an image.

As far as I can see from this comment, with python it is possible to load fonts from files at runtime using the fontfile option. However, when I try to transfer the code to PHP this does not seem to work, the text does not appear in the correct font.

I have already tried the following.

  • Absolute specification of the TTF path
  • Changing the font name in the font option
  • Using the LoversQuarrel-Regular.ttf TTF from the comment

Does anyone have an idea what could be wrong here?

Code example

use Jcupitt\Vips\BandFormat;
use Jcupitt\Vips\Extend;
use Jcupitt\Vips\Image;
use Jcupitt\Vips\Interpretation;

$canvas = Image::black(1, 1)
    ->add(60)
    ->cast(BandFormat::UCHAR)
    ->embed(0, 0, 500, 500, ['extend' => Extend::COPY]) // 500x500 px canvas
    ->copy(['interpretation' => Interpretation::SRGB])
    ->bandjoin([
        60, // green
        60, // blue
        255, // alpha
    ]);

$text = Image::text('Test!', [
    'fontfile' => './Impact.ttf',
    'font' => 'Impact 16',
    'dpi' => 300,
]);

$textImage = $text->newFromImage([255, 120, 0])
    ->copy(['interpretation' => Interpretation::SRGB])
    ->bandjoin($text);

$modified = $canvas->composite($textImage, 'over', [
    'x' => 100,
    'y' => 100,
]);

$encoded = $modified->writeToBuffer('.png');

Result image

vips

Environment

PHP 8.4
libvips 8.16.0
jcupitt/vips 2.4.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions