这是封装的验证码代码:
<?php 
namespace third;
use \think\View;
use \think\Model;
use think\DB;
use think\Controller;
use think\File;
use think\Lang;
use think\Request;
use think\Session;class ma{
    // session_start();
    // $w="100";   //画布高
    // $h="39";   //画布高
    // $num="5"; //显示数字个数
    //1、算术验证码
    function code_math($w,$h){
        $im = imagecreate($w,$h);
        $red = imagecolorallocate($im, 255, 0, 0);
        $white = imagecolorallocate($im, 255, 255, 255);        $num1 = rand(1, 20);
        $num2 = rand(1, 20);        $_SESSION['helloweba_math'] = $num1 + $num2;        $gray = imagecolorallocate($im, 118, 151, 199);
        $black = imagecolorallocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100));        //画背景
        imagefilledrectangle($im, 0, 0, 100, 30, $black);
        //在画布上随机生成大量点,起干扰作用;
        for ($i = 0; $i < 80; $i++) {
            imagesetpixel($im, rand(0, $w), rand(10, $h), $gray);
        }        imagestring($im, 5, 5, 4, $num1, $red);
        imagestring($im, 5, 30, 3, "+", $red);
        imagestring($im, 5, 45, 4, $num2, $red);
        imagestring($im, 5, 70, 3, "=", $red);
        imagestring($im, 5, 80, 2, "?", $white);        header("Content-type: image/png");
        imagepng($im);
        imagedestroy($im);
        return $im;
    }
    //2、谷歌验证码
    // code_gg($w,$h);
    function code_gg($w,$h){
        $image=imagecreatetruecolor($w,$h);
        $arr=array_merge(range('A','Z'));
        shuffle($arr);
        $str=substr(join($arr),0,4);
        $_SESSION['code']=$str;        for ($i=0; $i < 500; $i++) { 
            # code...
            $color=imagecolorallocate($image, rand(0,255), rand(0,255), rand(0,255));
            imagesetpixel($image, rand(0,100), rand(0,60), $color);
            // imageline($image, rand(0,100), rand(0,60), rand(0,100), rand(0,60), $color);
        }
        for ($j=0; $j < 4; $j++) { 
            # code...
            $color1=imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255));
            imagettftext($image, 20, 0, 10+$j*20, 30, $color1, 'msyh.ttf', $str{$j});
        }
        header('content-type:image/jpeg');
        imagejpeg($image);
        // imagedestroy($image);
        return imagepng($im);
    } ?>下面是页面调用的代码:<div class="code">
              <?php
                 $ma=new \third\yzm;
                 echo $aa=$ma->code_math(100,35);
              ?>
</div>

解决方案 »

  1.   

    你生成的图片数据流
    需要用 img 标记承载
    <img src='验证码生成程序.php‘>
      

  2.   

    用img也不能显示,图片的路径也是乱码
      

  3.   

    你要把你的代码保存为一个php,例如captcha.php
                  <?php
                     $ma=new \third\yzm;
                     echo $aa=$ma->code_math(100,35);
                  ?>
    然后直接在浏览器访问,如果可以在html中,使用<img src="http://www.abc.com/captcha.php"> 来显示