website logo
Auteur
avatar
Visiteur

Forum » » Le coin des codeurs » » [Résolu] - Bug du captcha en chinois


Posté : 21 juil. 2026 à 18:23 icone du post

DejaVuSans.ttf couvre le latin étendu, cyrillique, grec, etc. — mais pas les idéogrammes chinois. wqy-zenhei.ttc ("WenQuanYi Zen Hei") est spécifiquement une police chinoise — elle contient les glyphes des milliers de caractères CJK.
[code php]
case 'captcha':
    $mot = decrypt($att_id); // on garde en UTF-8, pas besoin de conversion ISO avec imagettftext

    // Détection des caractères chinois (CJK) dans le texte, plutôt que de dépendre
    // d'une variable de langue du site qui peut ne pas correspondre au texte du captcha
    $fontsDir =  'fonts/'; // adapter au chemin réel du dossier fonts sur ton serveur
    $fontFile = preg_match('/[\x{4e00}-\x{9fff}]/u', $mot)
        ? $fontsDir . 'wqy-zenhei.ttc'
        : $fontsDir . 'DejaVuSans.ttf';

    $fontSize = 16;
    $angle = 0;
    $bbox = imagettfbbox($fontSize, $angle, $fontFile, $mot);
    $width  = abs($bbox[2] - $bbox[0]) + 8;
    $height = abs($bbox[1] - $bbox[7]) + 8;

    $img = imagecreatetruecolor($width, $height);
    $blanc = imagecolorallocate($img, 255, 255, 255);
    $noir  = imagecolorallocate($img, 0, 0, 0);
    imagefill($img, 0, 0, $blanc);
    imagecolortransparent($img, $blanc);

    $x = -$bbox[0] + 4;
    $y = -$bbox[7] + 4;
    imagettftext($img, $fontSize, $angle, $x, $y, $noir, $fontFile, $mot);

    imagepng($img);
    imagedestroy($img);
break;
[/code]

Cet article provient de NPDS
https://www.npds.org/viewtopic.php?topic=27112&forum=5