
Solution
1. Ouvrez la bibliothèque gd2 et affichez-la via phpinfo. Effacez la nomenclature. Le code est écrit à partir de la ligne supérieure, le problème peut donc survenir dans le code.
2. Ajoutez l'instruction ob_clean() avant l'en-tête, puis exécutez-la.
Note
Lors de la génération d'images, header('Content-type: image/png'); ne peut pas avoir de sortie devant. Ou ajoutez : ob_clean(); Même si vous utilisez la sortie, vous pouvez utiliser cette phrase pour vider le cache de sortie.
Résoudre des exemples
//Définissez la hauteur et la largeur du code de vérificationnombre de caractères ci-dessus
$img_w = 70 ;
$img_h = 22 ;
$police = 5 ;
$char_len = 5;
// Fusion de tableaux, la fonction range() renvoie un tableau de plages
$char = array_merge ( range ( 'a', 'z' ), range ( 'A', 'Z' ), range ( '1', '9' ) );
$rand_keys = array_rand ( $char, $char_len ); // Prend aléatoirement un nombre spécifié d'éléments du tableau pour générer des valeurs clés
if ($char_len == 1) { //S'il n'y a qu'un seul nombre, array_rand() renvoie un type non-tableau
$rand_keys = tableau ($rand_keys );
}
shuffle($rand_keys); //Pas besoin d'utiliser
$code = '';
foreach ( $rand_keys comme $k ) {
$code .= $char [$k];
}
session_start ();
$_SESSION ['captcha'] = $code;
//Ajouter une ligne et une couleur
//Créer une nouvelle image
$img = imagecreatetruecolor ( $img_w, $img_h );
//attribuer une couleur
$bg_color = imagecolorallocate ( $img, 0xcc, 0xcc, 0xcc );
//Couleur de fond de la toile
imagefill ( $img, 0, 0, $bg_color );
//ligne d'interférence
pour($i = 0; $i < 300; ++$i) {
$color = imagecolorallocate ( $img, mt_rand ( 0, 255 ), mt_rand ( 0, 255 ), mt_rand ( 0, 255 ) );
imagesetpixel ( $img, mt_rand ( 0, $img_w ), mt_rand ( 0, $img_h ), $color );
}
pour($i = 0; $i <= 10; ++ $i) {
//Définit la couleur de la ligne
$color = imageColorAllocate ( $img, mt_rand ( 0, 255 ), mt_rand ( 0, 255 ), mt_rand ( 0, 255 ) );
//Trace une ligne droite au hasard sur l'image $img
imageline ( $img, mt_rand ( 0, $img_w ), mt_rand ( 0, $img_h ), mt_rand ( 0, $img_w ), mt_rand ( 0, $img_h ), $color );
//imagesetpixel($img,mt_rand(0,$img_w),mt_rand(0,$img_h),$color);
}
//Ajouter un cadre
$rect_color = imagecolorallocate ( $img, 0x90, 0x90, 0x90 );
imagerectangle ( $img, 0, 0, $img_w - 1, $img_h - 1, $rect_color );
$str_color = imagecolorallocate ( $img, mt_rand ( 0, 100 ), mt_rand ( 0, 100 ), mt_rand ( 0, 100 ) );
$font_w = imagefontwidth ( $font );
$font_h = imagefontheight ( $font );
$str_len = $font_w * $char_len;
imagestring ( $img, $font, ($img_w - $str_len) / 2, ($img_h - $font_h) / 2, $code, $str_color );Ce qui précède est la solution au problème selon lequel php ne peut pas générer d’images. J’espère que cela sera utile à tout le monde.