The image cannot be displayed because it contains errors
Solution 1
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" />
.
Solution 2
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).
Solution 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.
Solution 4
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.
Solution 5
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.
Related videos on Youtube

TCM
Updated on July 09, 2022Comments
-
TCM 5 months
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 enabledphp_gd2.dll
andmemory_limit
in php.ini is also 128M. If i removeheader('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
‚ `-
RuudVanNistelrooy over 7 yearsWatch 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.
-
-
Noufal Ibrahim over 12 yearsWell, not necessarily in it's own file. Just that the output shouldn't mix HTML and the PNG.
-
Johan over 12 yearsInteresting variant. Can you control the way the web browser should cache this image?
-
Gordon over 12 years@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.
-
Alexar over 9 yearsThanks Yuval, it all solved my problem. the Encoding of php file!