图片无法显示,因为其中包含错误php [英] the image cannot be displayed because it contains errors php

查看:121
本文介绍了图片无法显示,因为其中包含错误php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
可以将html与动态生成的图像一起使用吗? php?

Possible Duplicate:
Does html can be use with dynamic generated images in php?

我正在尝试在php中生成验证码.我相信我的代码正确,但是我无法在浏览器上获取图像.这是代码:

I'm trying to generate captcha in php. I believe i've the code right, but i'm not able to get the image on the browser..this is the code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php header('Content-type: image/png');?>
<?php session_start();
$md5 = md5(microtime() * time() );
$string = substr($md5, -5);
$captcha = imagecreatefrompng("./captcha.png");
$black = imagecolorallocate($captcha, 0, 0, 0);
$line = imagecolorallocate($captcha,233,239,239);
imageline($captcha,0,0,39,29,$line);
imageline($captcha,40,0,64,29,$line);
$_SESSION['key'] = md5($string);
imagestring($captcha, 5, 20, 10, $string, $black);
imagepng($captcha);?> 
</body>
</html>

png图像与此代码位于同一文件夹中.在php中启用了GD选项.

the png image is on the same folder as this code. the GD option is enabled in php..I a clueless..any help is appreciated...thank you

推荐答案

我猜这是您的验证码图片的来源(例如"image.php"文件),并且是从其他地方(例如从验证码")加载的.php"和<img src="image.php" />).您可能也必须在session_start().

I guess this is the source of your CAPTCHA image (e.g. a "image.php" file), and it is loaded from somewhere else (e.g. from "captcha.php" with <img src="image.php" />). You may have to include session_start() in the "captcha.php" file too.

从您发布的代码中,删除所有HTML.

From the code you posted, just remove all HTML.

此外,通常在准备就绪之前,请勿发送图片内容类型(并在合适的情况下先检查错误).

Also, as a rule, never send an Image content-type until you're ready (and check for errors first, if suitable).

<?php
    session_start();
    $md5 = md5(microtime() * time() );
    $string = substr($md5, -5);
    $captcha = imagecreatefrompng("./captcha.png");
    $black = imagecolorallocate($captcha, 0, 0, 0);
    $line = imagecolorallocate($captcha,233,239,239);
    imageline($captcha,0,0,39,29,$line);
    imageline($captcha,40,0,64,29,$line);
    $_SESSION['key'] = md5($string);
    imagestring($captcha, 5, 20, 10, $string, $black);
    Header('Content-type: image/png');
    imagepng($captcha);
?>

注意:您正在运行MD5字符串的MD5.那是对的吗?为什么不使用uniqid()而不是md5(microtime() * time() )并将$md5存储在_SESSION中?

NOTE: you are running the MD5 of a MD5 string. Is that correct? Why not use uniqid() instead of md5(microtime() * time() ), and store $md5 in the_SESSION?

这篇关于图片无法显示,因为其中包含错误php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆