PHP GD库生成验证码
1.引入GD库,在php.ini中吧extension=php_gd2.dll中把前面;去掉。print_r(gd_info());打印出来相关信息。
学GD库重在理解,1.画图的过程 2,计算机的坐标体系
GD库画图典型流程
1.创建画布 imagecreatetruecolor();
也可以打开一张图片作画布 imagecreatefromjpeg($path);
2.创建颜料 imagecolorcate();
3.画图泼墨渲染 imagefill();
画直线 imageline()
4.保持 imagepng() imagejpeg() imagegif();
5.销毁画布,释放资源 imagedestroy($img);
一些基本操作
代码如下:
1 <?php 2 // //创建画布 3 // $width = 500; 4 // $height = 300; 5 // $img = imagecreate($width, $height); 6 7 // //画笔颜色设置 8 // $red = imagecolorallocate($img, 255, 0,0); 9 // $orange = imagecolorallocate($img,255,125,0); 10 // $yellow = imagecolorallocate($img,255,255,0); 11 // $green = imagecolorallocate($img,0,255,0); 12 // $blue = imagecolorallocate($img,0,0,255); 13 // $indigo = imagecolorallocate($img,0,255,255); 14 // $purple = imagecolorallocate($img,255,0,255); 15 16 // $x1 = 0; 17 // $y1 = 0; 18 // $x2 = 5; 19 // $y2 = 5; 20 21 // imagefill($img,$x1,$y1,$green); 22 23 // imageline($img, $x1, $y1, $x2, $y2, $orange); 24 25 /* 26 * 绘制椭圆 27 */ 28 29 $width = 500; 30 $height = 300; 31 //产生画布 32 $img = imagecreate($width, $height); 33 34 //设置颜色 35 $orange = imagecolorallocate($img, 255, 125, 0); 36 $red = imagecolorallocate($img, 255, 0, 0); 37 $green = imagecolorallocate($img, 0, 255, 0); 38 //画布背景 39 $x2 = 500; 40 $y2 = 300; 41 imagefill($img, $x2, $y2, $orange); 42 43 //绘制圆1 44 $cx = 100; 45 $cy = 60; 46 $w = 80; 47 $h = 80; 48 imageellipse($img, $cx, $cy, $w, $h, $green); 49 50 //绘制圆2 51 $cx2 = 400; 52 $cy2 = 60; 53 $w2 = 80; 54 $h2 = 80; 55 imageellipse($img, $cx2, $cy2, $w2, $h2, $green); 56 57 //绘制多边形 58 $points = array( 59 250,100, 60 300,150, 61 200,150 62 ); 63 $num_points = count($points)/2; 64 imagepolygon($img, $points, $num_points, $green); 65 66 //绘制圆弧 67 $cx3 = 250; 68 $cy3 = 200; 69 $w3 = 200; 70 $h3 = 150; 71 $s3 = 5; 72 $e3 = 175; 73 imagearc($img, $cx3, $cy3, $w3, $h3, $s3, $e3, $green); 74 75 header("Content_type: image/png"); 76 imagepng($img); //输出图像 77 imagedestroy($img); //释放与图像相关联的内存 78 ?>
生产随机验证码
代码如下
<?php $rand_num = rand(); //生成随机数 $str = md5($rand_num); //取得该数md5值 $num_code = 5; //验证码字符数 $str_code = substr($str, 0, $num_code); //从md5截取字符串作为验证码 $width = 120; //验证码图片宽度 $height = 55; //高度 $img = imagecreate($width, $height); //创建图像句柄 $bg_color = imagecolorallocate($img, 255, 255, 255); //背景色 $border_color = imagecolorallocate($img, 0, 0, 0); //前景色 imagerectangle($img, 0, 0, $width-1, $height-1, $border_color); //绘制边框 //随机字体颜色 for ($i=0; $i < $num_code; $i++) { # code... $str_color = imagecolorallocate($img, rand(0,255), rand(0,255), rand(0,255)); $font_size = 10; $str_x = floor( ($width / $num_code) * $i ) + rand(0,5); $str_y = rand( 2,$height - 15 ); imagestring($img, $font_size, $str_x, $str_y, $str_code[$i], $str_color); } //绘制随机干扰点颜色 $num_disturb_points = 200; for ($i=0; $i < $num_disturb_points; $i++) { # code... $point_color = imagecolorallocate($img, rand(0,255), rand(0,255), rand(0,255)); $point_x = rand(2,$width - 2); $point_y = rand(2,$height - 2); imagesetpixel($img, $point_x, $point_y, $point_color); } //绘制随机颜色直线 $line = 5; for ($i=0; $i < $line; $i++) { # code... $line_color = imagecolorallocate($img, rand(0,255), rand(0,255), rand(0,255)); $line_x1 = rand(2,$width - 2); $line_y1 = rand(2,$height - 2); $line_x2 = rand(2,$width - 2); $line_y2 = rand(2,$height - 2); imageline($img, $line_x1, $line_y1, $line_x2, $line_y2, $line_color); } header("Content-type: image/png"); //发送header信息 imagepng($img); //输出图像 imagedestroy($img); //释放与图像关联内存 ?>