Chinese display in the image function

Source: Internet
Author: User
Tags chr strlen utf 8
Convert GB code to utf8,php TTF supports UTF8 encoded non-ASCII character output. After analyzing this code, we find that the mixed output image of Chinese and ASCII can be realized, which makes it more convenient for us to manipulate the image function.
The code is as follows:
?

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);

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

/* $ret = "";
for ($i =0; $i <strlen ($utf 8); $i +=3)
$ret. =CHR (substr ($utf 8, $i, 3));

return $ret; *

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


--------------------------------------------
?
Header ("Content-type:image/jpeg");
$im = imagecreate (800, 400);
$black = imagecolorallocate ($im, 0, 0, 0);
$white = Imagecolorallocate ($im, 255, 255, 255);
Include ("gb2utf8.php");
$str =gb2utf8 ("AAA 32434");
Imagettftext ($im, $white, "/USR/SHARE/FONTS/DEFAULT/TRUETYPE/SIMSUN.TTC",
$STR);

Imagejpeg ($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.