How to generate verification code file in php

墨辰丷
Release: 2023-03-31 11:16:01
Original
1393 people have browsed it

This article mainly introduces the method of generating verification code files in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.

The example of this article describes the verification code file class implemented by PHP, as follows:

<?php
/**
* @file
* @version 1.0
* @author  网海浪子
* @brief   验证码文件类
*
*/
class CCheckCodeFile
{
//验证码位数
private $mCheckCodeNum = 4;
//产生的验证码
private $mCheckCode  = &#39;&#39;;
//验证码的图片
private $mCheckImage = &#39;&#39;;
//干扰像素
private $mDisturbColor = &#39;&#39;;
//验证码的图片宽度
private $mCheckImageWidth = &#39;80&#39;;
//验证码的图片宽度
private $mCheckImageHeight = &#39;20&#39;;
/**
*
* @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==&#39;&#39;||$height==&#39;&#39;)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();
?>
Copy after login

Summary: The above is the entire content of this article, I hope it can be helpful to everyone's learning help.

Related recommendations:

php uses chain calls to continuously call functions

php numbers and number operation verification codemethod

php method for recursive traversal and renaming of files

The above is the detailed content of How to generate verification code file in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template