支持背景为调色板和图片文件的验证码类
<? /** * 验证码类 * @author firerat * @email lukai_rat@163.com * @time Feb 16 2011 15:28 * @lastmodify Feb 16 2011 15:28 */ class verifycode{ //image source private $im; //图片宽度 private $w; //图片高度 private $h; //验证字符集合 private $text; //字符数 private $length; //字体 private $font = array(); //字体大小 private $fontsize = array(); //字体颜色 private $fontcolor = array(); //字体的偏转角度 private $angel = array(); //背景色 private $backgroundcolor = array(); //背景图片 private $backgroundimage = array(); //坐标-x private $x ; //坐标-y private $y ; //验证码存放的字段 private $checkcodefield ; /** * 构造函数 * */ public function __construct(){ //图片宽度 $this->w = 500 ; //图片高度 $this->h = 600; //验证字符 $this->text = '0123456789qwertyuiopasdfghjklzxcvbnm'; //字体 $this->font = array( 'C:\\WINDOWS\\Fonts\\Ming Imperial.TTF', 'C:\\WINDOWS\\Fonts\\latha.TTF', 'C:\\WINDOWS\\Fonts\\ARIALNBI.TTF', 'C:\\WINDOWS\\Fonts\\GOTHICBI.TTF' ); //字符数 $this->length = '4'; //字体大小 $this->fontsize = array(50,60,70); //字体颜色 $this->fontcolor = array( array('red'=>0x14, 'green'=>0x37,'blue'=>0xad), array('red'=>0x6e, 'green'=>0x86,'blue'=>0xd6), array('red'=>0x2c, 'green'=>0x40 ,'blue'=>0x81), array('red'=>0x06, 'green'=>0x1f ,'blue'=>0x70), array('red'=>0x14, 'green'=>0x37 ,'blue'=>0xad), array('red'=>0x57, 'green'=>0x79 ,'blue'=>0xc0) ); //字体的偏转角度 $this->angel = array(2,4,8,-2,-4,-8); //背景色 $this->backgroundcolor = array( array('red'=>0x14, 'green'=>0x37,'blue'=>0xad), array('red'=>0x6e, 'green'=>0x86,'blue'=>0xd6), array('red'=>0x2c, 'green'=>0x40 ,'blue'=>0x81), array('red'=>0x06, 'green'=>0x1f ,'blue'=>0x70), array('red'=>0x14, 'green'=>0x37 ,'blue'=>0xad), array('red'=>0x57, 'green'=>0x79 ,'blue'=>0xc0) ); //背景图片 $this->backgroundimage = array( 'F:\\city_photo\\city_photo\\1\\1807141.jpg', 'F:\\city_photo\\city_photo\\1\\1807141.jpg', 'F:\\city_photo\\city_photo\\1\\1807141.jpg', 'F:\\city_photo\\city_photo\\1\\1807141.jpg', 'F:\\city_photo\\city_photo\\1\\1807141.jpg' ); //坐标轴X $this->x = 100 ; //坐标轴Y $this->y = 300 ; //验证码存放的字段 $this->checkcodefield = 'checkcode'; } /* 创建背景 * * @return $im */ public function createImgae(){ //$this->im = imagecreatetruecolor($this->w,$this->h); $key = array_rand($this->backgroundimage); $this->im = imagecreatefromjpeg( $this->backgroundimage[$key]); return $this->im; } /* 创建背景的索引色 * * @return */ public function createBackgroundColor(){ //获取背景随机颜色组key $rgbGroupKey = array_rand($this->backgroundcolor); $red = $this->backgroundcolor[$rgbGroupKey]['red']; $green = $this->backgroundcolor[$rgbGroupKey]['green']; $blue = $this->backgroundcolor[$rgbGroupKey]['blue']; //返回颜色索引 return imagecolorallocate($this->im, $red, $green, $blue); } /* 获取随机字符,并将字符存放发到 * * @return */ public function getRandStr(){ $randChars = ''; for($i = 0 ; $i < $this->length ; $i++){ $randChars .= $this->text[rand(0,strlen($this->text))] ; } //字体编码统一转换为utf-8 //$randChars = iconv("GB2312","UTF-8",$randChars); //写进session $_SESSION[$this->checkcodefield] = $randChars; return $randChars; } /* 创建字体颜色索引 * @return */ public function createFontColor(){ //获取背景随机颜色组key $rgbGroupKey = array_rand($this->fontcolor); $red = $this->fontcolor[$rgbGroupKey]['red']; $green = $this->fontcolor[$rgbGroupKey]['green']; $blue = $this->fontcolor[$rgbGroupKey]['blue']; //颜色索引 return imagecolorallocate($this->im, $red, $green, $blue);; } //添加文字到图片 public function addTextToImage(){ //字体颜色 $fontcolor =$this->createFontColor(); //字体 $key = array_rand($this->font); $font = $this->font[$key]; //验证码 $text = $this->getRandStr(); //偏转角度 $key = array_rand($this->angel); $_angle = $this->angel[$key]; //起始坐标 $x = $this->x; $y = $this->y; //字体大小 $key = array_rand($this->fontsize); $_fontsize = $this->fontsize[$key]; //添加文字到图片 imagettftext($this->im, $_fontsize , $_angle, $x, $y, $fontcolor, $font, $text); } /** * 输出图片 * */ public function outputImage(){ //创建布景 $this->createImgae(); //颜色添加到布景 $this->createBackgroundColor(); //添加文字到图片 $this->addTextToImage(); //增加过期,30分钟后过期 $expireTime = date("M, d Y H:i:s",time()+1800); header("Expires: {$expireTime} GMT "); header("Last-Modified: " . gmdate("D, d M Y H:i:s")." GMT"); header("Cache-Control: no-store, no-cache, must-revalidate"); header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); //声明输出文件的类型 header('Content-type: image/png'); //输出png图片 imagepng($this->im); //释放与此图片关联的内存 imagedestroy($this->im); } } $instance = new verifycode; $instance->outputImage();
3. [图片] examplle2.jpg
以上就是支持背景为调色板和图片文件的验证码类的内容,更多相关内容请关注PHP中文网(www.php.cn)!
성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

Video Face Swap
완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

인기 기사
Windows 11 KB5054979의 새로운 기능 및 업데이트 문제를 해결하는 방법
3 몇 주 전ByDDD
KB5055523을 수정하는 방법 Windows 11에 설치되지 않습니까?
2 몇 주 전ByDDD
Inzoi : 학교 및 대학에 지원하는 방법
3 몇 주 전ByDDD
KB5055518을 수정하는 방법 Windows 10에 설치되지 않습니까?
2 몇 주 전ByDDD
Roblox : Dead Rails - Nikola Tesla를 소환하고 패배하는 방법
4 몇 주 전By尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

SublimeText3 Linux 새 버전
SublimeText3 Linux 최신 버전

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

VSCode Windows 64비트 다운로드
Microsoft에서 출시한 강력한 무료 IDE 편집기

안전한 시험 브라우저
안전한 시험 브라우저는 온라인 시험을 안전하게 치르기 위한 보안 브라우저 환경입니다. 이 소프트웨어는 모든 컴퓨터를 안전한 워크스테이션으로 바꿔줍니다. 이는 모든 유틸리티에 대한 액세스를 제어하고 학생들이 승인되지 않은 리소스를 사용하는 것을 방지합니다.

PhpStorm 맥 버전
최신(2018.2.1) 전문 PHP 통합 개발 도구
