php轻型验证码生成程序

php轻型验证码生成程序
写一段轻型,非常轻型的验证码生成程序,因为不想太复杂,重要的是要调用简单,这个程序原来是网上流传的比较多的一个,后来几经改写,就成了现在这附德性,最喜欢的一点还是能随着图片的变化而均匀分布验证码字母。生成的字母也会随图片变大而变大。由于字体原因,并不能绝对均匀分布,这里推荐一款字体,是Aller_It.ttf  , 我在一个留言本里用的就是这个程序,字体可以由那里下载到。FontsAddict - Download fonts and web font icons! 

具体效果图片:

还算不是太难看。

生成的全部为大些字母,验证的时候,可能需要转换一下,因为我们通常会不限制大小写的。

生成的字符模式呢。三种:大写字母,小写字母,数字,如果有其他要求的就自己改写啦,因为本来就写的比较简单了。

可能有朋友需要系统字体,而不要外部字体,那么也自己改写了,因为一般情况下都是要用到外部字体的,系统默认字体太难看了而且永远是那么大,不能放大。

好久没来优快云发博客了,记得第一次来,还是因为当时的优快云用的编辑器是FCKeditor 加了一个代码高亮插件,非常喜欢,没想到FCKeditor变换了协议后,就换成了TinyMCE了,那个图片上传的按钮在Firefox下还有bug,不能上传图片,难道是因为我 机子是Linux的缘故?Linux下的Firefox和win下的并不总是一样的。

测试代码:

<?php

include_once 'seccode.php';    // 这一句导入seccode类文件。
$secimg = new seccode(180, 60); 
$secimg->setfont('./aller-italic.ttf'); // 这个字体路径自己设置。
$secimg->create('test-code.gif'); // 保存在本地,查看测试,web环境中,不需要带参数,直接输出到浏览器

seccode.php代码文件:

<?php
/*
 *  file: seccode.php
 *  written by AllinJS
 *  time : 2009-5-28
 *  验证码类,调用方法如下:
 *  session_start(); // 启动 session。
 *  include_once 'seccode.php'; // 这一句导入seccode类文件。
 *  $secimg=new seccode();
 *  $secimg->setfont('./aller-italic.ttf'); // 这个字体路径自己设置。
 *  $secimg->create();
 *  $_SESSION['seccode']=$secimg->getcode(); // 获取验证码的字符,存入session
 */
class seccode
{
    var $oImage;            // 图像类型对象。
    var $iWidth;            // 生成图像的宽
    var $iHeight;           // 生成图像的高
    var $iNumChars;         // 字符个数,默认为4个
    var $iSpacing;          // 字符间的间隙。
    var $sCode;             // 生成的验证码字符,可以获取到。
    var $font;              // 采用的字体。

    function setfont($font)
    {
        $this->font = $font;
    }
    function __construct($iWidth = 80, $iHeight = 30, $iNumChars = 4)
    {
        $this->iWidth = $iWidth;
        $this->iHeight = $iHeight;
        $this->iNumChars = $iNumChars;
        $this->oImage = imagecreate($iWidth, $iHeight);
        $iBgColour = 255;
        imagecolorallocate($this->oImage, $iBgColour, $iBgColour, $iBgColour);
        $this->iSpacing = (int)(($this->iWidth - 4) / $this->iNumChars);
    }

    function drawlines()
    {
        $color_white = imagecolorallocate($this->oImage, 255, 255, 255);
        $color_gray  = imagecolorallocate($this->oImage, 228, 228, 228);
        $color_black = imagecolorallocate($this->oImage, 255, 102, 204);
        imagerectangle($this->oImage, 0, 0, $this->iWidth - 1, $this->iHeight - 1, $color_gray);
        for ($i = 0; $i < 1000; $i++) {
            imagesetpixel($this->oImage, mt_rand(0, $this->iWidth), mt_rand(0, $this->iHeight), $color_gray);
        }
        for ($i = 10; $i < $this->iHeight; $i += 10) {
            imageline($this->oImage, 0, $i, $this->iWidth, $i, $color_gray);
        }
        for ($i = 10; $i < $this->iWidth; $i += 10) {
            imageline($this->oImage, $i, 0, $i, $this->iHeight, $color_gray);
        }
    }

    function gencode()
    {
        srand((float)microtime() * 1000000);
        for ($i = 0; $i < $this->iNumChars; $i++) {
            $this->sCode .= chr(rand(65, 90)); // 默认为大写字母
            //$this->sCode .= chr(rand(97, 122)); // 这个是小写字母
            //$this->sCode .= chr(rand(48, 57)); // 这个是数字
        }
    }

    function draw()
    {
        for ($i = 0; $i < strlen($this->sCode); $i++) {
            srand((float)microtime() * 1000000);
            $iRandColour_1 = rand(0, 128);
            $iRandColour_2 = rand(0, 128);
            $iRandColour_3 = rand(0, 128);
            $iTextColour = imagecolorallocate($this->oImage, $iRandColour_1, $iRandColour_2, $iRandColour_3);
            $FontHeight = $this->iHeight * 0.6;
            $y = ($this->iHeight - $FontHeight) / 2 + $FontHeight;
            $x = $i == 0 ? 2 : $i * $this->iSpacing;
            if (!$this->font) exit('no font');
            imagettftext($this->oImage, $FontHeight, 0, $x, $y, $iTextColour, $this->font, $this->sCode[$i]);
        }
    }
    function create($sFilename = '')
    {
        if (!function_exists('imagegif')) {
            return false;
        }

        $this->drawlines();
        $this->gencode();
        $this->draw();
        if ($sFilename != '') {
            imagegif($this->oImage, $sFilename);
        } else {
            header('Content-type: image/gif');
            imagegif($this->oImage);
        }
        imagedestroy($this->oImage);
        return true;
    }
    function getcode()
    {
        return $this->sCode;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值