Search code examples
phplaravelintervention

Can we use multiple fonts on the basis of different language to create overlay text using intervention library?


I'm using Intervention library to write overlay text on an image. This works perfect if the text provided is of one language(say English)

I need to write overlay text in multiple languages depending on the input given(the input can be in any language).

Here is my code base for generating the above.

public static function generateTextOverImage($image, $text)
{
    $img = Image::make($image->getContent());
    $extension = 'jpg';
    $centerX = 25;
    $centerY = 210;
    $text = str_replace('<br>', "\n", html_entity_decode($message)) ;
    $lines = explode("\n", wordwrap($text, 21));
    $currentLineVIndex = $centerY;

    foreach ($lines as $line) {
        $img->text($line, $centerX, $currentLineVIndex, function ($font) use ($fontSize) {
            $font->file(public_path('fonts/LucidaGrande.ttf'));
            $font->size(18);
            $font->color('#fdf6e3');
        });

    }
    $encoded = $img->encode($extension);
    return $encoded;
}

As the text can be in different languages(One or more language at a time). The expected text over image in broken. It works fine for english language only.

Any help will be highly appreciated. Thanks

Update

is there any font which support multiple languages?


Solution

  • I have solved it at my own. Posting my solution here, may be it will help someone having similar problem.

    The Solution was, using a unicode font (Arial Unicode MS) which supports different fonts.

    Following is the solution

    public static function generateTextOverImage($image, $text)
    {
        $img = Image::make($image->getContent());
        $extension = 'jpg';
        $centerX = 25;
        $centerY = 210;
        $text = str_replace('<br>', "\n", html_entity_decode($message)) ;
        $lines = explode("\n", wordwrap($text, 21));
        $currentLineVIndex = $centerY;
    
        foreach ($lines as $line) {
            $img->text($line, $centerX, $currentLineVIndex, function ($font) use ($fontSize) {
                $font->file(public_path('fonts/arial-unicode-ms/arial-uni.ttf'));
                $font->size(18);
                $font->color('#fdf6e3');
            });
    
        }
        $encoded = $img->encode($extension);
        return $encoded;
    }