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.

About these ads

31 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

    • orparttj said,

      August 3, 2012 at 9:24 am

      It works gr8. Thanks

    • Junior said,

      July 18, 2013 at 1:09 pm

      Thanks!!!!

  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!

  7. October 31, 2012 at 1:25 pm

    I am also facing the same problem, am using the wordpress latest version.
    It happens almost all the time :D

  8. jacko said,

    November 6, 2012 at 2:40 pm

    Thank you, thank you, thank you!!!
    Spent 2hours trying to figure this **** out before coming across this info!!

    I a black line 2 with my <?php tag on line 2 which was the root of the problem!!

    Such a simple (stupid) mistake to make without even thinking about it!

    Anyway, here are my searches (to help you out with the G):

    php readfile($image) cannot be displayed, because it contains errors

    php echo file_get_contents($image) cannot be displayed, because it contains errors

    • jacko said,

      November 6, 2012 at 2:42 pm

      That should be:
      I had a BLANK line 1 with my <?php tag on line 2

      Make sure your <?php tah is the first thing on line 1

  9. nag said,

    December 6, 2012 at 8:22 pm

    It is still showing the error, the image cant be displayed because it may contain some error.

    <?php
    header('Content-type:image/jpg');
    $password = "myPassword_!";
    $messageClear = 'farmer.jpg';

    // 32 byte binary blob
    $aes256Key = hash("SHA256", $password, true);
    // generate random iv
    $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC), MCRYPT_RAND);

    $crypted = fnEncrypt($messageClear, $password);

    $newClear = fnDecrypt($crypted, $password);

    ob_clean();

    echo
    "IV: ".$iv."“.
    “Encrypted: ".$crypted."“.

    “Decrypted: ".$newClear."“;

    function fnEncrypt($sValue, $sSecretKey) {
    global $iv;
    return rtrim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $sSecretKey, $sValue, MCRYPT_MODE_CBC, $iv)), “\3″);
    }

    function fnDecrypt($sValue, $sSecretKey) {
    global $iv;
    return rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $sSecretKey, base64_decode($sValue), MCRYPT_MODE_CBC, $iv), “\3″);
    }
    ?>

    Document Title

  10. Takondwa said,

    December 19, 2012 at 12:25 pm

    This helped me alot after toiling for days. thank alot

  11. Sbwire.Com said,

    January 19, 2013 at 11:13 am

    Hi there! Do you know if they make any plugins to protect
    against hackers? I’m kinda paranoid about losing everything I’ve worked hard on.
    Any tips?

  12. Juegos said,

    January 26, 2013 at 12:43 pm

    Oh my goodness! Incredible article dude! Thank you so much, However I am experiencing difficulties with your RSS.
    I don’t know the reason why I can’t join it. Is there anybody getting similar RSS problems?
    Anyone who knows the solution can you kindly respond?
    Thanks!!

  13. nose job said,

    January 28, 2013 at 7:13 am

    hello there and thank you for your info – I have definitely
    picked up something new from right here. I did however expertise a few technical issues using this web site, since I experienced to reload the site lots of times previous to I could get it to load correctly.
    I had been wondering if your web host is OK? Not that I’m complaining, but sluggish loading instances times will sometimes affect your placement in google and can damage your high-quality score if advertising and marketing with Adwords. Anyway I’m adding this RSS to my
    e-mail and could look out for a lot more of your respective intriguing content.
    Ensure that you update this again very soon.

  14. February 2, 2013 at 11:29 am

    Thanks for sharing your thoughts on business plan format example.
    Regards

  15. February 8, 2013 at 3:34 am

    wonderful issues altogether, you just received a new reader.

    What might you recommend in regards to your publish that you simply made some
    days in the past? Any certain?

  16. February 10, 2013 at 1:14 pm

    Good way of seeing things – I am a bit more of a monochrome
    guy, myself

  17. Mike lagan said,

    March 23, 2013 at 11:51 pm

    The PHP error handler desparately needs upating save everyone being lead up ther garden path for hours on end – 5* Many thanks!!!

  18. Mann said,

    June 12, 2013 at 11:51 am

    Thanks buddy :)

  19. Mico said,

    August 30, 2013 at 12:21 pm

    Spent hours on that problem!
    Good thing I opened the page on Mozilla to know wtf is wrong.
    Then found the right keywords using Google, and here’s your blog giving me the solution.
    Thanks mate! :))

  20. sexy girls said,

    November 29, 2013 at 2:36 am

    I will right away grab your rss as I can not to find your
    email subscription hyperlink or e-newsletter service.
    Do you’ve any? Please let me understand in order that I may subscribe.
    Thanks.

    • VinodKumar Saravana said,

      February 16, 2014 at 1:46 am

      It is available in the left side sidebar as block…just enter your email address and you will get update on the new post…thanks for subscribing…

  21. amit kumar said,

    January 27, 2014 at 2:37 pm

    thank you very much……
    your suggestion of space character gives the solution……..

  22. amit kumar said,

    January 27, 2014 at 2:47 pm

    thank


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 )

Google+ photo

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

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: