赞
踩
源地址:http://blog.sina.com.cn/s/blog_6d677b680100tqb3.html
在生成图片的过程中使用了imagettftext(),发现该函数 imagettftext() 文档标 明只接受UTF-8编码,但如果PHP编译时启用–enable-gd-jis-conv选项的话,那么非ASCII字符(例如汉字、拼音、希腊文和箭 头) 会被当成EUC-JP编码 (phpinfo中美其名曰“支持JIS编码的字体”), 从而导致乱码(由于西文字体没有假名或汉字,一般表现为全部是方框)或者报错any2eucjp(): invalid code in input string。
经过长时间搜索。终于通过代码的方式解决了这种错误方式
【先把字符串转换成utf8,然后用函数再次转换】
- public static 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 .= "�";
- } 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;
- }
在你调用的地方
- $text =$this->to_entities($text);
- imagettftext($im, $fontSize, 0, $paddingLeft, $offset, $fontColor, $fontStyle, $text);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。