1

I tried to merge text with images, but my text not rendering correctly after merge.

See my screenshot: render problem

The Red mark ( আমি তোমায় ভালবাসি ) text format is correct while I normally show the variable. Font Link: http://www.fonts2u.com/solaimanlipi.font

But while I merge text with image, its broken or not render correctly.

Here is my full code:

<?php
mb_language('uni');
mb_internal_encoding('UTF-8');
$username="";
if(isset($_POST['user'])){
    $username = $_POST['user'];
    $username = mb_convert_encoding($username, 'HTML-ENTITIES',"UTF-8");
    $username = html_entity_decode($username,ENT_NOQUOTES, "ISO-8859-1");
}

$im = imagecreatefromjpeg('image.jpg');
//The numbers are the RGB values of the color you want to use 
$black = ImageColorAllocate($im, 255, 255, 255); 

//The canvas's (0,0) position is the upper left corner 
//So this is how far down and to the right the text should start 
$start_x = 50; 
$start_y = 80; 

$font = 'SolaimanLipi.ttf';

Imagettftext($im, 50, 0, $start_x, $start_y, $black, $font, "$username"); 

//Creates the jpeg image and sends it to the browser 
//100 is the jpeg quality percentage 

Imagejpeg($im, 'result.jpg', 100); 

echo "<div style='color:red;font-size:60px;'>".$username."</font><br /><img src='result.jpg' height='500' />"
?>

Note 1: SolaimanLipi.ttf is Bangla (Unicode) Font.
Note 2: Bangla (Unicode) font not rendering correctly in tcpdf This Stack & Stack solution will help you to understand my problem & give me a solution.

Please someone help me to get out this problem. Thanks in advance.

[[Previously I asked the same question and no one answer. So I re-post this and deleted the old one. I hope this time someone help me.]]

Community
  • 1
  • 1
I Am Stack
  • 231
  • 3
  • 18
  • I don't see this text in your code, so you will have to do some more work on the required minimal example! Without that, it's impossible to tell what exactly is wrong, your expectations or the output. Also consider that it's possible that your code is correct and that there's simply a bug in the font or the code rendering the font, so check any associated bug tracking systems. – Ulrich Eckhardt Feb 20 '16 at 08:37
  • @UlrichEckhardt I added the font & the text in my question. or here is the text: আমি তোমায় ভালবাসি ( I love you - in english). English font work. Problem when try to write in Bangla. But SolaimanLipi is a bangla font. – I Am Stack Feb 20 '16 at 10:16

1 Answers1

4

Bengali is one of a number of scripts that cannot be rendered simply by putting one character after the next. The shapes of characters change and merge depending on their neighbours.

To render this correctly, you need a set of features known as Complex Text Layout. On text rendering systems that do not support CTL, you'll get the equivalent of each letter being rendered alone, so আমি comes out like আ ম ি.

The GD library used by PHP's imagettftext function does not support Complex Text Layout. You'd have to use some higher-level rendering library such as Pango. This is a bit of a pain; if you can achieve your aims by rendering the text over the image as part of HTML/CSS/SVG that would certainly be easier.

(BTW writing $username out to the HTML without doing htmlspecialchars() is an XSS security hole, and writing-then-serving the JPEG file is unsafe as it will break if two requests come in at the same time. You should also serve your page/form as UTF-8 by adding <meta charset="utf-8"/> so you don't have to do the crazy dance with mb_convert_encoding/html_entity_decode).

bobince
  • 528,062
  • 107
  • 651
  • 834