PHP雪花背景驗證碼

來源:互聯網
上載者:User
ValidateCode.class.php

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

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

/**

* 驗證碼類

* @author staven

*

*/

class ValidateCode{

private $width ; //驗證碼圖片的寬度

private $height ; //驗證碼圖片的高度

private $checkCode ; //驗證碼字元

private $codeNum ; //驗證碼字元個數

private $font ; //字型

private $fontSize ; //字型大小

private $fontColor ; //字型顏色

private $image ; //驗證碼控制代碼

private $charset ; //驗證字元隨機因子

//構造器方法初始化

function __construct( $width =100, $height =20, $codeNum =4, $fontSize = 12){

$this ->width = $width ;

$this ->height = $height ;

$this ->codeNum = $codeNum ;

$this ->fontSize = $fontSize ;

$this ->font = dirname( __FILE__ ) . '/font/elephant.ttf' ;

$this ->charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789' ; //提供隨機因子

$this ->checkCode = $this ->createCheckCode();

}

/* 用於輸出驗證碼圖片,並且將驗證碼資訊寫入session */

function __toString(){

/* 加到session中, 儲存下標為code */

$_SESSION [ "code" ] = strtoupper ( $this ->checkCode);

$this ->outImage();

return '' ;

}

/* 產生隨機驗證碼字元 */

private function createCheckCode(){

for ( $i =0; $i < $this ->codeNum; $i ++){

$this ->checkCode .= $this ->charset{rand(0, strlen ( $this ->charset)-1)};

}

return $this ->checkCode;

}

/* 輸出驗證碼 */

private function outImage(){

$this ->getCreateImage();

$this ->createLine();

$this ->outputText();

$this ->outputImage();

}

/* 產生驗證碼背景圖片 */

private function getCreateImage(){

$this ->image = imagecreatetruecolor( $this ->width, $this ->height);

$color = imagecolorallocate( $this ->image, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));

imagefilledrectangle( $this ->image,0, $this ->height, $this ->width,0, $color );

}

/* 向控制代碼圖片中寫入文字資訊 */

private function outputText(){

$_x = $this ->width / $this ->codeNum;

for ( $i =0; $i < $this ->codeNum; $i ++) {

$this ->fontcolor = imagecolorallocate( $this ->image,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));

imagettftext( $this ->image, $this ->fontSize,mt_rand(-30,30), $_x * $i +mt_rand(1,5), $this ->height / 1.4, $this ->fontColor, $this ->font, $this ->checkCode[ $i ]);

}

}

/* 建立雪花圖案 */

private function createLine() {

for ( $i =0; $i <6; $i ++) {

$color = imagecolorallocate( $this ->image,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));

imageline( $this ->image,mt_rand(0, $this ->width),mt_rand(0, $this ->height),mt_rand(0, $this ->width),mt_rand(0, $this ->height), $color );

}

for ( $i =0; $i <100; $i ++) {

$color = imagecolorallocate( $this ->image,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));

imagestring( $this ->image,mt_rand(1,5),mt_rand(0, $this ->width),mt_rand(0, $this ->height), '*' , $color );

}

}

/* 產生驗證碼圖片 */

private function outputImage(){

header( 'Content-type:image/png' );

imagepng( $this ->image);

imagedestroy( $this ->image);

}

/* 銷毀控制代碼,釋放資源 */

function __destruct() {

imagedestroy( $this ->image);

}

}

imageCode.php

1

2

3

4

session_start(); //開啟SESSION

require_once 'ValidateCode.class.php' ;

echo new ValidateCode();

image.php

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

驗證碼

/** 用於 */

session_start(); //開啟SESSION

if (isset( $_POST [ 'submit' ])){ //判斷使用者提交後執行

/* 判斷使用者在表單中輸入的字串和驗證碼圖片中的字串是否相同 */

if ( strtoupper (trim( $_POST [ "code" ])) == $_SESSION [ 'code' ]){ //如果驗證碼輸出成功

echo '驗證碼輸入成功
' ; //輸出成功的提示資訊

} else { //如果驗證碼輸入失敗

echo '驗證碼輸入錯誤!!
' ; //輸出失敗的輸入資訊

}

}

?>



來自為知筆記(Wiz)

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.