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.