image icon indicating copy to clipboard operation
image copied to clipboard

getBoxSize() from \Intervention\Image\Gd\Font modifies UTF-8 Text Input

Open GregorVoelkl opened this issue 4 years ago • 1 comments

The getBoxSize() function of \Intervention\Image\Gd\Font has the following behaviour which seems to be unwanted.

Problem:

    $text = 'Jörg';

    $font = new \Intervention\Image\Gd\Font($text);
    $font->file(public_path('fonts/montserrat-v14-latin-700.ttf'));
    $font->valign('top');
    $box = $font->getBoxSize();

    dump($font->getText());  // internal text is: Jörg

    // getBoxSize() is called in applyToImage()
    // Jörg is written on the image
    $font->applyToImage($this->stage, 200, 200);

    dump($font->getText()); // internal text is: Jörg

Inside getBoxSize()

        $this->text = mb_encode_numericentity($this->text, array(0x0080, 0xffff, 0, 0xffff), 'UTF-8');

is called, which modifies Codepoints above 0x0080 to decimal unicode representation.

My suggestion would be either to document this behaviour or to restore the original text input.

GregorVoelkl avatar Dec 02 '21 13:12 GregorVoelkl

Just found out doing this:

$text = "哈哈哈";
$font = new \Intervention\Image\Gd\Font($text);
// ...
$box = $font->getBoxSize();
$font->text($text); <--- tihs reset
// ...

solves my problem.

0xxb avatar Jan 18 '22 03:01 0xxb

This issue should be resolved as of version 3.

olivervogel avatar Dec 27 '23 12:12 olivervogel