php+二维码图片生成封装成对象类
2017-01-16 22:56
190 查看
<?php session_start(); //验证码类 /* * 背景 * 干扰 * 文字 * 输出 * 释放资源 */ class Code{ public $img; //画布资源 public $width; //验证码画布的宽 public $height; //验证码画布的高 public $length; //验证码的位数 public $words; //验证码的文字, //此成员属性用于保存到session中 //也用于将此文字输出到画布上 //构造方法 给成员属性赋初值 function __construct($width,$height,$length){ $this->width = $width; $this->height = $height; $this->length = $length; } //出口程序 function printImg(){ //背景 $this->bg(); //干扰 $this->disturb(); //产生文字 $this->getCode(); //将文字画到画布上 $this->printWord(); //输出 $this->outImg(); } //背景 private function bg(){ $this->img = imagecreatetruecolor($this->width,$this->height); $bg_color = imagecolorallocate($this->img,rand(200,255),rand(200,255),rand(200,255)); imagefill($this->img,0,0,$bg_color); } //干扰 private function disturb(){ for($i=0;$i<100;$i++){ //随机输出100个点 $color = imagecolorallocate($this->img,rand(100,200),rand(100,200),rand(100,200)); imagesetpixel($this->img,rand(1,$this->width-1),rand(1,$this->height-1),$color); } for($i=0;$i<10;$i++){ //随机输出10条线 $color = imagecolorallocate($this->img,rand(100,200),rand(100,200),rand(100,200)); imageline($this->img, rand(1,$this->width-1),rand(1,$this->height-1), rand(1,$this->width-1),rand(1,$this->height-1), $color); } } //文字 //生成验证码文字(4位) private function getCode(){ $cods = "1234567890abcdefghijklmnopqrstuvwxyz"; for($i=0;$i<$this->length;$i++){ //循环随机从codes里取字母 $this->words.= substr($cods, rand(0,strlen($cods)-1), 1); } $_SESSION['vcode']=$this->words; } //将验证码文字在图片上输出 private function printWord(){ for($i=0;$i<$this->length;$i++){ $color = imagecolorallocate($this->img,rand(0,100),rand(0,100),rand(0,100)); $font = 5; $x = ($this->width/$this->length)*$i+5; $y = rand(5,10); $code = substr($this->words,$i,1); imagestring($this->img,$font,$x,$y,$code,$color); } } //输出 private function outImg(){ header("Content-Type:image/png"); imagepng($this->img); } //释放资源 function __destruct(){ imagedestroy($this->img); } }
相关文章推荐
- PHP生成一张含有二维码的图片(文章末尾附代码下载链接)
- php使用phpqrcode类库生成带图片LOGO的二维码
- php小程序二维码获取生成图片分享
- php----图片上传封装类:单张,多张图片上传,生成缩略图
- 使用PHP生成二维码带logo图像(不生成临时图片文件)
- PHP生成一张含有二维码的图片(文章末尾附代码下载链接)
- PHP版 微信小程序生成带参数小程序二维码图片 php接口+小程序demo
- php生成二维码图片方法汇总
- php 生成图片二维码
- Laravel使用PHPQRCODE实现生成带有LOGO的二维码图片功能示例
- PHP生成包含信息的二维码图片
- phpqrcode生成带logo的二维码图片及带文字的二维码图片
- PHP修改图片颜色(生成彩色二维码)
- php使用二维码类库生成图片二维码
- php实现生成带二维码图片并强制下载功能
- PHP类库phpqrCode生成二维码图片
- php 生成图片带二维码,并且强制用户下载
- PHP修改图片颜色(生成彩色二维码)
- php使用phpqrcode类库生成带图片LOGO的二维码