PHP碎码——自己写的验证码

其实里面没必要封装函数,只是当时觉得视觉上好看而已,结构清晰点

<?php       

class captcha{
    //验证码-字符串
    private $codes;
    //图片长度
    private $img_length = 150;
    //图片高度
    private $img_height = 30;
    //字符列表,用以生成随机验证码
    private $charlist = ‘1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM‘;
    //随机码的个数
    private $code_num = 4;
    //字体大小--初始化时算出的
    private $font_size ;
    //干扰线数目
    private $line_num = 5;
    //干扰雪花数目
    private $sterisk_num = 50;
    //验证码--图片
    private $img;
    //字体文件路径
    private $ttf = ‘./instance/font/Elephant.ttf‘;
    
    public function __construct(){   
        
        //字体大小通过图片宽高动态生成的,但感觉不太完美
        $this->font_size = ($this->img_height*2/5 > $this->img_height*4/5 ? $this->img_height*4/5 : $this->img_height*2/5);
    }
    
    public function run(){
        //创建图片资源
        $this->createImage();

        //往图片中添加雪花
        $this->addaSterisk();
        //往图片中添加字符
        $this->addfont();
        //往图片中添加线条
        $this->addLine();
        //将图片输出至浏览器
        $this->outputImg();
    }
    //返回验证码字符串
    public function getCode(){
        return $this->codes;
    }
    
    //创建图片资源
    private function createImage(){
        //创建图片资源
        $this->img = imagecreatetruecolor($this->img_length,$this->img_height);  
        //创建颜色
        $color_bg = imagecolorallocate($this->img, mt_rand(210, 255), mt_rand(210, 255), mt_rand(210, 255));
        //设置图片背景色
        imagefill($this->img, 0, 0, $color_bg);
    }
    
    //往图片中添加线条
    private function addLine(){
        //添加指定数量的线条
        for ($i = 0; $i < $this->line_num; $i++) {
            //创建随机颜色--参数(图片资源,R,B,G)
            $color_line = imagecolorallocate($this->img, mt_rand(50, 200), mt_rand(50, 200), mt_rand(50, 200));
            
            //添加线条,位置随机--参数(图片资源,起点-x,起点-y,终点-x,终点-y,颜色)
            //不可调整
            //imageline($this->img, mt_rand(0, $this->img_length), mt_rand(0, $this->img_height), mt_rand(0, $this->img_length), mt_rand(0, $this->img_height), $color_line);
            //可以调整线条的粗细
            $src_x = mt_rand(0, $this->img_length);
            $src_y = mt_rand(0, $this->img_height);
            $dest_x = mt_rand(0, $this->img_length);
            $dest_y = mt_rand(0, $this->img_height);
            for ($j = 0; $j < 1; $j++) {
                imageline($this->img, $src_x+$j, $src_y+$j, $dest_x+$j,$dest_y+$j, $color_line);
            }
        }
    }
    //往图片中添加雪花
    private function addaSterisk(){
        //添加指定数量的雪花
        for ($i = 0; $i < $this->sterisk_num; $i++) {
            //创建随机颜色--参数(图片资源,R,B,G)
            $color_Ster = imagecolorallocate($this->img, mt_rand(220, 255), mt_rand(220, 255), mt_rand(220, 255));
            //添加雪花,位置随机--参数(图片资源,倾斜角度,左下角-x,左下角-y,颜色,字符串)
            imagestring($this->img,mt_rand(0,360),mt_rand(0,$this->img_length),mt_rand(0,$this->img_height),‘*‘,$color_Ster);
        }
    }
    
    private function addfont(){
        for ($i = 0; $i < $this->code_num; $i++) {
            //随机从字符列表中取一个字符
            $code = substr(str_shuffle($this->charlist),-1);
            //记录到验证码字符串中
            $this->codes .= $code;
            //创建随机颜色--参数(图片资源,R,B,G)
            $color_font = imagecolorallocate($this->img, mt_rand(10, 180), mt_rand(10, 180), mt_rand(10, 180));
            //添加雪花,位置随机--参数(图片资源,字体大小,倾斜角度,左下角-x,左下角-y,字体颜色,字体,字符串)
            // 左下角-y,字体的基准高度是估计的,由于字体大小使用磅,不同字符的长宽像素相差甚大
            imagettftext($this->img, $this->font_size, mt_rand(-30, 30), ($this->img_length/$this->code_num)*$i+mt_rand(1,$this->font_size*0.2), $this->img_height*0.7+mt_rand(-$this->img_height*0.2, $this->img_height*0.2), $color_font, $this->ttf, $code);
        }
    }
    
    //输出图片至浏览器
    private function  outputImg(){
        //通知浏览器是png格式
        header(‘Content-type:image/png‘);
        //以png格式输出
        imagepng($this->img);
        //销毁内存中的图片资源
        imagedestroy($this->img);
    }
    
    public function __set($key,$value){
        
    }
    
    public function __get($value){
        
    }
    
    
}
时间: 2024-08-02 18:22:20

PHP碎码——自己写的验证码的相关文章

再学IHanlder 类----------------关于Asp.net与iis原理网上看博客收获写一个验证码用一般处理程序记的好长时间前就写过不过现在再看有点不一样的感觉

建一个web网站 新建一般处理程序直接贴代码: using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.SessionState;using System.Drawing;using System.Text;using System.Drawing.Imaging; namespace HandlerStudy{    /// <summary>    /

PHP碎码——ci的验证码

自己写验证码的时候在文字定位上拿捏不准 于是把ci验证码主要部分扣了出来,看看,一般般,干扰线是亮点 代码去除了很多判断,简化了很多,显示效果一样 <?php $defaults = array(     'word'        => '',     'img_path'    => './',     'img_url'    => '',     'img_width'    => '150',     'img_height'    => '30',     

android应用开发--------------看RadioGroup源码,写类似单选选项卡的集成控件(如底部导航,tab等等)

博客为 有时个哥 原创,如需转载请标明出处:http://blog.csdn.net/ls703/article/details/46694967 上面就是需求设计,4个类似的布局控件,每次只能选择一个,然后得到上面对应的钱数.(上面只是效果图,实际数据是从服务器获取,然后付到控件上) 看到这种,我们就回想到,几种实现方法. 1.把这个整体写一个布局,在xml布局中,复制粘贴,代码,凑够4个.非常不建议这样,因为4个的布局样式是一样的,只是数据可能不相同,所以我们应该写一个组合控件然后重复利用.

了解mybatis源码手写mybatis

一:mybatis概述 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Ordinary Java Object,普通的 Java对象)映射成数据库中的记录. 二:手写mybatis 实现思路:: 1创建SqlSessionFactory实例 2:实例化过程中,加载配置

PHP碎码——分页类

写了两个版本,还是后面好点,只计算关键数据,返回数组,html代码例外拼接 <?php /**  * 分页类  * @author  timo  * @version 2016-12-11 08:15  */ class paginate{          //当前页码     private $page;     //记录总条目     private $entry_total;     //每页显示多少个记录     private $page_size;     //纯数值li总数,即分

Redux-react connect 的源码从新写了一遍

import Counter from '../components/Counter'; import { increment, decrement, incrementIfOdd, incrementAsync } from '../actions'; import { bindActionCreators } from 'redux'; import React, { Component, createElement } from 'react'; import PropTypes from

献上一款漂亮的手写PHP验证码

献上一款漂亮的PHP验证码,可以根据个人需求作调整,代码如下(审美观不同,欢迎吐槽): <?php /** * Author: xiongwei * Email: [email protected] * * 注:本代码需要要用到 msyh.ttf 字体,请自行下载 **/ header("Content-type:image/png"); //图像尺寸 $width=180; $height=70; //字体样式 $font_style='./fontface/msyh.ttf'

网友写的验证码生成方案,可防止绝大多数机械识别。

web.xml Xml代码   <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <!-- ******************************************** --> <!-- ***

Python源码---Excell写

CMD安装 xlwt pip install xlwt -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com 源码———— import xlwtnew_workbook = xlwt.Workbook()worksheet = new_workbook.add_sheet('new_test')worksheet.write(0,0,'test')new_workbook.save('d:/tes