The Image Cannot Be Displayed Because It Contains Errors

The image cannot be displayed because it contains errors (php)

Solved: I added ob_end_clean(); before the header and it worked.

The image cannot be displayed because it contains errors [Image generator]

Open the image in text editor, I think you'll find a warning from php there.

PHP GD: The image cannot be displayed because it contains errors

$im = @imagecreatetruecolor(120, 20)
or die('Cannot Initialize new GD image stream');

You first hide the real error and TRY to display something...

which you can't display because you don't look for it,

and expose the image no matter if it really got generated.

Then you go on stackoverflow and hope someone can guess the error you might have simply suppressed using @ operator.

Make sure there is nothing before <?php and if you have, remove ?> at the end.

To make sure you have GD installed try this in a new php file:

<?php
if (extension_loaded('gd') && function_exists('gd_info')) {
echo "PHP GD library is installed on your web server";
}
else {
echo "PHP GD library is NOT installed on your web server";
}

Getting The Image cannot be displayed because it contains an error

This is caused because there is html code on top of page too. When header("Content-type: image/jpeg"); is executed chrome interprets the content of whole page as image data in which html is included.

To fix this put the image output when id is passed code at top of script and exit the script as soon as you output the image else the code below it will be outputted too.

It should be done like this :

<?php
//this should be at top of php file, above html code
$link = mysqli_connect('127.0.0.1:3306','user','');
mysqli_select_db($link, "media");
if(isset($_POST['pictureID']))
{
$imgId = $_POST['pictureID'];
//echo $imgId; nothing else expect image should be echoed
if (!empty($imgId)) {
$sqliCommand = "SELECT Image FROM images WHERE Picture = $imgId";// select Image here so not select picture as you did earlier
$result = mysqli_query($link, $sqliCommand);
$row = mysqli_fetch_assoc($result);
mysqli_close($link);//close mysqli as script gonna exit after echoing image.
header("Content-type: image/jpeg");
echo $row['Image'];
exit();// this is important
}
}
?>
<html>
<body>
<!-- rest of the code -->

Read the comments I wrote in code.

There must be nothing expect image binary code on page if you set the header as image type.

If the problem persists then comment the header line(this will make page appear in text form) and see if there is anything else in page expect image data in binary form.

The image cannot be displayed because it contains errors(help)

Your call to imagejpeg is trying to write the image to a file with an empty path (the second parameter) instead of displaying it in the browser, so you need to set this parameter to "null" instead.

You need to change your code to this:

// Set second param to "null" to directly output the image
imagejpeg ( $image, null, 90 );
imagedestroy ( $image );
// Exit here so that the HTML below is not mixed with the image data:
die();


Related Topics



Leave a reply



Submit