Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The image cannot be displayed because it contains errors

Tags:

php

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.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‚ `

like image 745
TCM Avatar asked Aug 02 '10 07:08

TCM


People also ask

Is the image Cannot be displayed because it contains errors?

''The image [image] cannot be displayed because it contains errors. '' is message that Firefox displays as a placeholder of the actual image until this image has been retrieved and decoded/expanded and can be rendered. If that message stays then the image can't be displayed for some reason and possibly was blocked.

Why won't images show up on Firefox?

Chosen solutionClear the Cache and remove the Cookies from websites that cause problems via the "3-bar" Firefox menu button (Options/Preferences). Start Firefox in Safe Mode to check if one of the extensions ("3-bar" menu button or Tools -> Add-ons -> Extensions) or if hardware acceleration is is causing the problem.


1 Answers

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

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" />.

like image 151
Johan Avatar answered Oct 04 '22 03:10

Johan