php生成验证码类

php生成验证码类

直接看代码

<?php
session_start();
class Code{

    //资源
    private $img;
    //画布宽度
    private $width=100;
    //画布高度
    private $height=30;
    //背景颜色
    private $bgColor=‘#ffffff‘;
    //验证码
    private $code;
    //验证码的随机种子
    private $codeStr=‘23456789abcdefghjkmnpqrstuvwsyz‘;
    //验证码长度
    private $codeLen=4;
    //验证码字体
    private $font;
    //验证码字体大小
    private $fontSize=16;
    //验证码字体颜色
    private $fontColor=‘‘;

    public function __construct() {
    }

    //创建验证码
    public function make()
    {
        if(empty($this->font))
        {
            $this->font = base_path().‘\public\out\consola.ttf‘;
        }
        $this->create();//生成验证码
        header("Content-type:image/png");
        imagepng($this->img);
        imagedestroy($this->img);
        exit;
    }

    //设置字体文件
    public function font($font)
    {
        $this->font= $font;
        return $this;
    }

    //设置文字大小
    public function fontSize($fontSize)
    {
        $this->fontSize=$fontSize;
        return $this;
    }

    //设置字体颜色
    public function fontColor($fontColor)
    {
        $this->fontColor = $fontColor;
        return $this;
    }

    //验证码数量
    public function num($num)
    {
        $this->codeLen=$num;
        return $this;
    }

    //设置宽度
    public function width($width)
    {
        $this->width = $width;
        return $this;
    }

    //设置高度
    public function height($height)
    {
        $this->height = $height;
        return $this;
    }

    //设置背景颜色
    public function background($color)
    {
        $this->bgColor = $color;
        return $this;
    }

    //返回验证码
    public function get() {
        return $_SESSION[‘code‘];
    }

    //生成验证码
    private function createCode() {
        $code = ‘‘;
        for ($i = 0; $i < $this->codeLen; $i++) {
            $code .= $this->codeStr [mt_rand(0, strlen($this->codeStr) - 1)];
        }
        $this->code = strtoupper($code);
        $_SESSION[‘code‘] = $this->code;
    }

    //建画布
    private function create() {
        if (!$this->checkGD())
            return false;
        $w = $this->width;
        $h = $this->height;
        $bgColor = $this->bgColor;
        $img = imagecreatetruecolor($w, $h);
        $bgColor = imagecolorallocate($img, hexdec(substr($bgColor, 1, 2)), hexdec(substr($bgColor, 3, 2)), hexdec(substr($bgColor, 5, 2)));
        imagefill($img, 0, 0, $bgColor);
        $this->img = $img;
        $this->createLine();
        $this->createFont();
        $this->createPix();
        $this->createRec();
    }

    //画线
    private function createLine(){
        $w = $this->width;
        $h = $this->height;
        $line_color = "#dcdcdc";
        $color = imagecolorallocate($this->img, hexdec(substr($line_color, 1, 2)), hexdec(substr($line_color, 3, 2)), hexdec(substr($line_color, 5, 2)));
        $l = $h/5;
        for($i=1;$i<$l;$i++){
            $step =$i*5;
            imageline($this->img, 0, $step, $w,$step, $color);
        }
        $l= $w/10;
        for($i=1;$i<$l;$i++){
            $step =$i*10;
            imageline($this->img, $step, 0, $step,$h, $color);
        }
    }

    //画矩形边框
    private function createRec() {
        //imagerectangle($this->img, 0, 0, $this->width - 1, $this->height - 1, $this->fontColor);
    }

    //写入验证码文字
    private function createFont() {
        $this->createCode();
        $color = $this->fontColor;
        if (!empty($color)) {
            $fontColor = imagecolorallocate($this->img, hexdec(substr($color, 1, 2)), hexdec(substr($color, 3, 2)), hexdec(substr($color, 5, 2)));
        }
        $x = ($this->width - 10) / $this->codeLen;
        for ($i = 0; $i < $this->codeLen; $i++) {
            if (empty($color)) {
                $fontColor = imagecolorallocate($this->img, mt_rand(50, 155), mt_rand(50, 155), mt_rand(50, 155));
            }
            imagettftext($this->img, $this->fontSize, mt_rand(- 30, 30), $x * $i + mt_rand(6, 10), mt_rand($this->height / 1.3, $this->height - 5), $fontColor, $this->font, $this->code [$i]);
        }
        $this->fontColor = $fontColor;
    }

    //画线
    private function createPix() {
        $pix_color = $this->fontColor;
        for ($i = 0; $i < 50; $i++) {
            imagesetpixel($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), $pix_color);
        }

        for ($i = 0; $i < 2; $i++) {
            imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $pix_color);
        }
        //画圆弧
        for ($i = 0; $i < 1; $i++) {
            // 设置画线宽度
            imagearc($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height)
                , mt_rand(0, 160), mt_rand(0, 200), $pix_color);
        }
        imagesetthickness($this->img, 1);
    }

    //验证GD库
    private function checkGD() {
        return extension_loaded(‘gd‘) && function_exists("imagepng");
    }

}

原文地址:https://www.cnblogs.com/crazytata/p/10171414.html

时间: 2024-11-10 15:40:24

php生成验证码类的相关文章

php生成验证码,缩略图及水印图的类分享

封装了一个类,可生成验证码,缩略图,及水印图,分享给大家 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

生成验证码的类

<%@ WebHandler Language="C#" Class="ValidateCode" %> using System; using System.Web; using System.Drawing; using System.Web.SessionState; public class ValidateCode : IHttpHandler,IRequiresSessionState { public void ProcessRequest

Laravel 下生成验证码的类

<?php namespace App\Tool\Validate; //验证码类 class ValidateCode { private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';//随机因子 private $code;//验证码 private $codelen = 4;//验证码长度 private $width = 130;//宽度 private $height = 50;//高度 private

封装了一个类,可生成验证码,缩略图,及水印图

<?php class image{ session_start(); //验证码类 static public function verify($code,$width=75,$height=25,$n=4){ header("content-type:image/png"); // 创建画布 $img=imagecreatetruecolor($width,$height); // 设置背景色 $bgcolor=imagecolorallocate($img,mt_rand(

PHP生成缩略图、验证码类封装

1 <?php 2 /*如何知道图片的类型和大 3 * 利用getimagesize(),获得以下属性 4 Array 5 ( 6 [0] => 533 //宽 7 [1] => 300 //高 8 [2] => 2 //图片类型 jpg 9 [3] => width="533" height="300" 10 [bits] => 8 11 [channels] => 3 12 [mime] => image/jpeg

生成验证码总结

java生成验证码总结 1.serialVersionUID    private static final long serialVersionUID = -8501285780349046114L;    Java的序列化机制是通过在运行时判断类的serialVersionUID来验证版本一致性的.相当于java类的身份证.主要用于版本控制. 2.BufferedImage类    --BufferedImage 子类描述具有可访问图像数据缓冲区的 Image.    TYPE_INT_RG

javaweb学习总结(九)—— 通过Servlet生成验证码图片

一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下: 创建一个DrawImage Servlet,用来生成验证码图片 1 package gacl.response.study; 2 import java.awt.Color; 3 import java.awt.Font; 4 import java.awt.Graphics; 5 import java.awt.Graphics2D; 6 import java.awt.image.Buff

生成验证码效果

生成验证码效果    ValidateCode.java 验证码生成类 Java代码   package cn.dsna.util.images; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.FileOutputStream; import java.io.IOException; impor

C#生成验证码

生成验证码的类: using System; using System.Collections.Generic; using System.Drawing; using System.Text; namespace Controllers.Core.Util { /// <summary> /// 验证码 /// </summary> public class VerifyCodeHelper : AdminBaseController { #region 变量 /// <s