用GD Library编写印地语字体无法按要求呈现 [英] Writing Hindi Fonts with GD Library do not render as desired

查看:110
本文介绍了用GD Library编写印地语字体无法按要求呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想在图像上写以下文字:

If I want to write the following text on the image:



दीक्षा शिक्षा क्या क्या हो गया!

然后,它没有给出预期的结果,而是在图片上打印出了如下文本.我已经尝试了几乎所有可用于印地语的devanagari ttf和unicode字体.

Then it does not not give the expected result but instead is printing out text on the picture as below. I have tried almost all the devanagari ttf and unicode fonts available for Hindi.

这是代码:

$quote="दीक्षा शिक्षा क्या क्या हो गया!";
imagettftext($new_pic, $fontsize, 0, 170, 155-$hidd/2, $color, $font, $quote);

任何帮助将不胜感激.

推荐答案

我遇到了同样的问题,并提出了使用mangal.ttf的PHP解决方案. 字体.使用此代码,您的印地语文本将正确显示在图像文件上.

I had the same problem and came up with a solution in PHP using the mangal.ttf font. using this code your Hindi text will display correctly on your image file.

 $text = "की एक विधा" ; 
 $words = explode(" ", $text);        
for($k = 0; $k < count($words); $k++){

    // detect if the string was passed in as unicode
    $text_encoding = mb_detect_encoding($words[$k], 'UTF-8, ISO-8859-1');

    // make sure it's in unicode
    if ($text_encoding != 'UTF-8') {
        $words[$k] = mb_convert_encoding($words[$k], 'UTF-8', $text_encoding);
        }
 // html numerically-escape everything (&#[dec];)
    $words[$k] = mb_encode_numericentity($words[$k], array (0x0, 0xffff, 0, 0xffff), 'UTF-8');

    $arr = explode("&#", $words[$k]);
for ($i = 0; $i < (count($arr)-1); $i++){

        // interchange the order of "i" vowel
          if($arr[$i] == "2367;") {
            $arr[$i] = $arr[$i-1] . '';
            $arr[$i-1] = "2367;";
            }

        // letter "I" + Nukta forms letter vocalic "L"
          if($arr[$i] == "2311;") {
            if($arr[$i+1] == "2364;") {
                $arr[$i] = "2316;";
                $arr[$i+1] = '';
                }
            }

        // vowel sign vocalic "R" + sign Nukta forms vowel sign vocalic "Rr"
          if($arr[$i] == "2371;") {
              if($arr[$i+1] == "2364;") {
                $arr[$i] = "2372;";
                $arr[$i+1] = '';
                }
            }

        // Candrabindu + sign Nukta forms Om
          if($arr[$i] == "2305;") {
              if($arr[$i+1] == "2364;") {
                $arr[$i] = "2384;";
                $arr[$i+1] = '';
                }
            }

        // letter vocalic "R" + sign Nukta forms letter vocalic "Rr"
          if($arr[$i] == "2315;") {
              if($arr[$i+1] == "2364;") {
                $arr[$i] = "2400;";
                $arr[$i+1] = '';
                }
            }

        // letter "Ii" + sign Nukta forms letter vocalic "LI"
          if($arr[$i] == "2312;") {
              if($arr[$i+1] == "2364;") {
                $arr[$i] = "2401;";
                $arr[$i+1] = '';
                }
            }

        // vowel sign "I" + sign Nukta forms vowel sign vocalic "L"
          if($arr[$i] == "2367;") {
              if($arr[$i+1] == "2364;") {
                $arr[$i] = "2402;";
                $arr[$i+1] = '';
                }
            }

        // vowel sign "Ii" + sign Nukta forms vowel sign vocalic "LI"
          if($arr[$i] == "2368;") {
              if($arr[$i+1] == "2364;") {
                $arr[$i] = "2403;";
                $arr[$i+1] = '';
                }
            }

        // Danda + sign Nukta forms sign Avagraha
          if($arr[$i] == "2404;") {
              if($arr[$i+1] == "2364;") {
                $arr[$i] = "2365;";
                $arr[$i+1] = '';
                }
            }

        // consonant + Halant + Halant + consonant forms consonant + Halant + ZWNJ + consonant
          if($arr[$i] == "2381;") {
              if($arr[$i+1] == "2381;") {
              //$arr[$i+1] = '8204;';
                }
            }

        // consonant + Halant + Nukta + consonant forms consonant + Halant + ZWJ + Consonant
          if($arr[$i] == "2364;") {
              if($arr[$i+1] == "2381;") {
              //$arr[$i] = "2381;";
              //$arr[$i+1] = '8205;';
                }
            }

        }

    $words[$k] = implode('&#',$arr);
    }
$text = implode(" ", $words);

    $img_name = date('dmyhms');
    $image = $img_name.'.png';
    if(file_exists($imagefile)){    

            /*** create image ***/
             $im = @imagecreatefrompng($imagefile);
            /*** create the text color ***/
            $text_color = imagecolorallocate($im, 40, 50, 99);
           /***  set the font file ***/
            $font_file = 'mangal.ttf';
            // Convert HTML entities into ISO-8859-1
           // $text = html_entity_decode($text,ENT_QUOTES, "UTF-8");
            /*** splatter the image with text ***/
             imagefttext($im, 14,0,450, 390, $text_color, $font_file, $text);
            // Save the picture
             imagepng($im,$image);
            }else{
            /*** if the file does not exist we will create our own image ***/
            /*** Create a black image ***/
            $im  = imagecreatetruecolor(150, 30); /* Create a black image */
            /*** the background color ***/
            $bgc = imagecolorallocate($im, 255, 255, 255);
            /*** the text color ***/
            $tc  = imagecolorallocate($im, 0, 0, 0);
            /*** a little rectangle ***/
            imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
            /*** output and error message ***/
            imagestring($im, 1, 5, 5, "Error loading $imagefile", $tc);
        }

这篇关于用GD Library编写印地语字体无法按要求呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆