PHP function imagettftext() and unicode

前端 未结 5 1096
说谎
说谎 2020-11-30 07:09

I\'m using the PHP function imagettftext() to convert text into a GIF image. The text I am converting has Unicode characters including Japanese. Everything works fine on my

5条回答
  •  粉色の甜心
    2020-11-30 07:41

    I have been having the same problem with a script that will render text in an image and output it. Problem was, that due to different browsers (or code hardiness/paranoia, whichever way you want to think of it), I had no way of knowing what encoding was being put inside the $_GET array.

    Here is how I solved the problem.

    $item_text = $_GET['text'];
    
    # detect if the string was passed in as unicode
    $text_encoding = mb_detect_encoding($item_text, 'UTF-8, ISO-8859-1');
    # make sure it's in unicode
    if ($text_encoding != 'UTF-8') {
        $item_text = mb_convert_encoding($item_text, 'UTF-8', $text_encoding);
    }
    
    # html numerically-escape everything (&#[dec];)
    $item_text = mb_encode_numericentity($item_text,
        array (0x0, 0xffff, 0, 0xffff), 'UTF-8');
    

    This solves any problem with imagettftext not being able to handle characters above #127 by simply changing ALL the characters (including multibyte Unicode characters) into their HTML numeric character entity—"A" for "A", "B" for "B", etc.—which the manual page claims support for.

提交回复
热议问题