由于教程发布者很久没更新,自己改了了很多地方还是不能解决,一群新手只能期待CSDN大神帮忙了,
已将字体文件放入 ../fonts/
现在运行image.func.php一片空白image.func.php代码如下:
<?php 
require_once 'string.func.php';
//通过GD库做验证码
function verifyImage($type=1,$length=4,$pixel=0,$line=0,$sess_name = "verify"){
session_start();
//创建画布
$width = 80;
$height = 28;
$image = imagecreatetruecolor ( $width, $height );
$white = imagecolorallocate ( $image, 255, 255, 255 );
$black = imagecolorallocate ( $image, 0, 0, 0 );
//用填充矩形填充画布
imagefilledrectangle ( $image, 1, 1, $width - 2, $height - 2, $white );
$chars = buildRandomString ( $type, $length );
$_SESSION [$sess_name] = $chars;
//$fontfiles = array ("MSYH.TTF", "MSYHBD.TTF", "SIMLI.TTF", "SIMSUN.TTC", "SIMYOU.TTF", "STZHONGS.TTF" );
$fontfiles = array ("SIMYOU.TTF" );
//由于字体文件比较大,就只保留一个字体,如果有需要的同学可以自己添加字体,字体在你的电脑中的fonts文件夹里有,直接运行输入fonts就能看到相应字体
for($i = 0; $i < $length; $i ++) {
$size = mt_rand ( 14, 18 );
$angle = mt_rand ( - 15, 15 );
$x = 5 + $i * $size;
$y = mt_rand ( 20, 26 );
$fontfile = "../fonts/" . $fontfiles [mt_rand ( 0, count ( $fontfiles ) - 1 )];
$color = imagecolorallocate ( $image, mt_rand ( 50, 90 ), mt_rand ( 80, 200 ), mt_rand ( 90, 180 ) );
$text = substr ( $chars, $i, 1 );
imagettftext ( $image, $size, $angle, $x, $y, $color, $fontfile, $text );
}
if ($pixel) {
for($i = 0; $i < 50; $i ++) {
imagesetpixel ( $image, mt_rand ( 0, $width - 1 ), mt_rand ( 0, $height - 1 ), $black );
}
}
if ($line) {
for($i = 1; $i < $line; $i ++) {
$color = imagecolorallocate ( $image, mt_rand ( 50, 90 ), mt_rand ( 80, 200 ), mt_rand ( 90, 180 ) );
imageline ( $image, mt_rand ( 0, $width - 1 ), mt_rand ( 0, $height - 1 ), mt_rand ( 0, $width - 1 ), mt_rand ( 0, $height - 1 ), $color );
}
}
header ( "content-type:image/gif" );
imagegif ( $image );
imagedestroy ( $image );
}string.func.php代码如下:
<?php 
/**
 * 生成验证码
 * @param int $type
 * @param int $length
 * @return string
 */
function buildRandomString($type=1,$length=4){
if ($type == 1) {
$chars = join ( "", range ( 0, 9 ) );
} elseif ($type == 2) {
$chars = join ( "", array_merge ( range ( "a", "z" ), range ( "A", "Z" ) ) );
} elseif ($type == 3) {
$chars = join ( "", array_merge ( range ( "a", "z" ), range ( "A", "Z" ), range ( 0, 9 ) ) );
}
if ($length > strlen ( $chars )) {
exit ( "字符串长度不够" );
}
$chars = str_shuffle ( $chars );
return substr ( $chars, 0, $length );
}/**
 * 生成唯一字符串
 * @return string
 */
function getUniName(){
return md5(uniqid(microtime(true),true));
}/**
 * 得到文件的扩展名
 * @param string $filename
 * @return string
 */
function getExt($filename){
return strtolower(end(explode(".",$filename)));
}

解决方案 »

  1.   

    排除下环境 有没开启dll组件
      

  2.   

    已开启GD库。运行其他商城的代码是可以的。也是GD画验证码的。按其他朋友提示已删除
    session_start();增加
    ob_clean();均未解决
      

  3.   

    你的 image.func.php 中只是定义了一个 verifyImage 函数
    并没有调用他
      

  4.   


    getVerify.php调用
    <?php 
    require_once '../include.php';
    verifyImage();login.php相关代码如下:
    <form action="doLogin.php" method="post">
    <ul class="login">
    <li class="l_tit">管理员帐号</li>
    <li class="mb_10"><input type="text"  name="username" placeholder="请输入管理员帐号"class="login_input user_icon"></li>
    <li class="l_tit">密码</li>
    <li class="mb_10"><input type="password"  name="password" class="login_input password_icon"></li>
    <li class="l_tit">验证码</li>
    <li class="mb_10"><input type="text"  name="verify" class="login_input password_icon"></li>
    <img src="getVerify.php" alt="" />
    <li class="autoLogin"><input type="checkbox" id="a1" class="checked" name="autoFlag" value="1"><label for="a1">自动登陆(一周内自动登陆)</label></li>
    <li><input type="submit" value="" class="login_btn"></li>
    </ul>
    </form>
      

  5.   

    include.php 中又是什么?
      

  6.   


    你好,非常感谢您的回复,include.php中应该影响不大include.php代码如下,还有个相关的string.func.php一并列出。
    <?php 
    header("content-type:text/html;charset=utf-8");
    date_default_timezone_set("PRC");
    session_start();
    define("ROOT",dirname(__FILE__));
    set_include_path(".".PATH_SEPARATOR.ROOT."/lib".PATH_SEPARATOR.ROOT."/core".PATH_SEPARATOR.ROOT."/configs".PATH_SEPARATOR.get_include_path());
    require_once 'mysql.func.php';
    require_once 'image.func.php';
    require_once 'common.func.php';
    require_once 'string.func.php';
    require_once 'page.func.php';
    require_once "configs.php";
    require_once 'admin.inc.php';
    require_once 'cate.inc.php';
    require_once 'pro.inc.php';
    require_once 'album.inc.php';
    require_once 'upload.func.php';
    require_once 'user.inc.php';
    connect();string.func.php代码如下:
    <?php 
    /**
     * 生成验证码
     * @param int $type
     * @param int $length
     * @return string
     */
    function buildRandomString($type=1,$length=4){
    if ($type == 1) {
    $chars = join ( "", range ( 0, 9 ) );
    } elseif ($type == 2) {
    $chars = join ( "", array_merge ( range ( "a", "z" ), range ( "A", "Z" ) ) );
    } elseif ($type == 3) {
    $chars = join ( "", array_merge ( range ( "a", "z" ), range ( "A", "Z" ), range ( 0, 9 ) ) );
    }
    if ($length > strlen ( $chars )) {
    exit ( "字符串长度不够" );
    }
    $chars = str_shuffle ( $chars );
    return substr ( $chars, 0, $length );
    }/**
     * 生成唯一字符串
     * @return string
     */
    function getUniName(){
    return md5(uniqid(microtime(true),true));
    }/**
     * 得到文件的扩展名
     * @param string $filename
     * @return string
     */
    function getExt($filename){
    return strtolower(end(explode(".",$filename)));
    }
      

  7.   

    include 'image.func.php';
    verifyImage();可知代码并无问题
    问题就在于你的调用上了
      

  8.   


    版主你好,问题是我用WAMP,MAMP,以及上传到网络都无法正常运行此代码。但是以下代码却能正常使用,由此可见搭建环境应该并无问题啊。<?php 
          session_start();      $image=imagecreatetruecolor(100, 30);
          $bgcolor=imagecolorallocate($image, 255, 255, 255);
          imagefill($image, 0, 0, $bgcolor);
          
          /*for($i=0;$i<4;$i++)//写数字
          {
           $fontsize=6;//字体大小
           $fontcolor=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));//字体颜色
            $fontcontent=rand(0,9);        $x=($i*100/4)+rand(5,10);
            $y=rand(5,10);       imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);      } */
          
          $captch_code='';
          for($i=0;$i<4;$i++)//写字母数字混合体
          {
           $fontsize=6;//字体大小
           $fontcolor=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));//字体颜色
            
            $data='abcdefghijkmnpqrstuvwxy0123456789';
            $fontcontent=substr($data, rand(0,strlen($data)),1);
            $captch_code.=$fontcontent;        $x=($i*25)+rand(5,10);
            $y=rand(5,10);        imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);      } 
          $_SESSION['authcode']=$captch_code;      for ($i=0; $i < 200; $i++) //干扰点
          { 
           $pointcolor=imagecolorallocate($image, rand(50,200), rand(50,200), rand(50,200));
           imagesetpixel($image, rand(1,99), rand(1,29), $pointcolor);
          }
          
           for ($i=0; $i < 3; $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);
          }      header('content-type: image/png');
          imagepng($image);      imagedestroy($image);
          
    ?>
      

  9.   

    你有这个教程github的地址吗?我的资料都不知道什么时候的了,新手对着一堆运行不了的代码心好累
      

  10.   

    你有这个教程github的地址吗?我的资料都不知道什么时候的了,新手对着一堆运行不了的代码心好累