Home  >  Q&A  >  body text

Why is the picture not displayed when using the GD2 function to generate a 4-digit verification code?


#<?php

session_start();
header("content-type:image/png"); / /Set the format of creating images
$ Image_width = 70; // Set image width
$ Image_height = 18; // Set image height
srand (microtime ()*100000); // Set random number of random numbers Seed
for ($ i = 0; $ i & lt; 4; $ i) {// Cycle output a 4 -bit random number
$ New_number. = Dechex (RAND (0,15));
}
$_SESSION[check_checks]=$new_number; //Write the obtained random number verification code into the SESSION variable

$num_image=imagecreate($image_width,$image_height); //Create a Canvas
imagecolorallocate($num_image,255,255,255); //Set the color of the canvas
for($i=0;$i<strlen($_SESSION[check_checks]);$i ){ //Loop to read SESSION Verification code in variables
$ font = mt_rand (3,5); // Set random fonts
$ x = mt_rand (1,8) $ image_width*$ I/4; // Set the random characters. X coordinate of the position
$y=mt_rand(1,$image_height/4); //Set the Y coordinate of the position of the random character
$color=imagecolorallocate($num_image,mt_rand(0,100),mt_rand(0,150) ,mt_rand(0,200)); //Set the color of characters
imagestring($num_image,$font,$x,$y,$_SESSION[check_checks][$i],$color); //Output characters horizontally
}
imagepng($num_image);                                                                               using using PNG using PNG format ’                     ’ ‐ ’ s ’ s ‐ ‐ ‐ ‐ ‐ ‐ ​ ​ ​ ​                                           use to use PNG to PNG to use PNG format’s ’ s to use PNG format--

S.LS.L2586 days ago1226

reply all(1)I'll reply

  • 风豆丁

    风豆丁2017-08-22 17:08:36

    There are many errors in your code.

    Comment out header("content-type:image/png"); first and deal with the error first.

    Generally, the header is set above the imagepng() function that generates images, so that errors can be easily adjusted.

    reply
    0
  • S.L

    This code was copied from the tutorial, so it should be correct. It doesn't work according to your method, and the following error occurs: ( ! ) Notice: Undefined variable: new_number in D:wamp64wwwlianxichecks.php on line 7 Call Stack # Time Memory Function Location 1 0.0007 244832 {main}( ) ...checks.php:0

    S.L · 2017-08-22 23:22:07
    风豆丁

    Give $num_number an initial value before the for loop, and the $_SESSION subscript must be a string. Just change it to the following. session_start(); header("content-type:image/png"); //Set the format of the created image $image_width=70; //Set image width $image_height=18; //Set image height srand(microtime()*100000); //Set the random number seed $new_number = ''; for($i=0;$i<4;$i++){ //Loop to output a 4-digit random number $new_number .= dechex(rand(0,15)); } $_SESSION['check_checks']=$new_number; //Write the obtained random number verification code into the SESSION variable $num_image=imagecreate($image_width,$image_height); //Create a canvas imagecolorallocate($num_image,255,255,255); //Set the color of the canvas for($i=0;$i

    风豆丁 · 2017-08-23 22:23:48
  • Cancelreply