PHP中仿制 ecshop验证码实例

2025-05-29 0 61

仿制ecshop验证码的代码如下所示:

?

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
<?php

//仿制ecshop验证码(四位大写字母和数字、背景)

//处理码值(四位大写字母和数字组成)

//所有的可能的字符集合

$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';

$chars_len = strlen($chars); //集合长度

//随机选取

$code_len = 4;//验证码长度

$code=''; //验证码值初始化

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

//随机取得一个字符下标

$rand_index = mt_rand(0,$chars_len-1);

//利用字符串的下标操做,获得选择的字符

$code .= $chars[$rand_index];

}

//echo $code;

//存储于session中(用于校验)

session_start();

$_SESSION['code'] = $code;

//验证码图像(已知的背景图片)

//处理背景

$bg_file= './captcha/captcha_bg' . mt_rand(1,5). '.jpg';

//依据该图片,创建画布

$image = imagecreatefromjpeg($bg_file);

//简单的将字符串写在画布上的函数(imageString();)

//imageString(画布,字体,位置X, 位置y,字符串内容,颜色);

//字体:imagestring函数,使用的内置字体。由1-5表示。位置由字符串左上角的坐标决定。颜色也是需要预先分配好的。imagecolorallocate();

//分配字体颜色(随机分配黑色或者白色)

if(mt_rand(0,1)==1){

$str_color = imagecolorallocate($image,0,0,0); //黑色

}else{

$str_color = imagecolorallocate($image,255,0xff,255);//白色

}

//内置5号字体

$font = 5;

//位置

//画布大小

$image_w = imagesx($image);

$image_h = imagesy($image);

//获得字体的宽和高

$font_w = imagefontwidth($font);

$font_h = imagefontheight($font);

//获得字符串的宽高

$str_w = $font_w * $code_len;

$str_h = $font_h;

//计算位置

$str_x = ($image_w-$str_w) / 2;

$str_y = ($image_h-$str_h) / 2;

//字符串

imagestring($image,$font,$str_x,$str_y,$code,$str_color);

//输出和销毁画布

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

imagejpeg($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
//验证码工具类(将所有和验证码操作相关的全部封装到该类中)

class Captcha{

/*生成验证码*/

public function makeImage($code_len=4){

//仿制ecshop验证码(四位大写字母和数字、背景)

//处理码值(四位大写字母和数字组成)

//所有的可能的字符集合

$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';

$chars_len = strlen($chars); //集合长度

//随机选取

$code=''; //验证码值初始化

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

//随机取得一个字符下标

$rand_index = mt_rand(0,$chars_len-1);

//利用字符串的下标操做,获得选择的字符

$code .= $chars[$rand_index];

}

//echo $code;

//存储于session中(用于校验)

@session_start();

$_SESSION['code'] = $code;

//验证码图像(已知的背景图片)

//处理背景

$bg_file= TOOL . './captcha/captcha_bg' . mt_rand(1,5). '.jpg';

//依据该图片,创建画布

$image = imagecreatefromjpeg($bg_file);

//简单的将字符串写在画布上的函数(imageString();)

//imageString(画布,字体,位置X, 位置y,字符串内容,颜色);

//字体:imagestring函数,使用的内置字体。由1-5表示。位置由字符串左上角的坐标决定。颜色也是需要预先分配好的。imagecolorallocate();

//分配字体颜色(随机分配黑色或者白色)

if(mt_rand(0,1)==1){

$str_color = imagecolorallocate($image,0,0,0); //黑色

}else{

$str_color = imagecolorallocate($image,255,0xff,255);//白色

}

//内置5号字体

$font = 5;

//位置

//画布大小

$image_w = imagesx($image);

$image_h = imagesy($image);

//获得字体的宽和高

$font_w = imagefontwidth($font);

$font_h = imagefontheight($font);

//获得字符串的宽高

$str_w = $font_w * $code_len;

$str_h = $font_h;

//计算位置

$str_x = ($image_w-$str_w) / 2;

$str_y = ($image_h-$str_h) / 2;

//字符串

imagestring($image,$font,$str_x,$str_y,$code,$str_color);

//输出和销毁画布

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

imagejpeg($image);

imagedestroy($image);

}

}

以上所述是小编给大家介绍的PHP中仿制 ecshop验证码实例,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对快网idc网站的支持!

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 PHP中仿制 ecshop验证码实例 https://www.kuaiidc.com/95394.html

相关文章

发表评论
暂无评论