Lykos22 4 Posted December 21, 2012 Report Share Posted December 21, 2012 (edited) I'm having a problem and i'd like some help please. I have made a script for creating an image with PHP using GD library, but the problem is when i preview it on the Firefox it shows me an error in Firebug: "Image corrupt or truncated:localhost/my_scripts/images.php". NOTE: this is not an error displayed from PHP, it seems more like an error comming from the browser.I have checked my code for syntaxes many times, i have also tried out and other gd functions, like imagegif, imagettftext etc etc but didn't have any effect. Here's my code: <?php//$image = @imagecreate(200, 20)or die("Cannot Initialize new GD image stream");$image = imagecreate(200, 20);$background = imagecolorallocate($image,0,0,0);$foreground = imagecolorallocate($image,255,255,255);imagestring($image,5,5,1,"This is a Test",$foreground);header("Content-type: image/jpeg");imagejpeg($image);imagedestroy($image);?> GD library is enabled i've already checked that in phpinfo(), and I have also tried to preview it and on Chrome and looks the same. Any ideas what i might doing wrong or why happens this? and how can i fix this error?? Edited December 21, 2012 by Lykos22 Quote Link to post Share on other sites
kanchatchai 2 Posted December 21, 2012 Report Share Posted December 21, 2012 that work for memay need<?PHPand?> Quote Link to post Share on other sites
Lykos22 4 Posted December 21, 2012 Author Report Share Posted December 21, 2012 that work for memay need<?PHPand?>I always-always use the php tags! otherwise you can't write php Quote Link to post Share on other sites
birbal 168 Posted December 21, 2012 Report Share Posted December 21, 2012 comment out header() and check what is the output results. Your output has some sorts of other output rather than only the image data. probably some php errors are there which is breaking the image. Quote Link to post Share on other sites
Lykos22 4 Posted December 21, 2012 Author Report Share Posted December 21, 2012 comment out header() and check what is the output results. Your output has some sorts of other output rather than only the image data. probably some php errors are there which is breaking the image.if I comment out the header function shows strange symbolslike these: JFIF>CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), default quality C $.' ",#(7),01444'9=82<.342C 2!!22222222222222222222222222222222222222222222222222" }!1AQa"q2#BR$3br %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz w!1AQaq"2B #3Rbr $4% Quote Link to post Share on other sites
Ingolme 1,035 Posted December 21, 2012 Report Share Posted December 21, 2012 That's the representation of the image data. You can put the header back. There are no PHP errors shouwing. I think you might have some whitespace or line breaks before or after the PHP block which is corrupting the image. Quote Link to post Share on other sites
justsomeguy 1,135 Posted December 21, 2012 Report Share Posted December 21, 2012 Make sure that PHP code is the only thing on the page. No HTML, no spaces before or after the PHP code, etc, the only thing it should send to the browser should be the image data. If that still doesn't work then make sure you're saving your PHP script in a format that does not include the UTF BOM. If you're saving your code as UTF, look for an option in your text editor to save it without the BOM. If you need a text editor, I suggest Sublime Text. Quote Link to post Share on other sites
Lykos22 4 Posted December 22, 2012 Author Report Share Posted December 22, 2012 (edited) justsomeguy you are amazing ! I used notepad++ and set the format to UTF-8 without BOM and works, I can see the image on the browser, no errors etc. So I guess this is an error coming from the file, or not? What is this BOM??? Do I have to use this format in all my documents (php,html,js,txt etc)? - in both Notepad++ and Dreamweaver i had my format set to UTF-8 only. If I upload a file, like captcha on a contact form, on a remote server how could i make sure that this will apply and will display the image?? Edited December 22, 2012 by Lykos22 Quote Link to post Share on other sites
Ingolme 1,035 Posted December 22, 2012 Report Share Posted December 22, 2012 You should apply UTF-8 without BOM on all your documents. Quote Link to post Share on other sites
justsomeguy 1,135 Posted January 7, 2013 Report Share Posted January 7, 2013 The byte order mark tells the program reading the file about the encoding. If the BOM is not there then the program just assumes a default which is usually correct. PHP never needs a BOM, in fact it will ignore it and send it to the browser as output like you're seeing. The only time you need to use a BOM with PHP is if you're outputting UTF data that needs to be read by another program, like a CSV file. If you have a CSV file that contains UTF characters, and you want them to display properly in Excel, then you need to output a BOM with the CSV data, e.g.: header('Last-Modified: ' . gmdate('D,d M YH:i:s') . ' GMT');header('Cache-Control: must-revalidate, post-check=0, pre-check=0');header('Pragma: public');header('Content-Description: File Transfer');header('Content-type: application/octet-stream');header('Content-Disposition: attachment; filename="export.csv"');header('Content-Transfer-Encoding: binary');echo "\xEF\xBB\xBF"; // UTF-8 BOMecho $csv_data; Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.