当前位置:   article > 正文

[php]文字生成图片imagettftext(): any2eucjp(): invalid code in input string问题解决_imagettfbbox(): any2eucjp(): invalid code in input

imagettfbbox(): any2eucjp(): invalid code in input string

源地址: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,然后用函数再次转换】

  1. public static function to_entities($string)
  2. {
  3. $len = strlen($string);
  4. $buf = "";
  5. for($i = 0; $i < $len; $i++){
  6. if (ord($string[$i]) <= 127){
  7. $buf .= $string[$i];
  8. } else if (ord ($string[$i]) <192){
  9. //unexpected 2nd, 3rd or 4th byte
  10. $buf .= "&#xfffd";
  11. } else if (ord ($string[$i]) <224){
  12. //first byte of 2-byte seq
  13. $buf .= sprintf("&#%d;",
  14. ((ord($string[$i + 0]) & 31) << 6) +
  15. (ord($string[$i + 1]) & 63)
  16. );
  17. $i += 1;
  18. } else if (ord ($string[$i]) <240){
  19. //first byte of 3-byte seq
  20. $buf .= sprintf("&#%d;",
  21. ((ord($string[$i + 0]) & 15) << 12) +
  22. ((ord($string[$i + 1]) & 63) << 6) +
  23. (ord($string[$i + 2]) & 63)
  24. );
  25. $i += 2;
  26. } else {
  27. //first byte of 4-byte seq
  28. $buf .= sprintf("&#%d;",
  29. ((ord($string[$i + 0]) & 7) << 18) +
  30. ((ord($string[$i + 1]) & 63) << 12) +
  31. ((ord($string[$i + 2]) & 63) << 6) +
  32. (ord($string[$i + 3]) & 63)
  33. );
  34. $i += 3;
  35. }
  36. }
  37. return $buf;
  38. }

在你调用的地方

  1. $text =$this->to_entities($text);
  2. imagettftext($im, $fontSize, 0, $paddingLeft, $offset, $fontColor, $fontStyle, $text);

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/715754
推荐阅读
相关标签
  

闽ICP备14008679号