php实现的验证码文件类实例
程序员文章站
2022-03-04 17:27:33
本文实例讲述了php实现的验证码文件类。分享给大家供大家参考。具体如下:
本文实例讲述了php实现的验证码文件类。分享给大家供大家参考。具体如下:
<?php /** * @file * @version 1.0 * @author 网海浪子 * @brief 验证码文件类 * */ class ccheckcodefile { //验证码位数 private $mcheckcodenum = 4; //产生的验证码 private $mcheckcode = ''; //验证码的图片 private $mcheckimage = ''; //干扰像素 private $mdisturbcolor = ''; //验证码的图片宽度 private $mcheckimagewidth = '80'; //验证码的图片宽度 private $mcheckimageheight = '20'; /** * * @brief 输出头 * */ private function outfileheader() { header ("content-type: image/png"); } /** * * @brief 产生验证码 * */ private function createcheckcode() { $this->mcheckcode = strtoupper(substr(md5(rand()),0,$this->mcheckcodenum)); return $this->mcheckcode; } /** * * @brief 产生验证码图片 * */ private function createimage() { $this->mcheckimage = @imagecreate ($this->mcheckimagewidth,$this->mcheckimageheight); imagecolorallocate ($this->mcheckimage, 200, 200, 200); return $this->mcheckimage; } /** * * @brief 设置图片的干扰像素 * */ private function setdisturbcolor() { for ($i=0;$i<=128;$i++) { $this->mdisturbcolor = imagecolorallocate ($this->mcheckimage, rand(0,255), rand(0,255), rand(0,255)); imagesetpixel($this->mcheckimage,rand(2,128),rand(2,38),$this->mdisturbcolor); } } /** * * @brief 设置验证码图片的大小 * * @param $width 宽 * * @param $height 高 * */ public function setcheckimagewh($width,$height) { if($width==''||$height=='')return false; $this->mcheckimagewidth = $width; $this->mcheckimageheight = $height; return true; } /** * * @brief 在验证码图片上逐个画上验证码 * */ private function writecheckcodetoimage() { for ($i=0;$i<=$this->mcheckcodenum;$i++) { $bg_color = imagecolorallocate ($this->mcheckimage, rand(0,255), rand(0,128), rand(0,255)); $x = floor($this->mcheckimagewidth/$this->mcheckcodenum)*$i; $y = rand(0,$this->mcheckimageheight-15); imagechar ($this->mcheckimage, 5, $x, $y, $this->mcheckcode[$i], $bg_color); } } /** * * @brief 输出验证码图片 * */ public function outcheckimage() { $this ->outfileheader(); $this ->createcheckcode(); $this ->createimage(); $this ->setdisturbcolor(); $this ->writecheckcodetoimage(); imagepng($this->mcheckimage); imagedestroy($this->mcheckimage); } } $c_check_code_image = new ccheckcodefile(); //$c_check_code_image ->setcheckimagewh(100,50); //设置显示验证码图片的尺寸 $c_check_code_image ->outcheckimage(); ?>
希望本文所述对大家的php程序设计有所帮助。
上一篇: php实现通过ftp上传文件
下一篇: 昨天下班遇见邻居新婚小嫂子