The improvement of the function of outputting Chinese characters in GD

Source: Internet
Author: User
Tags strlen utf 8
function | Chinese thanks sadly wrote the function of outputting Chinese characters in GD,
I found in the use of this version of the output string must be pure Chinese, can not be mixed in English.
With the modification of this bug, share with you ...

?
Program writen by sadly www.phpx.com
Modified by Agun 2000/6/20
function Gb2utf8 ($GB)
{
if (!trim ($GB))
return $GB;
$filename = "Gb2312.txt";
$tmp =file ($filename);
$codetable =array ();
while (list ($key, $value) =each ($tmp))
$codetable [Hexdec (substr ($value, 0,6))]=substr ($value, 7,6);

$ret = "";
$utf 8 = "";
while ($GB)
{
if (Ord (substr ($GB, 0,1)) >127)
{
$this =substr ($GB, 0,2);
$GB =substr ($GB, 2,strlen ($GB));
$utf 8=u2utf8 (Hexdec ($codetable [Hexdec (Bin2Hex ($this)) -0x8080]);
for ($i =0; $i <strlen ($utf 8); $i +=3)
$ret. =CHR (substr ($utf 8, $i, 3));
}
Else
{
$ret. =substr ($GB, 0, 1);
$GB =substr ($GB, 1,strlen ($GB));
}
}
return $ret;
}

function U2utf8 ($c)
{
for ($i =0; $i <count ($c); $i + +)
$str = "";
if ($c < 0x80) {
$str. = $c;
}
else if ($c < 0x800) {
$str. = (0xc0 | $c >>6);
$str. = (0x80 | $c & 0x3F);
}
else if ($c < 0x10000) {
$str. = (0xe0 | $c >>12);
$str. = (0x80 | $c >>6 & 0x3F);
$str. = (0x80 | $c & 0x3F);
}
else if ($c < 0x200000) {
$str. = (0xF0 | $c >>18);
$str. = (0x80 | $c >>12 & 0x3F);
$str. = (0x80 | $c >>6 & 0x3F);
$str. = (0x80 | $c & 0x3F);
}
return $str;
}

Header ("Content-type:image/gif");
$im = Imagecreate (300,150);
$bkg = Imagecolorallocate ($im, 0,0,0);
$CLR = Imagecolorallocate ($im, 255,255,255);
$FNT = "C:\windows\fonts\simsun.ttf";
Include ("gb2utf8.php");
$str = Gb2utf8 ("Chinese agun");
Imagettftext ($im, 0, 50,50, $CLR, $fnt, $STR);
Imagegif ($im);
Imagedestroy ($im);
?>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.