2

Want to display the half letters like "અર્થ" and "અસત્ય" in imagefttext function PHP but the result something different. Like this.

Link of image that not properly displaying half words of gujarati

Or anyone can help me the alternate way?

 <?php
 header('Content-type: text/html; charset=utf-8');

#Declare fixed values
$textY = 190;
$textX = 130;   
$textY2 = 280;
$textX2 = 130;          
$textFont = './fonts/notoSans/NotoSansGujarati-Bold.ttf';
// $textFont = './fonts/Shrikhand-Regular.ttf';
$textSize = 54;
$imagesFolder = './images';

        $text = "અર્થ"; 
        $text2 = "અસત્ય";

        //header("Content-Type: image/png");
        header("Content-Type: image/png");

        //Creating background image
        $im = imagecreatefrompng('https://i.stack.imgur.com/A9Oll.png');
        //$im = imagecreatetruecolor(512,512);

        // Create the clours to be used.
         $yellow = imagecolorallocate( $im, 255, 255, 0 );

        imagefttext( $im, $textSize, 0, $textX, $textY, $yellow, $textFont, $text );
        imagefttext( $im, $textSize, 0, $textX2, $textY2, $yellow, $textFont, $text2 );

        $imageFile = $imagesFolder.'/'.rand(0,500).'.png';

        imagepng( $im, $imageFile );
        // Unload resources.
        imagedestroy( $im );

?>

=> Edits 0.1: Issue is getting displaying the half letters in the image. - Output I needed (I did that in photoshop) : desired Output image - But I'm getting through this code : image getting from code

Khilan S
  • 21
  • 4
  • Possible duplicate of [Working with GD ( imagettftext() ) and UTF-8 characters](https://stackoverflow.com/questions/9458317/working-with-gd-imagettftext-and-utf-8-characters) – miken32 Nov 17 '18 at 02:48
  • @miken32 I tried that solution on first place! But not working for me. – Khilan S Nov 17 '18 at 02:51
  • I'm a little unclear on the problem here. Are those characters different from what you're expecting? It's very hard to tell for those of us not familiar with this script. Do you have a sample of what those characters should look like, in that font? – miken32 Nov 17 '18 at 03:19
  • @miken32 Issue is getting displaying the half letters in the image. - Output I needed (I did that in photoshop) : [Link of desired Output image](https://i.stack.imgur.com/mO0hI.png) - But I'm getting through this code : [Link of image getting from code](https://i.stack.imgur.com/9RkwA.png) – Khilan S Nov 17 '18 at 04:54
  • I'm not very familiar with multibyte characters, but it seems like some of those characters like ર્થ are combination of characters? When I was using my arrow key to move through, it took two presses to pass this character. If I put a space in the middle, it separates into two characters, the same as show in the image: ર્ થ – miken32 Nov 17 '18 at 05:40
  • So I'm guessing maybe `imageffttext()` can't handle these combined characters. – miken32 Nov 17 '18 at 05:43
  • Tried with Imagemagick extension as well, same result. – miken32 Nov 17 '18 at 06:12
  • @KhilanS Did you get answer? – Pratik Butani Jan 27 '19 at 15:26

1 Answers1

0

Looks like this is not possible with Unicode, and has been a problem for a long time. From a PHP developer on a 10 year old bug:

Thanks, now I got it. The problem isn't particularly related to Bengali, but rather to combining characters in general, which are not supported by imagettftext().

I attempted this with the Imagick extension, but had the same output.

A possible workaround is to find a font that uses another encoding such as ISCII, and then convert the text to that encoding using mb_convert_encoding() or similar function.

Or perhaps have the image generated by a command-line tool run with exec().

miken32
  • 42,008
  • 16
  • 111
  • 154
  • I think, PHP didn't have answer do you think, it would be same with JS too ? – Khilan S Nov 17 '18 at 06:34
  • I expect it would work with a canvas. Browsers are more modern and would have full Unicode support – miken32 Nov 17 '18 at 07:07
  • Do you have any reference in JS? I need about multiple images to convert into text to image and need to save it on folder. – Khilan S Nov 17 '18 at 07:46