PHP在使用Intervention/image往图片插入文字时,报了imagettfbbox(): any2eucjp(): invalid code in input string错误。原因是PHP的一个bug把我们输入的文字被识别成日文。
方法一:
网上大多数解决方法是重新编译PHP,取消–enable-gd-jis-conv这个参数。
方法二:
对我们输入的字符编码进行转换,只要不是UTF8就不会被识别成日文。所以我们用下面的方法把编码转成unicode。
01
02
03
04
05
06
07
08
09
10
11
|
function to_unicode( $string ) { $str = mb_convert_encoding( $string , 'UCS-2' , 'UTF-8' ); $arrstr = str_split ( $str , 2); $unistr = '' ; foreach ( $arrstr as $n ) { $dec = hexdec(bin2hex( $n )); $unistr .= '&#' . $dec . ';' ; } return $unistr ; } |
千万不用再使用PHP的iconv方法,每台机器上转换后的编码都有可能不一样。
推荐使用mb库的mb_convert_encoding进行转换。