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.

Skull Tattoos Vectors « The Bad Band – LA BANDA DE LOS CULPABLES said,
April 26, 2010 at 10:05 am
[...] the image cannot be displayed, because it contains error (scvinodkumar.wordpress.com) Comments RSS feed [...]
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
Social SignIn Mods said,
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
Amirul Asyraf said,
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.
the image cannot be displayed, because it contains error « Dave Schwab said,
July 14, 2011 at 5:30 am
[...] You can go here to read the post. Apache, GD, ImageMagick, imagick, LINUX, php, Windows, Windows 7 [...]
softboxkid said,
August 10, 2011 at 12:14 am
thanks for the info! and thanks (Adawo) for the links
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!