3

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

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

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

印地语文本

这是代码:

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

任何帮助将不胜感激。

4

3 回答 3

3

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

 $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);
        }
于 2017-02-01T10:04:50.463 回答
0

请检查我的项目,以使用 Krutidev 字体在 Unity3d 中进行天城体字体渲染。它在我们的一个内部项目中为我工作。示例项目位于此链接。这个想法是将 unicode 转换为 Krutidev 并使用 krutidev 字体。

于 2018-03-15T18:27:06.117 回答
-2

我不太精通印地语,但我认为您使用的 GD 库没有使用有关印地语语言的规则,这些规则改变了字形显示的顺序。我不知道这些规则是什么,但猜想它们是在字体的 GSUB 或 GPOS 表中编码的。GSUB 控制字形序列的显示方式,而 GPOS 控制字形的间距。我假设 GD 库不处理 GPOS 或 GS​​UB 表,这就是为什么你的字符串在 imagettfext 中的顺序是字符串中字形的顺序。您可能需要在脚本中实现一些字符串处理代码,这些代码接受字符串,并且知道 GD 库将按照它们在字符串中的顺序显示它们,从而正确地对字符进行排序。

请使用以下代码获取以下印地语规则

string changeHindiGlyph(string originalText)
{
    string[] words = originalText.Split(new char[]{' '});

    for(int k = 0; k < words.Length; k++)
    {
        if(words[k].Contains("\u093f")) // check if the word contains "i" vowel
        {
            char[] arr = words[k].ToCharArray();
            for (int i = 0; i < arr.Length -1 ; i++)
            {
                //interchange the order of "i" vowel
                if(arr[i] == '\u093f')
                {
                    arr[i] = arr[i-1];
                    arr[i-1] = '\u093f';
                }
            }

            words[k] = new string(arr);
        }
    }

    originalText = string.Join(" ", words);

    return originalText;
}

我在上面的代码中使用了 C#,您可以将其转换为您的脚本语言。同样,您可以在代码中包含更多规则,以便在 GD 库中获得所需的文本。希望这能解决您的问题。

于 2014-12-16T10:11:32.730 回答