Display image with headers problem

Problems with the Windows version of XAMPP, questions, comments, and anything related.

Display image with headers problem

Postby jerem » 13. February 2009 06:56

Hi --I'm testing Xampp after really struggling with Wamp trying to display images with a content header on 3 browsers (firefox, opera, netscape). However, I also have the problem with XAMPP, with the difference that I now get the following warning:

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\image_draw.php:1) in C:\xampp\htdocs\image_draw.php on line 2
PNG  IHDRnV, PLTE [an

The warning also includes a bunch of question marks.

I'm using the the code below which is straight out of the PHP manual. I tried Firefox Live HTTP headers and noted that the image exists, etc. but the GET image is being skipped. I checked to see if the headers were being sent by writing a redirection header to be executed if the headers for the image had NOT been sent, and I was redirected successfully. I can display images with echo, but not with a content header. I'm using windows XP and given the warning I suspect something in windows is attempting to modify the header. Here's the code:

<?php
header("Content-type: image/png");
$im = @imagecreate(110, 20)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, "A Simple Text String", $text_color);
imagepng($im);
imagedestroy($im);
?>

Code copied from Example 1 at http://www.php.net/manual/en/function.imagecreate.php
...but similar code won't work either.

Thank you very much for ideas on how to correct this.
jerem
 
Posts: 4
Joined: 13. February 2009 06:35

Re: Display image with headers problem

Postby Izzy » 13. February 2009 07:14

The above code from the php manual works as intended for me.

I just copied and pasted into a blank page in my text editor then saved it to the htdocs folder and called it by http://localhost/image_draw.php in both 1.6.8 and 1.7.0 XAMPP versions on XP.
Worked as expected in Firefox, Opera, SeaMonkey and IE.

Does your php code start at the very first line in the image_draw.php file?

Make sure after removing WAMP that you have cleared any WAMP registry entries and checked the php.ini file being used by XAMPP:

http://localhost/xampp/phpinfo.php
Look for Loaded Configuration File.
Should be C:\xampp\apache\bin\php.ini

Look in the Windows folder or your whole PC for any other php.ini files and either rename or delete them other than the XAMPP php.ini file.
Izzy
 
Posts: 3344
Joined: 25. April 2006 17:06

Re: Display image with headers problem...solved

Postby jerem » 13. February 2009 08:45

OK, the problem is solved :D but with 2 unexpected twists. The code works after using a different text editor (Open Office). It's strange because I had switched from Open Office to Note Pad 3 months ago precisely because of a problem with good code not working.

The file had (or seemed to have) no blank spaces whatsoever in Notepad. It was exactly as one can read it in my post with no blank spaces preceeding it. Yet when I switched and opened the file with Open Office that was not the case. It had the following 3 characters before the opening PHP:



I did not place those characters there; evidently notepad did.

After I renamed the script, deleted the characters, and saved in htdocs as an open office text file... image_draw.php...I was able to open it through local host and the image was displayed. :D

So I suppose there is something in my system altering my documents since I have now had the problem with both text editors.

In any event, after a full search of the C drive I have only 4 php.ini files, as follows:
Configuration Settings in C:\xampp\php
Configuration Settings in C:\xampp\apache\bin
INI-DIST File in C:\xampp\php
INI-RECOMMENDED File in C:\xampp\php

Should I suppose that's ok?

Any idea how code written in these 2 text editors is being modified? Any suggestions for text editors?

Thanks very, very much for your help. The key was that there was something before the header even though it was not visible in notepad.
jerem
 
Posts: 4
Joined: 13. February 2009 06:35

Re: Display image with headers problem

Postby Izzy » 13. February 2009 09:03

With the php.ini files, if the Loaded Configuration File in phpinfo() is the one in the apache bin folder then that should be just fine as Apache will always use that by default as long as there is not a php.ini file in the Windows folder or in the Environment Variable paths.

Notepad, I have no idea what would be causing those characters to be included - just checked my Notepad but none of those characters are inserted.

I use NoteTab Pro form Fookes Holdings http://www.notetab.com which replaces Notepad on my system but there are quite a few free offerings by doing a Google I would assume - Wordpad would also do if you remember to save as a text file not an RTF file and there are some code orientated text editors that are also open source and free.
Izzy
 
Posts: 3344
Joined: 25. April 2006 17:06

Re: Display image with headers problem

Postby Wiedmann » 13. February 2009 09:04

It had the following 3 characters before the opening PHP:


PHP don't like UTF-8, and especially the BOM, in its files. Just disable this in your editor.
Wiedmann
AF Moderator
 
Posts: 17102
Joined: 01. February 2004 12:38
Location: Stuttgart / Germany

Re: Display image with headers problem

Postby jerem » 13. February 2009 09:28

Ahh...so it's the UTF-8. Thank you Wiedmann and Izzy for the tips.
jerem
 
Posts: 4
Joined: 13. February 2009 06:35

Re: Display image with headers problem

Postby Nobbie » 13. February 2009 09:59

And last not least, you really should go for an editor - neither Open Office nor Notepad is a good choice. There are many powefull editors out there which work by far better than Notepad - and OpenOffice is not yet an editor, it's a sophisticated word processing tool for writing letters and documentation (in order to print it out - not well suited for program code).

Textpad, Uedit, gvim (free), AgPad (free) etc. pp.
Nobbie
 
Posts: 13182
Joined: 09. March 2008 13:04

Re: Display image with headers problem

Postby jerem » 14. February 2009 04:10

Thanks very much for the recommendations Nobbie. I'll certainly check them out.
jerem
 
Posts: 4
Joined: 13. February 2009 06:35


Return to XAMPP for Windows

Who is online

Users browsing this forum: steffenmaier and 113 guests