无法显示图像,因为其中包含错误

为什么此代码不起作用?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title></title>

</head>

<body>

<?php

header('Content-type: image/png');

$myImage = imagecreate(200, 100);

$myGray = imagecolorallocate($myImage, 204, 204, 204);

$myBlack = imagecolorallocate($myImage, 0, 0, 0);

imageline($myImage, 15, 35, 120, 60, $myBlack);

imagepng($myImage);

imagedestroy($myImage);

?>

</body>

</html>

我总是会出错The image cannot be displayed because it contains

errors.。我已经启用,php_gd2.dll并且memory_limit在php.ini中也是128M。如果我删除,header('Content-

type: image/png');我不会收到错误,但也不会看到图像。我所看到的是这个:-

‰PNG ��� IHDR���È���d���ùHíH���PLTEÌÌÌ���Ó33d���MIDATH‰c£ Àx�§”

Nf*k²Ã)Ãø�§”•5}À)ÅS†ÚšpJUà”a§²¦œ2ÔŽw<špJ‚Q0 †;�� uTBúŸ����IEND®B,`

回答:

您不得在之前输出任何内容 header() 。只需以<?php(作为第一个文件字符)开头,然后是用于显示图像的代码即可。跳过HTML标签。

如果要在您的html文档中显示图像,则必须在两个文件中进行显示。一种,例如,将其 命名image.php

,仅包含包含标头的PHP代码。第二个文件称为 show.phpshow.html ,包括您喜欢的HTML代码,包括<img

src="image.php" alt="Your generated image" />

以上是 无法显示图像,因为其中包含错误 的全部内容, 来源链接: utcz.com/qa/427708.html

回到顶部