康斯氺坦丁 2017-02-14 01:46 采纳率: 0%
浏览 1181

PHP验证码图片不显示,弹出来“无法下载来自vercode.php(来自localhost)”

<? session_start();
//随机字符串种子,可以换成字母或其他英文字符
$glbVerifySeed = "123ab4567cde89ABCthixDEFHpKZ";
main();

function main() {
    session_start();

    $verifyCode = getRandomCode();
    $_SESSION["verifyCode"] = $verifyCode;
    $imgWidth = $_REQUEST["width"];
    $imgHeight = $_REQUEST["height"];
    $imgFont = $_REQUEST["font"];

    if($imgWidth == "") $imgWidth = 50;
    if($imgHeight == "") $imgHeight = 18;
    if($imgFont == "") $imgFont = 6;
    doOutputImg($verifyCode, $imgWidth, $imgHeight, $imgFont);
}

//获取随机数字字符串
function getRandomCode($length=4) {
    global $glbVerifySeed;

    $bgnIdx = 0;
    $endIdx = strlen($glbVerifySeed)-1;

    $code = "";
    for($i=0; $i<$length; $i++) {
        $curPos = rand($bgnIdx, $endIdx);
        $code .= substr($glbVerifySeed, $curPos, 1);
    }

    return $code;
}

//输出校验码图像
function doOutputImg($string, $imgWidth, $imgHeight, $imgFont,
    $imgFgColorArr=array(0,0,0), $imgBgColorArr=array(255,255,255)) {
    $image = imagecreatetruecolor($imgWidth, $imgHeight);

    //用白色背景加黑色边框画个方框
    $backColor = imagecolorallocate($image, 255, 255, 255);
    $borderColor = imagecolorallocate($image, 255, 255, 255);
    imagefilledrectangle($image, 0, 0, $imgWidth - 1, $imgHeight - 1, $backColor);
    imagerectangle($image, 0, 0, $imgWidth - 1, $imgHeight - 1, $borderColor);

    $imgFgColor = imagecolorallocate ($image, $imgFgColorArr[0], $imgFgColorArr[1], $imgFgColorArr[2]);
    doDrawStr($image, $string, $imgFgColor, $imgFont);
    doPollute($image, 64);

    header('Content-type: image/png');
    imagepng($image);
    imagedestroy($image);
}

//画出校验码
function doDrawStr($image, $string, $color, $imgFont) {
    $imgWidth = imagesx($image);
    $imgHeight = imagesy($image);

    $count = strlen($string);
    $xpace = ($imgWidth/$count);

    $x = ($xpace-6)/2;
    $y = ($imgHeight/2-8);
    for ($p = 0; $p<$count;  $p ++) {
        $xoff = rand(-2, +2);
        $yoff = rand(-2, +2);
        $curChar = substr($string, $p, 1);
        imagestring($image, $imgFont, $x+$xoff, $y+$yoff, $curChar, $color);
        $x += $xpace;
    }

    return 0;
}

//画出一些杂点
function doPollute($image, $times) {   
    $imgWidth = imagesx($image);
    $imgHeight = imagesy($image);
    for($j=0; $j<$times; $j++) {
        $x = rand(0, $imgWidth);
        $y = rand(0, $imgHeight);

        $color = imagecolorallocate($image, rand(0,255), rand(0,255), rand(0,255));
        imagesetpixel($image, $x, $y, $color);
    }
}

?>
HTML页面,验证码上面直接点刷新




  • 写回答

1条回答 默认 最新

  • baipiang 2017-02-15 13:34
    关注

    你的图片的路径是有问题

    评论

报告相同问题?

悬赏问题

  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏