zoukankan      html  css  js  c++  java
  • 图像处理_01_验证码

    一 数字验证码

    <?php
    session_start();//设置session,必须处于脚本最顶部
    header('Content-Type: image/png');//设置头部,image/png
    
    //1 创建一个真彩色图片
    $image = imagecreatetruecolor(100, 30);
    
    //2 填充背景色
    imagefill($image, 0, 0, imagecolorallocate($image,255,255,255));//#ffffff
    
    //3 给图片加一个边框
    imagerectangle($image,0,0,99,29,imagecolorallocate($image,rand(20,100),rand(20,100),rand(20,100)));
    
    //4 设置干扰雪花点
    for($i=0;$i<200;$i++){
        $pixelColor = imagecolorallocate($image,rand(100,200), rand(100,200), rand(100,200));//设置点的颜色
        imagesetpixel($image, rand(1,99), rand(1,29), $pixelColor);//画一个单一像素
    }
    
    //5 增加干扰线段
    for($i=0;$i<4;$i++){
        $lineColor = imagecolorallocate($image,rand(80,220), rand(80,220),rand(80,220));//设置线的颜色
        imageline($image,rand(1,99), rand(1,29),rand(1,99), rand(1,29),$lineColor);//设置线,两点一线
    }
    
    //7>生成随机数字
    $captcha_code = "";//定义变量保存数字值
    for($i=0;$i<4;$i++){
        $fontSize = 6;//设置字体大小
        $fontColor = imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120)); //设置字体颜色,0-120深颜色
        $fontContent = rand(0,9);//设置数字
        $captcha_code .= $fontContent;//.=连续定义变量
        $x = ($i*100/4)+rand(5,10);//设置x坐标
        $y = rand(5,10);//设置y坐标
        imagestring($image,$fontSize,$x,$y,$fontContent,$fontColor);
    }
    
    $_SESSION['authcode'] = $captcha_code;//8 存到session
    
    imagepng($image);//9 输出png图像
    
    imagedestroy($image);//10 结束图形函数 销毁$image

    二  字母加数字验证码

    <?php
    session_start();//设置session,必须处于脚本最顶部
    header('Content-Type: image/png');//设置头部,image/png
    
    //1 创建一个真彩色图片
    $image = imagecreatetruecolor(100, 30);
    
    //2 填充背景色
    imagefill($image, 0, 0, imagecolorallocate($image,255,255,255));//#ffffff
    
    //3 给图片加一个边框
    imagerectangle($image,0,0,99,29,imagecolorallocate($image,rand(20,100),rand(20,100),rand(20,100)));
    
    //4 设置干扰雪花点
    for($i=0;$i<200;$i++){
        $pixelColor = imagecolorallocate($image,rand(100,200), rand(100,200), rand(100,200));//设置点的颜色
        imagesetpixel($image, rand(1,99), rand(1,29), $pixelColor);//画一个单一像素
    }
    
    //5 增加干扰线段
    for($i=0;$i<4;$i++){
        $lineColor = imagecolorallocate($image,rand(80,220), rand(80,220),rand(80,220));//设置线的颜色
        imageline($image,rand(1,99), rand(1,29),rand(1,99), rand(1,29),$lineColor);//设置线,两点一线
    }
    
    //7>生成随机数字
    $captcha_code = "";//定义变量保存数字值
    for($i=0;$i<4;$i++){
        $fontSize = 6;//设置字体大小
        $fontColor = imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120)); //设置字体颜色,0-120深颜色
    
        $element = 'abcdefghigkmnpqrstuvwxy23456789';//设置验证码里面的
        $fontContent = substr($element,rand(0,strlen($element)),1);//设置数字
    
        $captcha_code .= $fontContent;//.=连续定义变量
        $x = ($i*100/4)+rand(5,10);//设置x坐标
        $y = rand(5,10);//设置y坐标
        imagestring($image,$fontSize,$x,$y,$fontContent,$fontColor);
    }
    
    $_SESSION['authcode'] = $captcha_code;//8 存到session
    
    imagepng($image);//9 输出png图像
    
    imagedestroy($image);//10 结束图形函数 销毁$image

     拼接元素

    $element=array('a','b','c','d','e','f','g','h','i','j','k','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
    $string='';
    for ($i=0;$i<5;$i++){
        $string.=$element[rand(0,count($element)-1)];
    }

    遗留问题:这个函数老是输出失败

    imagettftext($img,14,rand(-5,5),rand(5,15),rand(30,35),$colorString,'font/SketchyComic.ttf',$string);  //用 TrueType 字体向图像写入文本

  • 相关阅读:
    spark-env.sh增加HADOOP_CONF_DIR使得spark运行文件是hdfs文件
    spark wordcount程序
    hive内表和外表的创建、载入数据、区别
    select下拉框,二次定位
    Linux基本命令
    用户信息添加数据库
    MySQL查询语句练习
    mysql数据库、数据表、数据基本操作
    图像人脸检测(框出人脸、笑脸、眼睛)
    collections模块
  • 原文地址:https://www.cnblogs.com/fuyunlin/p/13945589.html
Copyright © 2011-2022 走看看