如何用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实现的随机广告显示代码
Jun 14 PHP
php 静态变量与自定义常量的使用方法
Jan 26 PHP
php启动时候提示PHP startup的解决方法
May 07 PHP
深入理解:XML与对象的序列化与反序列化
Jun 08 PHP
解析Extjs与php数据交互(增删查改)
Jun 25 PHP
浅谈PHP正则表达式中修饰符/i, /is, /s, /isU
Oct 21 PHP
thinkphp浏览历史功能实现方法
Oct 29 PHP
Zend Framework分页类用法详解
Mar 22 PHP
Yii2创建多界面主题(Theme)的方法
Oct 08 PHP
PHP实现执行外部程序的方法详解
Aug 17 PHP
thinkPHP5框架路由常用知识点汇总
Sep 15 PHP
解决Laravel5.x的php artisan migrate数据库迁移创建操作报错SQLSTATE[42000]
Apr 06 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
WordPress中自定义后台管理界面配色方案的小技巧
2015/12/29 PHP
老生常谈php中传统验证与thinkphp框架(必看篇)
2017/06/10 PHP
Laravel 前端资源配置教程
2019/10/18 PHP
javascript removeChild 使用注意事项
2009/04/11 Javascript
用dtree实现树形菜单 dtree使用说明
2011/10/17 Javascript
基于jQuery中对数组进行操作的方法
2013/04/16 Javascript
js传参数受特殊字符影响错误的解决方法
2013/10/21 Javascript
SeaJS入门教程系列之使用SeaJS(二)
2014/03/03 Javascript
div失去焦点事件实现思路
2014/04/22 Javascript
jQuery中mouseover事件用法实例
2014/12/26 Javascript
jQuery实现为图片添加镜头放大效果的方法
2015/06/25 Javascript
javascript实现类似百度分享功能的方法
2015/07/27 Javascript
JavaScript探测CSS动画是否已经完成的方法
2016/08/30 Javascript
javascript常用的设计模式
2017/02/09 Javascript
ES5学习教程之Array对象
2017/04/01 Javascript
Angular2 自定义validators的实现方法
2017/07/05 Javascript
从对象列表中获取一个对象的方法,依据关键字和值
2017/09/20 Javascript
微信小程序 循环及嵌套循环的使用总结
2017/09/26 Javascript
Vue DevTools调试工具的使用
2017/12/05 Javascript
详解VUE中常用的几种import(模块、文件)引入方式
2018/07/03 Javascript
python实现的解析crontab配置文件代码
2014/06/30 Python
python 识别图片中的文字信息方法
2018/05/10 Python
PyCharm代码格式调整方法
2018/05/23 Python
python实现简单加密解密机制
2019/03/19 Python
Python实现二叉树前序、中序、后序及层次遍历示例代码
2019/05/18 Python
Python获取时间范围内日期列表和周列表的函数
2019/08/05 Python
关于python导入模块import与常见的模块详解
2019/08/28 Python
使用python快速在局域网内搭建http传输文件服务的方法
2019/11/14 Python
python下载卫星云图合成gif的方法示例
2020/02/18 Python
前台文员的岗位职责
2013/11/14 职场文书
便利店的创业计划书
2014/01/15 职场文书
幼儿园教师教学反思
2014/02/06 职场文书
2014年公司植树节活动方案
2014/03/04 职场文书
2015年秋季开学典礼校长致辞
2015/07/16 职场文书
2016党员发展对象培训心得体会
2016/01/08 职场文书
一道JS算法面试题——冒泡、选择排序
2021/04/21 Javascript