php 图像无法显示,因为它包含错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3385982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:33:44  来源:igfitidea点击:

The image cannot be displayed because it contains errors

php

提问by TCM

Why is this code not working ?

为什么这段代码不起作用?

<!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>

I always get error The image cannot be displayed because it contains errors.. I've already enabled php_gd2.dlland memory_limitin php.ini is also 128M. If i remove header('Content-type: image/png');i don't get the error but i don't see the image either. All i see is this :-

我总是得到错误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*k2?)???§”?5}à)?S?ú?pJUà”a§2|?2??w<?pJ?Q0 ?;?? uTBú?????IEND?B? `

‰PNG ??? IHDR???è???d???ùHíH???PLTEììì???ó33d???MIDATH‰càx?§” Nf*k2?)???§”?5}à)?S?ú?pJUà”a§2|?2??w<?pJ?Q0 ?;?? uTBú?????IEND?B? `

回答by Johan

You must not output anything before header(). Just start your document with <?php(as the first file characters), followed by the code for displaying the image. Skip the HTML tags. Do not even write a single blankline before header().

之前不能输出任何东西header()。只需以<?php(作为第一个文件字符)开始您的文档,然后是显示图像的代码。跳过 HTML 标签。甚至不要在header()之前写一个空白行

If you want to display an image inside the html document of yours, you must do it in two files. One, call it for example image.php, containing only the PHP code including the header. The second file, call it show.phpor show.html, includes the HTML code you like, including <img src="image.php" alt="Your generated image" />.

如果要在您的 html 文档中显示图像,则必须在两个文件中进行。一,例如调用它image.php,只包含包含标题的 PHP 代码。第二个文件,称为show.phpshow.html,包含您喜欢的 HTML 代码,包括<img src="image.php" alt="Your generated image" />.

回答by Gordon

If you base64 encode the output, you could use the image directly with a Data URI scheme.

如果您对输出进行 base64 编码,则可以直接使用带有Data URI scheme的图像。

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

        <title></title>
    </head>
    <body>
        <?php

        $myImage = imagecreate(200, 100);
        $myGray = imagecolorallocate($myImage, 204, 204, 204);
        $myBlack = imagecolorallocate($myImage, 0, 0, 0);
        imageline($myImage, 15, 35, 120, 60, $myBlack);
        ob_start();
        imagepng($myImage);
        printf('<img src="data:image/png;base64,%s"/>', 
                base64_encode(ob_get_clean()));

        imagedestroy($myImage);
        ?>
    </body>
</html>

Note that Data URIs are not supported by all browsers (guess which).

请注意,并非所有浏览器都支持数据 URI(猜猜是哪个)。

回答by Yuval A.

While trying to output a jpg of a Facebook profile-picture from their Graph API with PHP, I noticed that if the PHP file is saved with UTF-8 encoding - this error was returned, but if the file was saved with ANSI encoding - then it worked OK.

在尝试使用 PHP 从他们的 Graph API 输出 Facebook 个人资料图片的 jpg 时,我注意到如果 PHP 文件使用 UTF-8 编码保存 - 会返回此错误,但如果文件使用 ANSI 编码保存 - 那么它工作正常。

回答by sneha

If you have this problem, try to delete any space character between the begin of the script and the php tag

如果您遇到此问题,请尝试删除脚本开头和 php 标记之间的任何空格字符

> <?php

I spent several hours before realize this. And now it works OK. This happens because any character on the file alter the png format. This worked great for me.

我花了几个小时才意识到这一点。现在它工作正常。发生这种情况是因为文件中的任何字符都会更改 png 格式。这对我很有用。

回答by Noufal Ibrahim

You should output onlythe image. You are outputting a bunch of tags. Specifically

您应该输出图像。您正在输出一堆标签。具体来说

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

    <title></title>
</head>
<body>

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

    <title></title>
</head>
<body>

and then the image as a binary. If you want to see this, wgetthe page from your server and try opening it in an editor. Your code should start at the <?php.

然后将图像作为二进制文件。如果你想看到这个,wget你的服务器上的页面并尝试在编辑器中打开它。您的代码应该从<?php.

Removing the header gets rid of the notification to the client that this is an image so it will try out print it out as text.

删除标题消除了通知客户端这是一个图像,因此它会尝试将其打印为文本。

回答by Danack

Even if you think you have removed all the text from before header() your PHP file may contain a Byte Order Marker

即使您认为已从 header() 之前删除了所有文本,您的 PHP 文件也可能包含字节顺序标记

This will be invisible to you in your editor, but your browser will see it and think the image is corrupt. You need to take the steps appropriate to your editor to remove any BOM.

这将在您的编辑器中对您不可见,但您的浏览器会看到它并认为图像已损坏。您需要采取适合您的编辑器的步骤来删除任何 BOM。

回答by Shane Reustle

This is all you need. You can not print anything else because it needs to look like it's own file. You could call this image.php and pass it a variable to define which image to output.

这就是你所需要的。您不能打印任何其他内容,因为它需要看起来像它自己的文件。您可以调用此 image.php 并将其传递给一个变量来定义要输出的图像。

<?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);
?>

回答by Martin Bean

I isolated the PHP script into its own file (image.php) and it worked fine: I got a grey rectangle with a black slanted line. Your issue is trying to embed this within a HTML file.

我将 PHP 脚本隔离到它自己的文件 ( image.php) 中,它运行良好:我得到了一个带有黑色斜线的灰色矩形。您的问题是试图将其嵌入 HTML 文件中。

You need the PHP in its own, separate file (as I did, call it image.phpor something more description to your needs) and then create a HTML file as follows:

您需要 PHP 在它自己的单独文件中(就像我所做的那样,image.php根据您的需要调用它或更多描述),然后创建一个 HTML 文件,如下所示:

<html>
  <head>
    <title>Document Title</title>
  </head>
  <body>
    <img src="image.php" alt="" />
  </body>
</html>

You should get your desired output then.

然后你应该得到你想要的输出。

回答by user1520676

I had the same problem and the solution was to change the charset of the code from UTF-8 to ansi or viceversa. If the server is set to UTF-8 and your code is in ansi this don't work and if your code is in ansi and the server is configured to UTF-8 neither.

我遇到了同样的问题,解决方案是将代码的字符集从 UTF-8 更改为 ansi,反之亦然。如果服务器设置为 UTF-8 并且您的代码在 ansi 中,则这不起作用,如果您的代码在 ansi 中并且服务器也未配置为 UTF-8。

回答by Vishrant

If you are using Filezillato upload your webpages make sure you select transfer type as 'Auto/Binary'.

如果您使用Filezilla上传网页,请确保选择传输类型为'Auto/Binary'

Transfer -> Transfer type -> Auto/ Binary.

传输 -> 传输类型 -> 自动/二进制。