Chinese display in image function [turn], remember someone asked

Source: Internet
Author: User
Tags chr strlen utf 8
function | display | Chinese ZLM June 7, 2001

Email it to me. I'll talk about my opinion. Last article



On the www.phpx.com read a piece of code, is the conversion of GB code to utf8,php TTF support UTF8 encoded non-ASCII character output. After analyzing this code, we find that we can realize the mixed output image of Chinese and ASCII, which is more convenient when we 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);

?>

GB2312.txt file, please go to www.phpx.com.



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.