unicode文本的问题。在php中创建unicode文本到图像

时间:2011-07-22 08:21:35

标签: php image unicode

这是我的代码。我在转换图像时使用的是mangala.ttf。

我正面临这样的问题

这是输入:वीडियो

我得到这样的输出图像:

<?php
function wrap2($fontSize, $angle, $fontFace, $string, $width)
{    
    $ret = "";
    $arr = explode(' ', $string);
    foreach ( $arr as $word )
    {   $teststring = $ret.' '.$word;
        $testbox = imagettfbbox($fontSize, $angle, $fontFace, $teststring);
        if ( $testbox[2] > $width )
        {
            $ret.=($ret==""?"":"\n").$word;
        } 
        else 
        {
            $ret.=($ret==""?"":' ').$word;
        }
    }
    return $ret;
}

$textval = 'मंदिर  वीडियो';
// buffer output in case there are errors
ob_start();
$textcolor = '6E6E6E';
$font="mangala.ttf";    

$size = 20;
$padding= 2;
$bgcolor= "ffffff";

$transparent = 0;
$antialias = 0;

$fontfile = $fontpath.$font;
$textval = wrap2($size, $angle, $font, $textval, $width = 250);
$box= imageftbbox( $size, 0, $fontfile, $textval, array());
$boxwidth= $box[4];
$boxheight= abs($box[3]) + abs($box[5]);
$width= $boxwidth + ($padding*2) + 1;
$height= $boxheight + ($padding) + 0;
$textx= $padding;
$texty= ($boxheight - abs($box[3])) + $padding;

// create the image
$png= imagecreate($width, $height);


$color = str_replace("#","",$bgcolor);
$red = hexdec(substr($bgcolor,0,2));
$green = hexdec(substr($bgcolor,2,2));
$blue = hexdec(substr($bgcolor,4,2));
$bg = imagecolorallocate($png, $red, $green, $blue);

$color = str_replace("#","",$textcolor);
$red = hexdec(substr($textcolor,0,2));
$green = hexdec(substr($textcolor,2,2));
$blue = hexdec(substr($textcolor,4,2));
$tx = imagecolorallocate($png, $red, $green, $blue);



imagettftext( $png, $size, 0, $textx, $texty, $tx, $fontfile, $textval );

header("content-type: image/jpeg");
imagejpeg($png);
imagedestroy($png);
exit;
?>

0 个答案:

没有答案