Chinese display in image functions [go]. I remember someone asked me.

Source: Internet
Author: User
Tags chr strlen
Zlm June 7, 2001
Send it to me to talk about my views.

I read a piece of code on www.phpx.com to convert the GB code to UTF8. In php, TTF supports UTF8 encoded non-ASCII characters. after analyzing this code, we found that the image can be output in combination with Chinese characters and ASCII characters, which makes it easier for us to operate Image functions.
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,)] = substr ($ value );
$ Utf8 = "";
While ($ gb)
{
If (ord (substr ($ gb, 127)>)
{
$ This = substr ($ gb, 0, 2 );
$ Gb = substr ($ gb, 2, strlen ($ gb)-2 );
$ Utf8. = u2utf8 (hexdec ($ codetable [hexdec (bin2hex ($ this)-0x8080]);
}
Else
{
$ This = substr ($ gb, 0, 1 );
$ Gb = substr ($ gb, 1, strlen ($ gb)-1 );
$ Utf8. = u2utf8 ($ this );
}
}
/* $ Ret = "";
For ($ I = 0; $ I <strlen ($ utf8); $ I + = 3)
$ Ret. = chr (substr ($ utf8, $ I, 3 ));
Return $ ret ;*/
Return $ utf8;
}
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 ("32434 in aaa ");
ImageTTFText ($ im, 90, 10,110,300, $ white, "/usr/share/fonts/default/TrueType/simsun. ttc ",
$ Str );
ImageJPEG ($ im );
ImageDestroy ($ im );
?>
Go to www.phpx.com to find the GB2312.txt file.

Related Article

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.