Chinese display in image functions [go]. I remember someone asked me.
Last Update:2017-01-13
Source: Internet
Author: User
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.