zoukankan      html  css  js  c++  java
  • php制作验证码

    我们知道,验证码是一张图片,但是php本身并不支持图片的处理,需要借助于GD库。

    我们来看一下利用gd库实现验证码的制作:

    直接看代码~

    <?php
        //制作验证码

        $im=imagecreatetruecolor(200,50);

        //创建背景色

        $color=imagecolorallocate($im,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));

        //填充画布

        imagefill($im,0,0,$color);
        
        //获取验证码数据
        $str='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789';
        $captcha='';
        for($i=0;$i<5;$i++)
        {
            $captcha .=$str[mt_rand(0,strlen($str)-1)];
        }

        //设置文字颜色
        $str_color=imagecolorallocate($im,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));

        //将文字写入图片

        imagestring($im,5,80,20,$captcha,$str_color);

        //增加干扰线(要在字上层)
        for($j=0;$j<5;$j++)
        {
        $line_color=imagecolorallocate($im,mt_rand(100,200),mt_rand(100,200),mt_rand(100,200));

        //划线
        
        imageline($im,mt_rand(0,200),mt_rand(0,50),mt_rand(0,200),mt_rand(0,50),$line_color);
        }
            //画点
        for($i=0;$i<300;$i++)
            {
            $line_pixel=imagecolorallocate($im,mt_rand(100,200),mt_rand(100,200),mt_rand(100,200));

            imagesetpixel($im,mt_rand(0,200),mt_rand(0,50),$line_pixel);
            }
        header('Content-type:image/png');

        imagepng($im);

  • 相关阅读:
    eclipse经常卡死、反应慢、内存溢出的解决方案
    PAC4J 初探
    suse11离线安装nginx
    linux修改乱码的文件名
    CentOS修改服务器系统时间
    Unable to open nested entry '********.jar' 问题解决
    openssl req(生成证书请求和自建CA)
    CRT证书转JKS证书
    如何创建一个自签名的SSL证书(X509)
    Redis分布式锁的深度剖析
  • 原文地址:https://www.cnblogs.com/youxianyen/p/5967468.html
Copyright © 2011-2022 走看看