zoukankan      html  css  js  c++  java
  • GD图片(画布)的制作及验证码的应用

    创建画布:创建画布的函数有

    imagecreatetruecolor(width,height);创建画布

    width指画布的宽height指画布的高

    imagecolorallocate(img,red,green,blue);创建画布句柄

    img是一个画布资源,后面的rgb就代表一个颜色!

    imagetring(img,size,x,y,string,color);绘制文字

    img:画布资源

    size:文字的大小,这里只能是1-5,并且5最大

    xy:起始坐标

    string:文字内容

    color:颜色句柄

    imagefill(img,x,y,color);填充背景

    img:画布资源

    xy:坐标点

    color:要填充的颜色句柄

    imageline(img,x1,y1,,x2,y2,color)干扰线

    imagesetpixel(img,x1,y1,,x2,y2,color)干扰点

    ob_clean();清楚数据缓冲区

    输出图片之前设置响应头信息header("Content-type:image/png");这里的png指的是用什么函数输出就用什么函数的后缀

    imagepng,imagejpeg,imagejif输出图片的函数

    <?php
    //设置响应头信息
    header("Content-type:text/html;charset=utf-8");
    //创建一个画布资源
    $img=imagecreatetruecolor(200,50);
    //创建画布背景句柄
    $color=imagecolorallocate($img,mt_rand(100,200),mt_rand(100,200),mt_rand(200,255));
    //填充背景颜色
    imagefill($img,0,0,$color);
    $arr=array_merge(range('A','Z'),range('a','z'),range(0,20));
    //打乱该数组
    shuffle($arr);
    //利用array_rand()函数随机获取若干个该数组的下标
    $rand_key=array_rand($arr,4);
    $str='';
    foreach($rand_key as $value){
        $str.=$arr[$value];
    }
    session_start();
    $session['huabu']=$str;
    //循环遍历将文字写在画布上
    $pant=ceil(200/(4+1));
    for($i=1;$i<=4;$i++){
    $strcolor=imagecolorallocate($img,mt_rand(2,20),mt_rand(20,20),mt_rand(20,55));
    imagestring($img,5,$i*$pant,20,$str[$i-1],$strcolor);
    }
    //使用imageline函数给画布添加干扰线
    for($i=1;$i<=5;$i++){
        $linecolor=imagecolorallocate($img,mt_rand(100,200),mt_rand(100,200),mt_rand(200,255));
    imageline($img,mt_rand(0,199),mt_rand(0,49),mt_rand(0,199),mt_rand(0,49),$linecolor);
    }
    //使用imagesetpixel函数给画布添加干扰点
    for($i=1;$i<=200*100*0.01;$i++){
        $fillcolor=imagecolorallocate($img,mt_rand(0,200),mt_rand(0,200),mt_rand(0,255));
    imagesetpixel($img,mt_rand(0,199),mt_rand(0,49),$fillcolor);
    }
    //设置画布的响应头
    header("Content-type:image/png");
    //清除空格
    ob_clean();
    //输出画布
    imagepng($img);

  • 相关阅读:
    二叉树
    队列
    python3使用pdfminer3k解析pdf文件
    得到手机版新闻解析
    python连接redis并插入url
    Python使用requirements.txt安装类库
    (1366, "Incorrect string value: '\xF3\xB0\x84\xBC</...' for column 'content' at row 1")
    mysql中Incorrect string value乱码问题解决方案
    mysql命令
    requests ip代理池单ip和多ip设置方式
  • 原文地址:https://www.cnblogs.com/zgh0715/p/6143347.html
Copyright © 2011-2022 走看看