加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP中仿制 ecshop验证码实例

发布时间:2020-12-12 21:57:36 所属栏目:PHP教程 来源:网络整理
导读:仿制ecshop验证码的代码如下所示: //验证码图像(已知的背景图片) //处理背景 $bg_file= './captcha/captcha_bg' . mt_rand(1,5). '.jpg'; //依据该图片,创建画布 $image = imagecreatefromjpeg($bg_file); //简单的将字符串写在画布上的函数(imageStrin

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

//验证码图像(已知的背景图片)
//处理背景
$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); //黑色
}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验证码实例。编程之家 52php.cn 收集整理的教程希望能对你有所帮助,如果觉得编程之家不错,可分享给好友!感谢支持。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读