php笔记之GD库图片创建/简单验证码

来源:互联网 发布:网络段子写手招聘 编辑:程序博客网 时间:2024/06/05 09:27


燕十八 公益PHP培训 课堂地址:YY频道88354001 学习社区:www.zixue.it  

php画图:比如说验证码,缩略图,加水印都要用到GD库,所以要开启gd2库,才能用

首先找到php.ini中extension=php_gd2.dll  去掉前边的分号,然后重启apache,创建图片就可以用了

 /*     画图的一般步骤:     1.创建画布     2.创建颜料     3.画图或者写字     4.保存     5.销毁资源 */ //创建画布 $img=imagecreatetruecolor(300,200); //创建颜料 $bg=imagecolorallocate($img,30,255,255); //画布填充颜色 imagefill($img,0,0,$bg); //保存图片 if(imagepng($img,'./01.png')){     echo "图片创建成功"; } //销毁图片 imagedestroy($img); 

 

简单验证码

//创建图片 $im = imagecreatetruecolor(50, 30);  // 将背景设为蓝色 $blue = imagecolorallocate($im, 100, 255, 255); //创建颜料 $imgcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); //填充背景颜色 imagefill($im, 0, 0, $blue);   //画干扰线 for($i=0;$i<4;$i++){               imageline($im,rand(0,20),0,100,rand(0,60),$imgcolor); }  //画噪点 for($i=0;$i<100;$i++){          imagesetpixel($im,rand(0,50),rand(0,30),$imgcolor); }  //写字符串 $str=substr(str_shuffle('ABCDEFGHIJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789'),0,4);   imagestring($im,4,10,10,$str,$imgcolor);  //输出图片 header('content-type: image/png'); imagepng($im); //销毁图片 imagedestroy($im); 

 

验证码注意保存格式,utf8无bom格式,只保存为utf8的会出现乱码

输出随机验证码为:



原创粉丝点击