If you work on an extremely large project with a lot of source and required files, like myself, you will be well-advised to always clear the output buffer prior to creating an image in php.
clean the output buffer ob_clean()
<?php
// require some external library files
require ("lib/somelibrary.php");
require ("lib/class/someclass.php");
// clean the output buffer
ob_clean();
// simple test image
header("Content-type: image/gif");
$im = imagecreate (100, 50);
imagegif($im);
imagedestroy($im);
?>
ps:在导出Excel文件的时候,在header之前也需要清除缓冲区