GD Library imagettftext getting problem in Gujarati Language Text
Asked Answered
B

1

6

I have used GD Library to create "Text on Image". I am facing one issue that passing some Gujarati text but getting wrong output as below:

I want like this

enter image description here

and getting:

enter image description here

My code is:

        $textBox = imagettfbbox($fontSize, $angle, $font, $txt);

        $textWidth = abs(max($textBox[2], $textBox[5]));

        $textHeight = abs(max($textBox[5], $textBox[7]));

        $x = (imagesx($this->img) - $textWidth)/2;

        $y = ((imagesy($this->img) + $textHeight)/$h)-($lines-2)*$textHeight;

        $lines = $lines-1;

        // Added this line from SO answer.
        $txt = mb_convert_encoding($txt, "HTML-ENTITIES", "UTF-8");
        $txt = preg_replace('~^(&([a-zA-Z0-9]);)~', htmlentities('${1}'), $txt);

        //add some shadow to the text
        imagettftext($this->img, $fontSize, $angle, $x + 2, $y + 1, $white, $font, $txt);

        //add the text
        imagettftext($this->img, $fontSize, $angle, $x, $y, $maroon, $font, $txt);

I have already tried this answer in above code but didn't worked.

Can anyone help me please?

Brush answered 27/1, 2019 at 7:17 Comment(4)
Kindly provide the font name and also input text to be more specificSeverable
I have used Gujarati-Saral-1.ttf font also I have uploaded index.phpBrush
Gujarati Saral does not have ત્ર included in fontsStockholder
I have tried many other fonts from lipikaar.com/support/download-unicode-fonts-for-gujaratiBrush
M
2

I have created sample example for your given text, I have used other font. You need to match the character mapping to display the correct the text:

<?php
header('Content-Type: image/png');

// Create the image
$im = imagecreatetruecolor(400, 30);

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);

// The text to draw
$text = 'xF]EZF+L';
// Replace path by your own font path
$font = __DIR__ . '/LMG-Arun.ttf';

// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);

// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);

You can add your font and style. For the character mapping follow the url: https://fonts.webtoolhub.com/font-n48283-lmg-arun.aspx

Output:

enter image description here

Metts answered 5/2, 2019 at 6:24 Comment(4)
How did you get this 'xF]EZF+L'?Brush
Use following link: fonts.webtoolhub.com/font-n48283-lmg-arun.aspx and click on "View Character Set" You will get exact character mappingMetts
I got output, but problem is I want to do it dynamically where I can put "String" in gujarati from Google Translate. Its very lengthy process to find each character and put in php file. Please let me know if you get any other solutions. Thanks for your efforts.Brush
@PratikButani you could map the user input (Gujarati) to -> character mapping of the fonts and you can utilize above answerStockholder

© 2022 - 2024 — McMap. All rights reserved.