解决PHP函数imagettftext生成二维码中文乱码问题

$str1 = ‘我是’;
imagettftext($target, 20, 0, 150, 70, $bc, $font, $this->to_entities($str1));
imagettftext($target, 20, 0, 210, 70, $cc, $font, $this->to_entities($userinfo[‘nickname’]));

public function to_entities($string){
$len = strlen($string);
$buf = “”;
for($i = 0; $i < $len; $i++){ if (ord($string[$i]) <= 127){ $buf .= $string[$i]; } else if (ord ($string[$i]) <192){ //unexpected 2nd, 3rd or 4th byte $buf .= "&#xfffd"; } else if (ord ($string[$i]) <224){ //first byte of 2-byte seq $buf .= sprintf("&#%d;", ((ord($string[$i + 0]) & 31) << 6) + (ord($string[$i + 1]) & 63) ); $i += 1; } else if (ord ($string[$i]) <240){ //first byte of 3-byte seq $buf .= sprintf("&#%d;", ((ord($string[$i + 0]) & 15) << 12) + ((ord($string[$i + 1]) & 63) << 6) + (ord($string[$i + 2]) & 63) ); $i += 2; } else { //first byte of 4-byte seq $buf .= sprintf("&#%d;", ((ord($string[$i + 0]) & 7) << 18) + ((ord($string[$i + 1]) & 63) << 12) + ((ord($string[$i + 2]) & 63) << 6) + (ord($string[$i + 3]) & 63) ); $i += 3; } } return $buf; } [/php]

分享到: 更多 (0)