如何用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 相关文章推荐
基于mysql的论坛(4)
Oct 09 PHP
用PHP+MySql编写聊天室
Oct 09 PHP
对PHP语言认识上需要避免的10大误区
Jun 12 PHP
PHP获取文件相对路径的方法
Feb 26 PHP
WordPress中用于获取及自定义头像图片的PHP脚本详解
Dec 17 PHP
在WordPress中使用wp_count_posts函数来统计文章数量
Jan 05 PHP
thinkphp表单上传文件并将文件路径保存到数据库中
Jul 28 PHP
php 使用fopen函数创建、打开文件详解及实例代码
Sep 24 PHP
php array_merge_recursive 数组合并
Oct 26 PHP
php使用preg_match()函数验证ip地址的方法
Jan 07 PHP
PHP如何搭建百度Ueditor富文本编辑器
Sep 21 PHP
thinkphp5框架实现的自定义扩展类操作示例
May 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
zf框架的registry(注册表)使用示例
2014/03/13 PHP
PHP5.3以上版本安装ZendOptimizer扩展
2015/03/27 PHP
php需登录的文件上传管理系统
2020/03/21 PHP
修改WordPress中文章编辑器的样式的方法详解
2015/12/15 PHP
Yii净化器CHtmlPurifier用法示例(过滤不良代码)
2016/07/15 PHP
PHP receiveMail实现收邮件功能
2018/04/25 PHP
json对象转字符串如何实现
2012/12/02 Javascript
自定义jQuery选项卡插件实例
2013/03/27 Javascript
用示例说明filter()与find()的用法以及children()与find()的区别分析
2013/04/26 Javascript
js实现九宫格图片半透明渐显特效的方法
2015/02/16 Javascript
javascript瀑布流式图片懒加载实例
2020/06/28 Javascript
nodejs 实现钉钉ISV接入的加密解密方法
2017/01/16 NodeJs
js获取浏览器的各种属性
2017/04/27 Javascript
Angularjs添加排序查询功能的实例代码
2017/10/24 Javascript
jquery学习笔记之无new构建详解
2017/12/07 jQuery
vue进入页面时滚动条始终在底部代码实例
2019/03/26 Javascript
JavaScript命名空间模式实例详解
2019/06/20 Javascript
关于vue路由缓存清除在main.js中的设置
2019/11/06 Javascript
通过实例了解Render Props回调地狱解决方案
2020/11/04 Javascript
apache部署python程序出现503错误的解决方法
2017/07/24 Python
Python构建网页爬虫原理分析
2017/12/19 Python
Python面向对象之静态属性、类方法与静态方法分析
2018/08/24 Python
用Python逐行分析文件方法
2019/01/28 Python
在cmd中查看python的安装路径方法
2019/07/03 Python
基于python实现的百度新歌榜、热歌榜下载器(附代码)
2019/08/05 Python
获取Pytorch中间某一层权重或者特征的例子
2019/08/17 Python
POP文化和音乐灵感的时尚:Hot Topic
2019/06/19 全球购物
如何利用find命令查找文件
2015/02/07 面试题
生物化工工艺专业应届生求职信
2013/10/08 职场文书
孝敬父母的活动方案
2014/08/28 职场文书
大三学生英语考试作弊检讨书
2015/01/01 职场文书
大学生自荐信范文
2015/03/05 职场文书
毕业论文答辩开场白和答辩技巧
2015/05/27 职场文书
学生会主席任命书
2015/09/21 职场文书
用python自动生成日历
2021/04/24 Python
Rust中的Struct使用示例详解
2022/08/14 Javascript