php產生圖片驗證碼_PHP教程

來源:互聯網
上載者:User

php產生圖片驗證碼


  驗證碼在WEB應用中非常重要,通常用來防止使用者惡意提交表單,如惡意註冊和登入、論壇惡意灌水等。本文將通過執行個體講解使用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

$image = imagecreatetruecolor(100, 30); //建立畫布

$imagecolor = imagecolorallocate($image, 255, 255, 255); //背景色

imagefill($image, 0, 0, $imagecolor); //填充背景色

for($i=0;$i<4;$i++ ){ //迴圈4位元

$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++ ){ //迴圈 添加幹擾點

$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++){ //迴圈 添加幹擾線

$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);

?>

  再給大家分享一個可以產生中文驗證碼

  ?

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

//1.qi啟用gd庫GD庫提供了一系列用來處理圖片的API,使用GD庫可以處理圖片,或者產生圖片。

// 在網站上GD庫通常用來產生縮圖或者用來對圖片加浮水印或者對網站資料產生報表。

session_start();

// 把GBK編碼的字串轉換成UTF-8字串,第一個參數之所以寫GBK,是因為本php檔案在主機中儲存的編碼是GBK編碼

// UTF-8編碼瀏覽器普遍支援,通用性強,這裡就轉換成UTF-8

$str = iconv("GBK", "utf-8", "芸芸眾生綠水青山名勝古迹敞開心胸便會雲蒸霞蔚快樂將永遠伴隨著你");

if(!is_string($str) || !mb_check_encoding($str,"utf-8"))

{

exit("不是字串或者不是utf-8");

}

$zhongwenku_size;

// 按UTF-8編碼方式擷取字串的長度

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

// 把上述字元匯入數組中

$zhongwenku = array();

for( $i=0; $i<$zhongwenku_size; $i++)

{

$zhongwenku[$i] = mb_substr($str, $i,1,"UTF-8");

}

$result = "";

// 圖片上要寫入的四個字元

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;

// 建立一個真彩圖片 寬100,高30

$img = imagecreatetruecolor(100, 30);

// 分配背景顏色

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

// 分配文字顏色

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

// 在圖片上寫字串

//imagestring($img, rand(3,8), rand(1,70), rand(1,10), $result, $te);

// 在圖片上根據載入字型可以寫出特殊字型

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));

// 畫線

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));

// 為圖片添加噪點

for($i=0; $i<200; $i++)

{

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

}

// 發送http頭資訊 指定本次發送的是image中的jpeg

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

// 輸出jpeg圖片至瀏覽器

imagejpeg($img);

?>

  再來一個執行個體吧

  ?

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

session_start();

function random($len) {

$srcstr = "1a2s3d4f5g6hj8k9qwertyupzxcvbnm";

mt_srand();

$strs = "";

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

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

}

return $strs;

}

//隨機產生的字串

$str = random(4);

//驗證碼圖片的寬度

$width = 50;

//驗證碼圖片的高度

$height = 25;

//聲明需要建立的圖層的圖片格式

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

//建立一個圖層

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

//背景色

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

//模糊點顏色

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

//字型色

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

//繪模糊作用的點

mt_srand();

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

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

}

//輸出字元

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

//輸出矩形

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

//輸出圖片

imagepng($im);

imagedestroy($im);

$str = md5($str);

//選擇 cookie

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

//選擇 Session

$_SESSION["verification"] = $str;

?>

  接下來只要在頁面中調用就可以了:

  ?

1

  如果想實現 "看不清?換一張" 效果,添加如下 JS 到頁面中

  ?

1

2

3

function changing(){

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

}

  以上所述就是本文的全部內容了,希望大家能夠喜歡。

http://www.bkjia.com/PHPjc/1013643.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1013643.htmlTechArticlephp產生圖片驗證碼 驗證碼在WEB應用中非常重要,通常用來防止使用者惡意提交表單,如惡意註冊和登入、論壇惡意灌水等。本文將通過執行個體講解...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.