Why is the picture not displayed when using the GD2 function to generate a 4-digit verification code?
S.L
S.L 2017-08-21 20:38:54
0
1
1301

#<?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.L
S.L

reply all(1)
风豆丁

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 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 author 2017-08-22 23:22:07
  • reply 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<strlen($_SESSION['check_checks']);$i++){ //Loop to read the verification code in the SESSION variable $font=mt_rand(3,5); //Set a random font $x=mt_rand(1,8)+$image_width*$i/4; //Set the X coordinate of the random character location $y=mt_rand(1,$image_height/4); //Set the Y coordinate of the random character location $color=imagecolorallocate($num_image,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200)); //Set the color of the character imagestring($num_image,$font,$x,$y,$_SESSION['check_checks'][$i],$color); //Horizontal output characters } imagepng($num_image); //Generate images in PNG format imagedestroy($num_image); //Release image resources
    风豆丁 author 2017-08-23 22:23:48
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template