Php generates an image Verification Code

Source: Internet
Author: User

Php generates an image Verification Code

Verification codes are very important in WEB applications. They are usually used to prevent users from submitting forms maliciously, such as malicious registration and login, and malicious Forum bumping. This document describes how to use PHP to generate common verification codes.

First, let's take a look at the general effect.

Then, paste the Code directly.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

<? Php

$ Image = imagecreatetruecolor (100, 30); // create a canvas

$ Imagecolor = imagecolorallocate ($ image, 255,255,255); // background color

Imagefill ($ image, 0, 0, $ imagecolor); // fill the background color

For ($ I = 0; $ I <4; $ I ++) {// 4-digit Loop

$ Fontsize = 6;

$ Fontcolor = imagecolorallocate ($ image, rand (0,200), rand (0,200), rand (0,200 ));

$ Fontcontent = rand (0, 9 );

$ X = $ I * 100/4 + rand (5, 15 );

$ Y = rand (5, 10 );

Imagestring ($ image, $ fontsize, $ x, $ y, $ fontcontent, $ fontcolor );

}

For ($ I = 0; $ I <200; $ I ++) {// Add interference points cyclically

$ Pointcolor = imagecolorallocate ($ image, rand (50,200), rand (50,200), rand (50,200 ));

$ X = rand (1, 99 );

$ Y = rand (1, 29 );

Imagesetpixel ($ image, $ x, $ y, $ pointcolor );

}

For ($ I = 0; $ I <3; $ I ++) {// Add interference lines cyclically

$ Linecolor = imagecolorallocate ($ image, rand (100,250), rand (100,250), rand (100,250 ));

$ X1 = rand (1, 25 );

$ X2 = rand (50, 75 );

$ Y1 = rand (1, 15 );

$ Y2 = rand (15, 25 );

Imageline ($ image, $ x1, $ y1, $ x2, $ y2, $ linecolor );

}

Header ("content-type: image/png ");

Imagepng ($ image );

Imagedestroy ($ image );

?>

I will share with you a Chinese verification code that can be generated.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

<? Php

// 1. qi enables the gd library of the GD library to provide a series of APIS for image processing. The GD library can be used to process images or generate images.

// The GD library on the website is usually used to generate thumbnails, add watermarks to images, or generate reports on website data.

Session_start ();

 

// The GBK encoding string into a UTF-8 string, the first parameter is written GBK, because the PHP file stored in the host encoding is GBK Encoding

// UTF-8 coding browser universal support, versatility, here is converted to UTF-8

$ Str = iconv ("GBK", "UTF-8", "all sentient beings, green mountains, mountains, scenic spots, and monuments will always be with you ");

If (! Is_string ($ str) |! Mb_check_encoding ($ str, "UTF-8 "))

{

Exit ("not a string or not UTF-8 ");

}

$ Zhongwenku_size;

// Obtain the length of the string by UTF-8 Encoding

$ Zhongwenku_size = mb_strlen ($ str, "UTF-8 ");

 

// Import the preceding characters into the Array

$ Zhongwenku = array ();

For ($ I = 0; $ I <$ zhongwenku_size; $ I ++)

{

$ Zhongwenku [$ I] = mb_substr ($ str, $ I, 1, "UTF-8 ");

}

 

$ Result = "";

 

// Four characters to be written to the image

For ($ I = 0; $ I <4; $ I ++)

{

Switch (rand (0, 1 ))

{

Case 0:

$ Result. = $ zhongwenku [rand (0, $ zhongwenku_size-1)];

Break;

Case 1:

$ Result. = dechex (rand (0, 15 ));

Break;

}

 

}

 

$ _ SESSION ["check"] = $ result;

 

// Create a true color image with a width of 100 and a height of 30

$ Img = imagecreatetruecolor (100, 30 );

 

// Assign the background color

$ Bg = imagecolorallocate ($ img, 0, 0 );

 

// Assign text color

$ Te = imagecolorallocate ($ img, 255,255,255 );

 

// Write a string on the Image

// Imagestring ($ img, rand (), $ result, $ te );

 

// Write a special font Based on the loaded font on the Image

Imagettftext ($ img, 13, rand (2, 9), 20, 20, $ te, "MSYH. TTF", $ result );

 

$ _ SESSION ["check"] = $ result;

 

For ($ I = 0; $ I <3; $ I ++)

{

// $ T = imagecolorallocate ($ img, rand (0,255), rand (0,255), rand (0,255 ));

// Draw a line

Imageline ($ img, 0, rand (0, 20), rand (70,100), rand (0, 20), $ te );

}

 

$ T = imagecolorallocate ($ img, rand (0,255), rand (0,255), rand (0,255 ));

// Add noise to the image

For ($ I = 0; I I <200; $ I ++)

{

Imagesetpixel ($ img, rand (1,100), rand (1, 30), $ t );

}

// Send the http header to specify that the sent image is jpeg.

Header ("Content-type: image/jpeg ");

// Output jpeg image to the browser

Imagejpeg ($ img );

 

?>

Let's try another instance.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

<? Php

 

Session_start ();

Function random ($ len ){

$ Srcstr = "1a2s3d4f5g6hj8k9qwertyupzxcvbnm ";

Mt_srand ();

$ Strs = "";

For ($ I = 0; $ I <$ len; $ I ++ ){

$ Strs. = $ srcstr [mt_rand (0, 30)];

}

Return $ strs;

}

 

// Randomly generated string

$ Str = random (4 );

 

// The image width of the Verification Code

$ Width = 50;

 

// Height of the Verification Code Image

$ Height = 25;

 

// Declare the image format of the layer to be created

@ Header ("Content-Type: image/png ");

 

// Create a Layer

$ Im = imagecreate ($ width, $ height );

 

// Background color

$ Back = imagecolorallocate ($ im, 0xFF, 0xFF, 0xFF );

 

// Blur the vertex color

$ Pix = imagecolorallocate ($ im, 187,230,247 );

 

// Font color

$ Font = imagecolorallocate ($ im, 41,163,238 );

 

// Plot the vertices of fuzzy match.

Mt_srand ();

For ($ I = 0; I I <1000; $ I ++ ){

Imagesetpixel ($ im, mt_rand (0, $ width), mt_rand (0, $ height), $ pix );

}

 

// Output characters

Imagestring ($ im, 5, 7, 5, $ str, $ font );

 

// Output rectangle

Imagerectangle ($ im, 0, 0, $ width-1, $ height-1, $ font );

 

// Output image

Imagepng ($ im );

 

Imagedestroy ($ im );

 

$ Str = md5 ($ str );

 

// Select cookie

// SetCookie ("verification", $ str, time () + 7200 ,"/");

 

// Select Session

$ _ SESSION ["verification"] = $ str;

?>

Next, you just need to call it on the page:

?

1

If you want to achieve "cannot see clearly? For a different effect, add the following JS to the page.

?

1

2

3

Function changing (){

Document. getElementById ('checkpic '). src = "/images/checkcode. php? "+ Math. random ();

}

The above is all the content of this article. I hope you will like it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.