the image cannot be displayed, because it contains error

I am displaying image using below code


header("Content-type: image/jpeg");
header('Expires: ' . date('r',time() + 864000));
header("Pragma: public");
header("Cache-Control: public");
header("Content-Length: " . strlen($contents));
echo $contents;


where $contents is getting from fread functions. Some days later the images are not displaying and i am getting the below errors

“The image <Route to imagescript.php> cannot be displayed, because it contains errors”

And after a long Google search, i found the solutions, May be this might be very helpful to others who have same issues.

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.

10 Comments

  1. April 26, 2010 at 10:05 am

    [...] the image cannot be displayed, because it contains error (scvinodkumar.wordpress.com) Comments RSS feed [...]

  2. Adawo said,

    July 20, 2010 at 7:05 pm

    Some another solution is use ob_clean() function before output image content. More info: http://www.php.net/manual/en/function.ob-clean.php#75694

    • June 28, 2011 at 2:34 pm

      Thanks Adawo, I really do not find from where the white space was coming for my image. But ob_clean(); really did the trick. You made my day, thanks a heaps :)

      • abdul muin aam said,

        November 1, 2011 at 7:23 am

        ob_clean();
        great……. , it’s work… thanks

    • frenzal said,

      January 5, 2012 at 8:56 pm

      thanks! this fixed what seems to be a wordpress bug

  3. June 22, 2011 at 4:25 am

    Hi. I just want to ask.
    Why the space affected this error?

    • David Schwab said,

      August 11, 2011 at 6:17 am

      A computer sees any white space as a character, the same as any alphanumeric character. So outputting a white space is the same as outputting any other letter or number and it will cause the file to be damaged and unreadable.

  4. July 14, 2011 at 5:30 am

    [...] You can go here to read the post. Apache, GD, ImageMagick, imagick, LINUX, php, Windows, Windows 7 [...]

  5. softboxkid said,

    August 10, 2011 at 12:14 am

    thanks for the info! and thanks (Adawo) for the links

  6. David Schwab said,

    August 11, 2011 at 6:15 am

    So the ob_clean() function before writing the header() and readfile() did it for me. Adawo’s solution works and I highly recommend it!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.