Jump to content

Can't create/display php images - GD Image corrupt or truncated: http://localhost/my_scripts/images.php


Lykos22

Recommended Posts

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 by Lykos22
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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%
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

justsomeguy you are amazing :good: ! 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 by Lykos22
Link to comment
Share on other sites

  • 3 weeks later...

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;

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...