本文实例为大家分享了php验证码类,供大家参考,具体内容如下
width=$width; $this->height=$height; $this->codeNum=$codeNum; $this->checkCode=$this->CreateCheckCode(); $number=floor($width*$height/25); if ($number>240-$codeNum) { $this->disturbColorNum=240-$codeNum; }else{ $this->disturbColorNum=$number; } } public function showImage($fontpath='') { //创建图像背景 $this->Img_resouce(); //var_dump($img_resouce); //设置干扰元素 $this->setDistructcolor(); //向图像中随机画出文本 $this->outputtext($fontpath); //输出图像 $this->outputimage(); } /** * *获取随机创建的验证码 */ public function getCheckCode(){ } private function Img_resouce(){ //创建一个真彩图像 $this->img_resouce=imagecreatetruecolor($this->width,$this->height); //随机设置图像背景 $backcolor=imagecolorallocate($this->img_resouce,rand(225,255),rand(225,255),rand(225,255)); //填充颜色 imagefill($this->img_resouce, 0, 0, $backcolor); //设置边框背景 $border=imagecolorallocate($this->img_resouce, 0,0,0); //画一个矩形 imagerectangle($this->img_resouce,0,0,$this->width-1,$this->height-1,$border); } private function setDistructcolor(){ //绘画干扰点 for ($i = 0; $i disturbColorNum; $i++) { imagesetpixel($this->img_resouce, rand(1, $this->width-2), rand(1, $this->height-2), rand(0,255)); } //绘画干扰线 for ($j = 0; $jimg_resouce,rand(0,255),rand(0,255),rand(0,255)); imagearc($this->img_resouce, rand(0,$this->width), rand(0,$this->height), rand(10, 225), rand(20, 150), 55, 44, $linecolor); } } private function CreateCheckCode(){ $code='23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ'; $string=''; for ($i = 0; $i < $this->codeNum; $i++) { $char=$code{rand(0, strlen($code)-1)}; $string.=$char; } return $string; } private function outputtext($fontpath=''){ for ($i = 0; $i < $this->codeNum; $i++) { $fontcolor=imagecolorallocate($this->img_resouce, rand(0,128), rand(0, 128), rand(0, 128)); if ($fontpath=='') { $fontsize=rand(3, 5); $x=floor($this->width/$this->codeNum)*$i+3; $y=rand(0, $this->height-20); imagechar($this->img_resouce, $fontsize, $x, $y, $this->checkCode{$i}, $fontcolor); }else{ $fontsize=rand(12, 16); $x=floor(($this->width-8)/$this->codeNum)*$i+8; $y=rand($fontsize, $this->height-15); imagettftext($this->img_resouce,$fontsize,rand(-45,45),$x,$y,$fontcolor,fontpath,$this->checkCode{$i}); } } } private function outputimage() { if (imagetypes() & IMG_GIF) { header("Content-type: image/gif"); imagegif($this->img_resouce); }else if(imagetypes() & IMG_JPEG) { header("Content-type: image/jpeg"); imagejpeg($this->img_resouce); }else if(imagetypes() & IMG_PNG) { header("Content-type: image/png"); imagepng($this->img_resouce); }else { echo "PHP不支持的类型"; } } private function __destruct(){ imagedestroy($this->img_resouce); } } ?>

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

禅工作室 13.0.1
功能强大的PHP集成开发环境

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

Dreamweaver CS6
视觉化网页开发工具

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具