php 生产验证码
来源:互联网 发布:mac拼音切换 编辑:程序博客网 时间:2024/04/29 08:46
<?php/** * 生成验证码 * @author chenzhouyu * 类用法 * $checkcode = new checkcode(); * $checkcode->doimage(); * //取得验证 * $_SESSION['code']=$checkcode->get_code(); */class checkcode {//验证码的宽度public $width=130;//验证码的高public $height=50;//设置字体的地址private $font;//设置字体色public $font_color;//设置随机生成因子public $charset = 'abcdefghkmnprstuvwyzABCDEFGHKLMNPRSTUVWYZ23456789';//设置背景色public $background = '#EDF7FF';//生成验证码字符数public $code_len = 5;//字体大小public $font_size = 20;//验证码private $code;//图片内存private $img;//文字X轴开始的地方private $x_start;function __construct() {$rand = rand(0,1);if($rand==0) {$this->font = PC_PATH.'libs'.DIRECTORY_SEPARATOR.'data'.DIRECTORY_SEPARATOR.'font'.DIRECTORY_SEPARATOR.'elephant.ttf';} else {$this->font = PC_PATH.'libs'.DIRECTORY_SEPARATOR.'data'.DIRECTORY_SEPARATOR.'font'.DIRECTORY_SEPARATOR.'Vineta.ttf';}}/** * 生成随机验证码。 */protected function creat_code() {$code = '';$charset_len = strlen($this->charset)-1;for ($i=0; $i<$this->code_len; $i++) {$code .= $this->charset[rand(1, $charset_len)];}$this->code = $code; return $code;}/** * 获取验证码 */public function get_code() {return strtolower($this->code);}/** * 生成图片 */public function doimage() {$code = $this->creat_code();$this->img = imagecreatetruecolor($this->width, $this->height);if (!$this->font_color) {$this->font_color = imagecolorallocate($this->img, rand(0,156), rand(0,156), rand(0,156));} else {$this->font_color = imagecolorallocate($this->img, hexdec(substr($this->font_color, 1,2)), hexdec(substr($this->font_color, 3,2)), hexdec(substr($this->font_color, 5,2)));}//设置背景色$background = imagecolorallocate($this->img,hexdec(substr($this->background, 1,2)),hexdec(substr($this->background, 3,2)),hexdec(substr($this->background, 5,2)));//画一个柜形,设置背景颜色。imagefilledrectangle($this->img,0, $this->height, $this->width, 0, $background);$this->creat_font();$this->creat_line();$this->output();}/** * 生成文字 */private function creat_font() {$x = $this->width/$this->code_len;for ($i=0; $i<$this->code_len; $i++) {imagettftext($this->img, $this->font_size, rand(-30,30), $x*$i+rand(0,5), $this->height/1.4, $this->font_color, $this->font, $this->code[$i]);if($i==0)$this->x_start=$x*$i+5;}}/** * 画线 */private function creat_line() {imagesetthickness($this->img, 3); $xpos = ($this->font_size * 2) + rand(-5, 5); $width = $this->width / 2.66 + rand(3, 10); $height = $this->font_size * 2.14; if ( rand(0,100) % 2 == 0 ) { $start = rand(0,66); $ypos = $this->height / 2 - rand(10, 30); $xpos += rand(5, 15); } else { $start = rand(180, 246); $ypos = $this->height / 2 + rand(10, 30); } $end = $start + rand(75, 110); imagearc($this->img, $xpos, $ypos, $width, $height, $start, $end, $this->font_color); if ( rand(1,75) % 2 == 0 ) { $start = rand(45, 111); $ypos = $this->height / 2 - rand(10, 30); $xpos += rand(5, 15); } else { $start = rand(200, 250); $ypos = $this->height / 2 + rand(10, 30); } $end = $start + rand(75, 100); imagearc($this->img, $this->width * .75, $ypos, $width, $height, $start, $end, $this->font_color);}/** * 输出图片 */private function output() {header("content-type:image/png\r\n");imagepng($this->img);imagedestroy($this->img);}}在前台显示结果为:
0 0
- php 生产验证码
- 如何利用PHP生产验证码的图片?
- 验证码生产程序
- 生产验证码代码
- 生产验证码
- Java生产图片验证码
- java 生产图片验证码
- 利用服务器response生产随机验证码
- asp.net中用C#生产验证码
- servlet生产唯一图片验证码
- XBM验证码用ASP如何生产
- JSP彩色验证码的生产
- 最精简验证码生产代码
- 数字字母组合生产的图片验证码方法
- 数字字母组合生产的图片验证码方法
- JAVA技术实现验证码图片的生产
- 如何在Discuz! X2生产验证码的
- 使用基本servlet技术生产图片验证码
- 渐进式迁移 — 保证稳定性的一种方法
- js中判断浏览器的类别
- vetor和数组的区别 vector和List的区别
- arm定时器4
- 如何快速构建基于Spring4.0的Rest API(攻略)
- php 生产验证码
- 数组实现的队列(限制最大长度)的head、tail指针的修改方法。
- 余佳文发微博致歉:炒作可耻
- WebViewJavascriptBridge源码分析
- Find them, Catch them
- HDU 1598 find the most comfortable road
- C++基础---指针与堆
- [Linux]curl输出原始数据,不对网页解压
- CodeForces 500D - New Year Santa Network(DFS)