Author information: Sadly
)
Article Information: Intermediate Atmosphere Date: 2001 11 Keywords: GD Output Chinese Image
Source link:
http://www.chinalin ket.com/read.htm?id=900
Summary:
PHP (GD library) outputs the conversion function of Chinese images
The following is a function of GB2312 -> UTF-8, Western, I copied
Unicode -> UTF-8 algorithm, so actual than GB2312-> Unicode
Not much, please pay attention to calling GB2UTF8 () can not be mixed in English.
GB2312.TXT file required by the program
GB2UTF8.PHP
// Program Writen by Sadly www.phpx.com
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);
$ UTF8 = ""
While ($ GB)
{
IF (ORD (Substr ($ GB, 0, 1)> 127)
{
$ this = Substr ($ GB, 0, 2);
$ GB = SUBSTR ($ GB, 2, STRLEN ($ GB));
$ UTF8. = U2UTF8 (HEXDEC ($ CODETABLE [HEXDEC (Bin2HEX ($ this)) - 0x8080]));
}
Else
{
$ GB = SUBSTR ($ GB, 1, STRLEN ($ GB));
$ UTF8. = U2UTF8 (Substr ($ GB, 0, 1));
}
}
$ RET = "";
For ($ I = 0; $ I $ RET. = CHR (Substr ($ UTF8, $ I, 3);
Return $ RET;
}
Function U2UTF8 ($ C)
{
For ($ I = 0; $ 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;
}
?>
Calling example: Output "China" two Chinese characters via GD
EXAMPLE.PHP
//Header ("Content-Type: Image / GIF");
$ IM = ImageCreate (400, 300);
$ bkg = imagecoloralloccate ($ IM, 0, 0); $ CLR = ImageColoralLocate ($ IM, 255, 255, 255);
$ fnt = "d: /winnt/fonts/simhei.ttf";
Include ("GB2UTF8.PHP");
$ Str = GB2UTF8 ("China");
ImagetTftext ($ IM, 20, 0, 10, 20, $ CLR, $ FNT, $ STR);
ImageGIF ($ IM);
ImageDestroy ($ IM);
?>