GD库创建图像步骤
1 <?php 2 //1.创建画布 3 $width = 300; 4 $height= 200; 5 $image=imagecreatetruecolor($width,$height); 6 7 //2.创建颜色 [RGB红绿蓝] 8 $white=imagecolorallocate($image,255,255,255);//白色 9 $black=imagecolorallocate($image,0,0,0);//黑色 10 $red=imagecolorallocate($image,255,0,0);//红色 11 $green=imagecolorallocate($image,0,255,0);//绿色 12 $blue=imagecolorallocate($image,0,0,255);//蓝色 13 14 //3.进行绘画 15 imagefill($image,0,0,$white);//将背景设置为白色,默认黑色 16 //水平绘制字符 17 imagechar($image,2,40,40,'R',$red); 18 //垂直绘制字符 19 imagecharup($image,3,80,80,'G',$green); 20 21 //水平绘制字符串 22 imagestring($image,4,120,120,"BLUE",$blue); 23 //垂直绘制字符 24 imagestringup($image,5,160,160,'BLACK',$black); 25 26 //画出一条红色的线 27 imageline($image,20,15,200,150,$red); 28 29 //4.输出或保存 30 header('content-type:image/png'); 31 imagejpeg($image); 32 33 if(imagejpeg($image,'./gd.png')) { 34 echo '保存成功'; 35 } else { 36 echo '保存失败'; 37 } 38 39 //5.销毁画布 40 imagedestroy($image);
封装类demo
1 <?php 2 //验证码类 3 class ValidateCode { 4 5 /** 6 *简单封装demo方法 7 */ 8 public $type;//验证字符串类型 9 public $length;//验证字符串长度 10 11 public function __construct($type,$length){ 12 $this->type=$type; 13 $this->length=$length; 14 } 15 16 public function getVerify() { 17 $type=$this->type; 18 $length=$this->length; 19 20 // 随机颜色 21 function getRandColor($image) { 22 return imagecolorallocate($image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); 23 } 24 25 // 创建画布 26 $width=20+$length*40; 27 $height=45; 28 $image=imagecreatetruecolor($width,$height); 29 30 // 创建颜色 31 $white=imagecolorallocate($image,255,255,255); 32 33 // 绘制填充矩形 34 imagefilledrectangle($image,0,0,$width,$height,$white); 35 36 /** 37 * 验证字符串类型 38 * 1-数字 39 * 2-字母 40 * 3-数字+字母 41 */ 42 switch ($type) { 43 case 1: 44 // 数字 45 $codeArray = array_rand(range(0,9),$length); 46 break; 47 case 2: 48 // 字母 49 $codeArray = array_rand(array_flip(array_merge(range('a','z'),range('A','Z'))),$length); 50 break; 51 case 3: 52 // 数字+字母 53 $codeArray = array_rand(array_flip(array_merge(range(0,9),range('a','z'),range('A','Z'))),$length); 54 break; 55 default: 56 exit('非法参数'); 57 break; 58 } 59 60 //session保存验证码,登录时验证code 61 session_start(); 62 $_SESSION['code'] = join('',$codeArray); 63 64 for($i=0;$i<$length;$i++) { 65 $size=mt_rand(20,22); 66 67 $textWidth = imagefontwidth($size); 68 $textHeight= imagefontheight($size); 69 70 $angle=mt_rand(-3,3); 71 72 $x=($width/$length)*$i+$textWidth; 73 $y=mt_rand($height/2,$height-$textHeight); 74 75 $fontfile="Elephant.ttf";//自选字体库 76 $text = $codeArray[$i]; 77 imagettftext($image,$size,$angle,$x,$y,getRandColor($image),$fontfile,$text); 78 } 79 80 // 添加雪花干扰元素 81 for($i=1;$i<=50;$i++) { 82 imagesetpixel($image,mt_rand(0,$width),mt_rand(0,$height),getRandColor($image)); 83 } 84 85 // 绘制线段干扰元素 86 for ($i=1;$i<=6;$i++) { 87 imageline($image,mt_rand(0,$width),mt_rand(0,$height),mt_rand(0,$height),mt_rand(0,$width),getRandColor($image)); 88 } 89 90 // 展示 91 header('content-type:image/png'); 92 imagejpeg($image); 93 94 // 销毁 95 imagedestroy($image); 96 } 97 } 98 99 (new ValidateCode(3,4))->getVerify();