如何用php生成扭曲及旋转的验证码图片

代码如下:

<?php
function make_rand($length="32"){//验证码文字生成函数
        $str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
    $result="";
    for($i=0;$i<$length;$i++){
        $num[$i]=rand(0,61);
        $result.=$str[$num[$i]];
    }
    return $result;
}
$checkcode = make_rand(5);
$im_x=160;
$im_y=32;
function make_crand($length="5") {
    $string = '';
    for($i=0;$i<$length;$i++) {
        $string .= chr(rand(0xB0,0xF7)).chr(rand(0xA1,0xFE));
    }
    return $string;
}
function getAuthImage($text , $im_x = 230 , $im_y = 32) {
    $im = imagecreatetruecolor($im_x,$im_y);
    $text_c = ImageColorAllocate($im, mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
    $tmpC0=mt_rand(100,255);
    $tmpC1=mt_rand(100,255);
    $tmpC2=mt_rand(100,255);
    $buttum_c = ImageColorAllocate($im,$tmpC0,$tmpC1,$tmpC2);
    imagefill($im, 16, 13, $buttum_c);
    //echo $text;
    $font = 'c://WINDOWS//Fonts//simsun.ttc';
    //echo strlen($text);
    $text=iconv("gb2312","UTF-8",$text);
    //echo mb_strlen($text,"UTF-8");
    for ($i=0;$i<mb_strlen($text);$i++)
    {
            $tmp =mb_substr($text,$i,1,"UTF-8");
            $array = array(-1,0,1);
            $p = array_rand($array);
            $an = $array[$p]*mt_rand(1,9);//角度
            $size = 20;
            imagettftext($im,$size,$an,10+$i*$size*2,25,$text_c,$font,$tmp);
    }
     $distortion_im = imagecreatetruecolor ($im_x, $im_y);
     imagefill($distortion_im, 16, 13, $buttum_c);
     for ( $i=0; $i<$im_x; $i++) {
         for ( $j=0; $j<$im_y; $j++) {
             $rgb = imagecolorat($im, $i , $j);
             if( (int)($i+20+sin($j/$im_y*2*M_PI)*10) <= imagesx($distortion_im) && (int)($i+20+sin($j/$im_y*2*M_PI)*10) >=0 ) {
                 imagesetpixel ($distortion_im, (int)($i+10+sin($j/$im_y*2*M_PI-M_PI*0.5)*3) , $j , $rgb);
             }
         }
     }
     //加入干扰象素;
    $count = 600;//干扰像素的数量
    for($i=0; $i<$count; $i++){
            $randcolor = ImageColorallocate($distortion_im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
            imagesetpixel($distortion_im, mt_rand()%$im_x , mt_rand()%$im_y , $randcolor);
    }
    $line_c=5;
     //imageline
     for($i=0; $i < $line_c; $i++) {
         $linecolor = imagecolorallocate($distortion_im, 17, 158, 20);
         $lefty = mt_rand(1, $im_x-1);
         $righty = mt_rand(1, $im_y-1);
         imageline($distortion_im, 0, $lefty, imagesx($distortion_im), $righty, $linecolor);
     }
     Header("Content-type: image/PNG");
    //以PNG格式将图像输出到浏览器或文件;
    //ImagePNG($im);
    ImagePNG($distortion_im);
    //销毁一图像,释放与image关联的内存;
    ImageDestroy($distortion_im);
    ImageDestroy($im);
}
?>

用法示例如下:


代码如下:

<?php
getAuthImage(make_crand(5));
?>

(0)

相关推荐

  • php 生成随机验证码图片代码

    复制代码 代码如下: <?php /** 默认首页 **/ class DefaultController extends AppController { public function index() { $len = 5; $str = "ABCDEFGHIJKLNMPQRSTUVWXYZ123456789"; $im = imagecreatetruecolor ( 70, 20 ); $bgc = imagecolorallocate($im, 255, 255, 255

  • PHP生成Gif图片验证码

    先看效果图  字体及字体文件的路径需要在类中$FontFilePath及$FontFileName中设置.如: 复制代码 代码如下: private static $FontFilePath = "static/font/"; //相对地本代码文件的位置private static $FontFileName = array("3.ttf");// array("1.ttf", "2.ttf", "3.ttf&quo

  • php图片验证码代码

    复制代码 代码如下: <?php     //文件头...     header("Content-type: image/png");     //创建真彩色白纸     $im = @imagecreatetruecolor(50, 20) or die("建立图像失败");     //获取背景颜色     $background_color = imagecolorallocate($im, 255, 255, 255);     //填充背景颜色(这

  • 一漂亮的PHP图片验证码实例

    一.显示效果二.代码如下 复制代码 代码如下: /* *  @Author fy */ $imgwidth =100; //图片宽度$imgheight =40; //图片高度$codelen =4; //验证码长度$fontsize =20; //字体大小$charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';$font = 'Fonts/segoesc.ttf'; $im=imagecreatetruecolor($im

  • PHP图片验证码制作实现分享(全)

    就如今天遇到随即函数rand();脑海中想到用它做点啥好呢,最后想起了验证码,数字验证码,字母验证码,中文验证码,可是自己不会呀,咋办呢,上网搜,看别人的代码,开不懂,看视频,听老师讲,将其中所遇到的函数,值得注意的地方都拿笔记下,平常看到一般网页上的随机验证码都是以一定的方框包围起来,貌似就是以图片为背景的.经过边看,自己边敲,虽然遇到很多不会的问题,但是我相信只要自己脚踏实地,一定学会的.现在想做一下总结,自己可能写的很乱,可我相信有一天会实现的.1.产生数字的随机数 -->创建图片-->

  • PHP生成图片验证码、点击切换实例

    这里来看下效果: 现在让我们来看下 PHP 代码 复制代码 代码如下: <?php   session_start(); function random($len) {     $srcstr = "1a2s3d4f5g6hj8k9qwertyupzxcvbnm";     mt_srand();     $strs = "";     for ($i = 0; $i < $len; $i++) {         $strs .= $srcstr[mt

  • 《PHP编程最快明白》第七讲:php图片验证码与缩略图

    实例22 图片验证的核心代码 复制代码 代码如下: <?php //header("content-type:image/png"); $num ='1234'; $imagewidth=60; $imageheight=18; $numimage = imagecreate($imagewidth,$imageheight); imagecolorallocate($numimage,240,240,240); for($i=0;$i<strlen($num);$i++)

  • php5 图片验证码实现代码

    GD库的函数 1,imagecreatetruecolor -----创建一个真彩色的图像 imagecreatetruecolor(int x_size,int y_size) //x表示宽,y表示高 2,imagecolorallocate 为一幅图像分配颜色(调色板) imagecolorallocate(resource image,int red,int green,int blue)//red,green,blue----三原色 3,imagestring 绘图函数 iamgestr

  • PHP 绘制网站登录首页图片验证码

    几乎所有的网站登录页都会有验证码,验证码是一种安全保护机制,在注册时要求必须有人工操作进行验证,用于防止垃圾注册机大量注册用户账号占用服务器内存从而使服务器瘫痪. 图片验证码的实现十分简单.首先从指定字符集合中随机抽取固定数目的字符,以一种不规则的方法画在画布上,再适当添加一些干扰点和干扰元素,最后将图片输出,一张崭新的验证码就完成了. 先给大家展示下生成的验证码: 点击刷新: 如果大家对实现效果非常满意,请继续往下看. 前端代码如下: <!DOCTYPE html> <html>

  • php生成图片验证码-附五种验证码

    以前输出验证码的时候用过一个方法,在前台用JS生成验证码字符串,再传递到后台用PHP输出验证码图像.这样在验证时就不需要使用$_SESSION传递验证码的值,直接用JS比较生成的字符串和输入的字符串是否相等即可. 本文以实例演示5种验证码,并介绍生成验证码的函数.PHP生成验证码的原理:通过GD库,生成一张带验证码的图片,并将验证码保存在Session中. 1.HTML 5中验证码HTML代码如下: <div class="demo"> <h3>1.数字验证码&

随机推荐