The first line indicates the start of the user’s session on our page.<\/p>\n
The display() function has nothing other than a normal HTML code that displays an image in the browser. Other than that, only styling is done for the output to look presentable.<\/p>\n
Inside the create_image() function, a variable is used to refer the image returned by the imagecreatetruecolor() function which takes the width and length of the image as its arguments. imagepng() creates a png image of the specified name and path (in the same directory). <\/p>\n
A black image will be the output after our first step.<\/p>\n
<\/p>\n
Note that the function imagepng() will be the last line of our function and all the following steps are to be inserted in the create_image() function before this function call only, else they would not take effect. <\/p>\n
Any shape can be chosen for the captcha. We’ll choose a rectangle by using the function imagefilledrectangle(). It takes five arguments – image reference, starting x-pos, starting y-pos, ending x-pos, ending y-pos, and the background color. You may use the corresponding function for an ellipse for generating elliptical captcha.<\/p>\n
The imagecolorallocate() function allocates a color to a variable as it takes the RGB combination of the color as arguments. The following code is to be appended in the create() function. <\/p>\n
<\/span>session_start();\n<\/span><\/span>?><\/span>\n<\/span>\n ><\/span>demo.php<\/title<\/span>><\/span>\n<\/span>
国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂
style=\"background-color:#ddd; <\/span>\"<\/span><\/span>><\/span>\n<\/span>\n <\/span> create_image();\n<\/span><\/span> display();\n<\/span><\/span> \/***** definition of functions *****\/\n<\/span><\/span> function display()\n<\/span><\/span> {\n<\/span><\/span> ?><\/span>\n<\/span>\n style=\"text-align:center;<\/span>\"<\/span><\/span>><\/span>\n<\/span> ><\/span>TYPE THE TEXT YOU SEE IN THE IMAGE<\/h3<\/span>><\/span>\n<\/span> ><\/span>This is just to check if you are a robot<\/b<\/span>><\/span>\n<\/span>\n style=\"display:block;margin-bottom:20px;margin-top:20px;<\/span>\"<\/span><\/span>><\/span>\n<\/span>
src=\"image.png\"<\/span>><\/span>\n<\/span> <\/div<\/span>><\/span>\n<\/span> \/\/div1 ends\n <\/div<\/span>><\/span> \/\/div2 ends\n<\/span>\n <\/span> }\n<\/span><\/span>\n<\/span> function create_image()\n<\/span><\/span> {\n<\/span><\/span> $image = imagecreatetruecolor(200, 50);\n<\/span><\/span> imagepng($image, \"image.png\");\n<\/span><\/span> }\n<\/span><\/span>\n<\/span> ?><\/span>\n<\/span> <\/body<\/span>><\/span>\n<\/span><\/span>?><\/span><\/span><\/pre>\nThe previous image will be white after this step.<\/p>\n
<\/p>\n
Generate random lines.<\/h3>\n
Now, we actually start with making the distortion part of the captcha. In PHP, the lines are generated from the starting point(x1,y1) to the end point(x2,y2). Now as we want our lines to touch both ends of the box, we will keep the coordinates as i.e., the complete width of our box. The coordinates will be randomly generated. This will create just one random line. We will generate multiple lines by putting this functionality inside a for loop. <\/y1><\/x1><\/p>\n$background_color = imagecolorallocate($image, 255, 255, 255); \n<\/span>imagefilledrectangle($image,0,0,200,50,$background_color);<\/span><\/pre>\nThe imageline() function takes the x1,x2,y1,y2 coordinates as arguments in that order apart from the image reference and color of the line. The line color has been allocated just as the background color had been allocated in the previous step. <\/p>\n
The y-coordinate is given as rand()*P because this is the height of our box and will always return a value under 50. You may alternatively use rand(0,50). They will yield the same output range. <\/p>\n
<\/p>\n\n
Generate random dots.<\/h3>\n
Random dots will be generated in the same way as random lines. The function used is imagesetpixel(). This function takes the value of coordinates where the dot will be placed in the box.<\/p>\n
$line_color = imagecolorallocate($image, 64,64,64); \n<\/span>for($i=0;$i<10;$i++) {\n<\/span> imageline($image,0,rand()%50,200,rand()%50,$line_color);\n<\/span>}<\/span><\/pre>\nThe x-coordinate is randomly generated by using rand()* 0 as this is the width of our box and this will always return a value under 200. You may alternatively use rand(0,200). They will yield the same output range. The y coordinate is generated as in the lines step.<\/p>\n
<\/p>\n
Generate random text<\/h3>\n
We will randomly point to a position in the string (which contains the alphabet in both lower and upper case) and assign it to the variable $letter<\/p>\n
<\/span>session_start();\n<\/span><\/span>?><\/span>\n<\/span>\n ><\/span>demo.php<\/title<\/span>><\/span>\n<\/span> style=\"background-color:#ddd; <\/span>\"<\/span><\/span>><\/span>\n<\/span>\n <\/span> create_image();\n<\/span><\/span> display();\n<\/span><\/span> \/***** definition of functions *****\/\n<\/span><\/span> function display()\n<\/span><\/span> {\n<\/span><\/span> ?><\/span>\n<\/span>\n style=\"text-align:center;<\/span>\"<\/span><\/span>><\/span>\n<\/span> ><\/span>TYPE THE TEXT YOU SEE IN THE IMAGE<\/h3<\/span>><\/span>\n<\/span> ><\/span>This is just to check if you are a robot<\/b<\/span>><\/span>\n<\/span>\n style=\"display:block;margin-bottom:20px;margin-top:20px;<\/span>\"<\/span><\/span>><\/span>\n<\/span>
src=\"image.png\"<\/span>><\/span>\n<\/span> <\/div<\/span>><\/span>\n<\/span> \/\/div1 ends\n <\/div<\/span>><\/span> \/\/div2 ends\n<\/span>\n <\/span>