18

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

TCM
  • 16,780
  • 43
  • 156
  • 254
  • 1
    Watch out for PHP error reporting, especially since 5.4. The error won't be printed on screen but will throw an error with the image. Try 'error_reporting(0);' at the top of your code, worked for me. – Chaoley Mar 15 '15 at 08:12

10 Answers10

30

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

Leopoldo Sanczyk
  • 1,529
  • 1
  • 26
  • 28
Johan
  • 5,003
  • 3
  • 36
  • 50
  • Sorry i didn't understand. What changes do i need to do? header() is already the first line of php. If you mean i should remove `html`,`head` etc, i tried that too. If i do that i still get `image contains errors` – TCM Aug 02 '10 at 08:03
  • @Nitesh you dont have to have two files. [See my answer below for an inline solution](http://stackoverflow.com/questions/3385982/the-image-cannot-be-displayed-because-it-contains-errors/3386050#3386050). It's just more sensible and reliable to have two files. – Gordon Aug 02 '10 at 08:39
  • 1
    Removing extra spaces at beginning of the file resolved the error for me :) – coletrain Mar 27 '20 at 20:09
12

If you base64 encode the output, you could use the image directly with a 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).

Gordon
  • 312,688
  • 75
  • 539
  • 559
  • Interesting variant. Can you control the way the web browser should cache this image? – Johan Aug 02 '10 at 12:26
  • 1
    @Johan The image is not an external resource with this approach. There will be no expiry headers or something. It's text on the served page. Any caching that applies to the page, will apply to the image. – Gordon Aug 02 '10 at 12:41
3

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

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.

Community
  • 1
  • 1
Danack
  • 24,939
  • 16
  • 90
  • 122
3

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

<!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, wget the page from your server and try opening it in an editor. Your code should start at the <?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.

Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169
3

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.

Yuval A.
  • 5,849
  • 11
  • 51
  • 63
3

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

> <?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.

Stephen
  • 1,737
  • 2
  • 26
  • 37
sneha
  • 31
  • 1
2

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.

<?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);
?>
Shane Reustle
  • 8,633
  • 8
  • 40
  • 51
2

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.

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

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

You should get your desired output then.

Martin Bean
  • 38,379
  • 25
  • 128
  • 201
0

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.

0

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

Transfer -> Transfer type -> Auto/ Binary.

Vishrant
  • 15,456
  • 11
  • 71
  • 120