gb2utf8.php files are as follows:
Copy Code code as follows:
<?php
Class Gb2utf8
{
var $gb; GB2312 string to be converted
var $utf 8; Converted UTF8 String
var $CodeTable; Array of GB2312 code files used during conversion
var $ErrorMsg; Error messages during the conversion process
function Gb2utf8 ($InStr = "")
{
$this->gb= $InStr;
$this->setgb2312 ();
($this->gb== "")? 0: $this->convert ();
}
function SetGb2312 ($InStr = "Gb2312.txt")
{//Set gb2312 code file, defaults to Gb2312.txt
$this->errormsg= "";
$tmp = @file ($INSTR);
if (! $tmp) {
$this->errormsg= "No GB2312";
return false;
}
$this->codetable=array ();
while (list ($key, $value) =each ($tmp)) {
$this->codetable[hexdec (substr ($value, 0,6))]=substr ($value, 7,6);
}
}
function Convert ()
{//Convert GB2312 string to UTF8 string, required to be set in advance $GB
$this->utf8= "";
if (!trim ($this->gb) | | $this->errormsg!= "") {
Return ($this->utf8= $this->errormsg);
}
$str = $this->gb;
while ($STR) {
if (Ord (substr ($STR, 0,1)) >127)
{
$tmp =substr ($str, 0,2);
$str =substr ($str, 2,strlen ($STR));
$tmp = $this->u2utf8 (Hexdec ($this->codetable[hexdec (Bin2Hex ($tmp)) -0x8080]);
for ($i =0; $i <strlen ($tmp); $i +=3)
$this->UTF8.=CHR (substr ($tmp, $i, 3));
}
Else
{
$tmp =substr ($str, 0, 1);
$str =substr ($str, 1,strlen ($STR));
$this->utf8.= $tmp;
}
}
return $this->utf8;
}
function U2utf8 ($INSTR)
{
for ($i =0; $i <count ($INSTR); $i + +)
$str = "";
if ($InStr < 0x80) {
$str. =ord ($INSTR);
}
else if ($InStr < 0x800) {
$str. = (0xc0 | $InStr >>6);
$str. = (0x80 | $InStr & 0x3F);
}
else if ($InStr < 0x10000) {
$str. = (0xe0 | $InStr >>12);
$str. = (0x80 | $InStr >>6 & 0x3F);
$str. = (0x80 | $InStr & 0x3F);
}
else if ($InStr < 0x200000) {
$str. = (0xF0 | $InStr >>18);
$str. = (0x80 | $InStr >>12 & 0x3F);
$str. = (0x80 | $InStr >>6 & 0x3F);
$str. = (0x80 | $InStr & 0x3F);
}
return $str;
}
}
?>
The test files are as follows:
Copy Code code as follows:
<?php
Header ("Content-type:image/png");
$im = Imagecreate (400,300);
$black = Imagecolorallocate ($im, 0,0,0);
$white = Imagecolorallocate ($im, 184,44,6);
Include ("gb2utf8.php");
$obj =new Gb2utf8 ();
$obj->gb= "123abc China 456def test is correct";
$obj->convert ();
Imagettftext ($im, 0, 5, $white, "Simkai. TTF ", $obj->utf8);
Imagepng ($im);
Imagedestroy ($im);
?>
Description
The font file needs to be set correctly, please confirm that you can use font directly (without Gb2utf8) to output English.