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

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

问题描述

为什么此代码不起作用?

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>

我总是收到错误The image cannot be displayed because it contains errors..我已经在php.ini中启用了php_gd2.dllmemory_limit,它也是128M.如果我删除header('Content-type: image/png');,我不会收到错误,但也不会看到图像.我所看到的就是这个:-

I always get error The image cannot be displayed because it contains errors.. I've already enabled php_gd2.dll and memory_limit in 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 :-

‰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标签. 甚至不要在 header() 前写一个空行.

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().

如果要在自己的html文档中显示图像,则必须在两个文件中进行显示.一种,例如以 image.php 命名,仅包含包含标头的PHP代码.第二个文件称为 show.php show.html ,其中包含您喜欢的HTML代码,包括<img src="image.php" alt="Your generated image" />.

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.php or show.html, includes the HTML code you like, including <img src="image.php" alt="Your generated image" />.

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

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