问题描述
我想使用PHP5在图像上写北印度语文本(例如पुलिसवालाआमिरखान").
I want to write a Hindi text(say "पुलिसवाला आमिर खान" ) on an image using PHP5.
我正在使用
$im = @imagecreate(210, 50);
$background_color = imagecolorallocate($im, 255, 255, 255);
$text_color = imagecolorallocate($im, 0, 0, 0);
imagestring($im, 7, 15, 15, utf8_encode("पुलिसवाला आमिर खान..."), $text_color);
ImagePNG($im, 'a.png');
imagedestroy($im);
推荐答案
只要主机支持GD2和FreeType(大多数服务器都支持),就可以使用imagettftext()
将文本写到图像上.您可以在此处找到其详细的语法和注释:
You can use imagettftext()
to write text onto images as long as your host supports GD2 and FreeType (as most servers do). You can find its detailed syntax and comments here:
http://php.net/manual/en/function.imagettftext.php
找到支持印地语字符的字体(* .ttf或* .otf).将字体文件放在与脚本相同的目录中,然后尝试以下代码-将"yourhindifont.ttf"替换为您自己的字体文件名:
Find a font (*.ttf or *.otf) that supports Hindi characters. Put the font file in the same directory as your script, and then try this code -- substituting "yourhindifont.ttf" for your own font filename:
<?php
// your string, preferably read from a source elsewhere
$utf8str = "पुलिसवाला आमिर खान";
// buffer output in case there are errors
ob_start();
// create blank image
$im = imagecreatetruecolor(400,40);
$white = imagecolorallocate($im,255,255,255);
imagefilledrectangle($im,0,0,imagesx($im),imagesy($im),$white);
// write the text to image
$font = "yourhindifont.ttf";
$color = imagecolorallocatealpha($im, 50, 50, 50, 0); // dark gray
$size = 20;
$angle = 0;
$x = 5;
$y = imagesy($im) - 5;
imagettftext($im, $size, $angle, $x, $y , $color, $font, $utf8str);
// display the image, if no errors
$err = ob_get_clean();
if( !$err ) {
header("Content-type: image/png");
imagepng($im);
} else {
header("Content-type: text/html;charset=utf-8");
echo $err;
}
?>
如果打算将印地语直接输入到源中,请确保事先在编辑器中将源的文档编码设置为UTF-8(无BOM).否则,该字符串将不会按预期存储.
If you plan to input Hindi directly into your source, be sure to set the document encoding of your source to UTF-8 (without BOM) in your editor beforehand. Otherwise the string will not be stored as expected.
这篇关于PHP5-我想在图像上写印地语文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!