PHP function imagettftext() and unicode

前端 未结 5 1111
说谎
说谎 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:20

    Here's the solution that finally worked for me:

    $text = "你好";
    // Convert UTF-8 string to HTML entities
    $text = mb_convert_encoding($text, 'HTML-ENTITIES',"UTF-8");
    // Convert HTML entities into ISO-8859-1
    $text = html_entity_decode($text,ENT_NOQUOTES, "ISO-8859-1");
    // Convert characters > 127 into their hexidecimal equivalents
    $out = "";
    for($i = 0; $i < strlen($text); $i++) {
        $letter = $text[$i];
        $num = ord($letter);
        if($num>127) {
          $out .= "&#$num;";
        } else {
          $out .=  $letter;
        }
    }
    

    Converting the string to HTML entities works except that the function imagettftext() doesn't accept named entities. For example,

    日本語
    

    is OK, but

    ç
    

    is not. Converting back to ISO-8859-1, converts the named entities back to characters, but there is a second problem. imagettftext() doesn't support characters with a value greater than >127. The final for-loop encodes these characters in hexadecimal. This solution is working for me with the text that I am using (includes Japanese, Chinese and accented latin characters for Portuguese), but I'm not 100% sure it will work in all cases.

    All of these gymnastics are needed because imagettftext() doesn't really accept UTF-8 strings on my server.

提交回复
热议问题