• PHP基本的 验证码代码


    header('content-type:image/png');
    //创建画布并设置颜色,推荐使用下面那个方法
    //$im=imagecreate($width,$height);
    $im=imagecreatetruecolor(200,100) or die('Cannot Initialize GD image');
    
    //创建背景颜色
    $bg=imagecolorallocate($im,255,255,255);
    $line=imagecolorallocatealpha($im,mt_rand(50,255),mt_rand(50,255),mt_rand(50,255),mt_rand(20,60));
    imagefill($im,0,0,$bg);
    
    for($i=0;$i<4;$i++){
        $fontSize=mt_rand(40,80);
        $fontColor=imagecolorallocatealpha($im,rand(0,120),rand(0,120),rand(0,120),rand(20,80));
        $data="abcdefghijklmnopqrstuvwyzABCDEFGHIJKLMNOPQRSTUVWYZ0123456789";
        $fontContent=substr($data,rand(0,strlen($data)),1);
        $captcha_code.=$fontContent;
        $x=($i*100/4)+rand(10,30);
        $y=10*rand(2,6);
        imagestring($im,$fontSize,$x,$y,$fontContent,$fontColor);
    }
    
    
    //增加干扰点
    for($i=0;$i<200;$i++){
        $pixel=imagecolorallocatealpha($im,mt_rand(50,255),mt_rand(50,255),mt_rand(50,255),mt_rand(20,60));
        imagesetpixel($im,mt_rand(0,200),mt_rand(0,100),$pixel);
    }
    //增加干扰线
    for($i=0;$i<5;$i++){
        imagesetthickness($im,mt_rand(1,5));
        $line=imagecolorallocatealpha($im,rand(0,120),rand(0,120),rand(0,120),rand(40,80));
        imageline($im,mt_rand(0,200),mt_rand(0,100),mt_rand(0,200),mt_rand(0,100),$line);
    }
    
    
    imagepng($im);
    
    
    //销毁对象,便于系统资源回收
    imagedestroy($im);
    

      这是最基础的 php 验证码代码

  • 相关阅读:
    混淆代码
    滑动listview隐藏和显示顶部布局
    软件工程基础知识
    模仿QQ左滑删除
    apk签名(不打包)
    常见项目管理名词
    打包
    Banner无限轮播
    微信分享封装类
    自定义数字软键盘
  • 原文地址:https://www.cnblogs.com/fengyeyang/p/4692886.html
Copyright © 2020-2023  润新知