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


Posted in PHP onJune 07, 2013
<?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)); 
?>

PHP 相关文章推荐
php mssql 日期出现中文字符的解决方法
Mar 10 PHP
Windows Apache2.2.11及Php5.2.9-1的安装与配置方法
Jun 08 PHP
PHP输入流php://input介绍
Sep 18 PHP
php中检查文件或目录是否存在的代码小结
Oct 22 PHP
php强制文件下载而非在浏览器打开的自定义函数分享
May 08 PHP
PHP curl实现抓取302跳转后页面的示例
Jul 04 PHP
php程序内部post数据的方法
Mar 31 PHP
PHP实现简单实用的验证码类
Jul 29 PHP
各种快递查询--Api接口
Apr 26 PHP
PHP封装的MSSql操作类完整实例
May 26 PHP
PHP程序员必须知道的两种日志实例分析
May 14 PHP
ThinkPHP6.0如何利用自定义验证规则规范的实现登陆
Dec 16 PHP
利用php获取服务器时间的实现代码
Jun 07 #PHP
探讨PHP中OO之静态关键字以及类常量的详解
Jun 07 #PHP
PHP5常用函数列表(分享)
Jun 07 #PHP
深入理解php的MySQL连接类
Jun 07 #PHP
PHP之生成GIF动画的实现方法
Jun 07 #PHP
深入HTTP响应状态码速查表的详解
Jun 07 #PHP
探讨如何把session存入数据库
Jun 07 #PHP
You might like
php上传图片到指定位置路径保存到数据库的具体实现
2013/12/30 PHP
CentOS下与Apache连接的PHP多版本共存方案实现详解
2015/12/19 PHP
php文件系统处理方法小结
2016/05/23 PHP
Extjs学习笔记之六 面版
2010/01/08 Javascript
JavaScript 通过模式匹配实现重载
2010/08/12 Javascript
原生js 秒表实现代码
2012/07/24 Javascript
JavaScript高级程序设计 阅读笔记(十四) js继承机制的实现
2012/08/14 Javascript
Extjs4 消息框去掉关闭按钮(类似Ext.Msg.alert)
2013/04/02 Javascript
jquery访问ashx文件示例代码
2014/08/11 Javascript
Javascript 赋值机制详解
2014/11/23 Javascript
jQuery中[attribute^=value]选择器用法实例
2014/12/31 Javascript
JS实现在线统计一个页面内鼠标点击次数的方法
2015/02/28 Javascript
jQuery实现图片文字淡入淡出效果
2015/12/21 Javascript
深入理解js generator数据类型
2016/08/16 Javascript
JS 拦截全局ajax请求实例解析
2016/11/29 Javascript
图解Javascript——作用域、作用域链、闭包
2017/03/21 Javascript
利用jquery正则表达式在页面验证url网址输入是否正确
2017/04/04 jQuery
详解如何在你的Vue项目配置vux
2018/06/04 Javascript
js验证密码强度解析
2020/03/18 Javascript
js简单实现自动生成表格功能示例
2020/06/02 Javascript
vue-cli3 热更新配置操作
2020/09/18 Javascript
vue 验证两次输入的密码是否一致的方法示例
2020/09/29 Javascript
Python实现的多叉树寻找最短路径算法示例
2018/07/30 Python
Python文件读写常见用法总结
2019/02/22 Python
python爬虫中多线程的使用详解
2019/09/23 Python
python logging设置level失败的解决方法
2020/02/19 Python
使用CSS3制作一个简单的进度条(demo)
2017/05/23 HTML / CSS
英国知名的护肤彩妆与时尚配饰大型综合零售电商:Unineed
2016/11/21 全球购物
小学生元旦广播稿
2014/02/21 职场文书
共产党员承诺书
2014/03/25 职场文书
个人简历求职信范文
2015/03/20 职场文书
物流业务员岗位职责
2015/04/03 职场文书
单位考核鉴定意见
2015/06/05 职场文书
导游词之广州陈家祠
2019/10/21 职场文书
python字典进行运算原理及实例分享
2021/08/02 Python
Win10系统搭建ftp文件服务器详细教程
2022/08/05 Servers