Intervention Font, Text align center, wraps around left edge

110 Views Asked by At

I am trying to generate text on an image. To be able to position the image properly in backend, I need to generate an image of the text, then place the text-image on the real image. (Laravel 10)

When i use $font->align('left') it works as expected, however when trying to ``$font->align('center')``` only half of the text is visible (text is centered around the left edge of text-image.

How do I get the text within the text-image to be align centered?

use Image;
use Intervention\Image\Gd\Font;
use Intervention\Image\ImageManager;
   $font = new Font("12345\n12345");
            $font->valign('top');
            $font->align('center');
            $font->file('/var/www/html/storage/fonts/' . $text['fontFamily'] . '.ttf');
            $font->size($text['fontSize']);
            $font->color($text['color']);
            $size = $font->getBoxSize();

            $gdmanager = new ImageManager(array('driver' => 'gd'));
            $image_text = $gdmanager->canvas( $size['width'], $size['height']);
            $font->applyToImage($image_text);

            $fontImage = $gdmanager->make($image_text->encode('data-url'));

            $img->insert(
                $fontImage,
                $text['position'] ?? "center",
                $text['x'] ?? 0,
                $text['y'] ?? 0
            );

Example, align = left (as expected) enter image description here

Example, align = center (only half of string) enter image description here

0

There are 0 best solutions below