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 字体向图像写入文本

  • 相关阅读:
    JVM 性能调优工具:jstat 使用
    JVM 性能调优工具,列表
    Mac 上 java 究竟在哪里,本文彻底让你搞清楚!
    Java 中常用锁实现的方式有两种:1. 用并发包中的锁类;2. 使用同步代码块
    Android 9.0 配置 charles 的 https 抓包
    背景,不要用:background-size: contain; 推荐用:background-size: 100% 100%;
    textarea 过滤 emoji 表情和空格(如果只 replace emoji 表情,会产生一个空格,所以再 replace 空格)
    小程序 textarea 无法隐藏的解决方案
    textarea 过滤 emoji 表情
    wx.setClipboardData:检测有复制内容再弹窗
  • 原文地址:https://www.cnblogs.com/fuyunlin/p/13945589.html
Copyright © 2011-2022 走看看