homepage Welcome to WebmasterWorld Guest from 54.205.144.231
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
problems displaying a PNG file with headers
the PNG file doesn't show in the browser window if i use headers
vladady




msg:3682075
 2:55 am on Jun 24, 2008 (gmt 0)

hi,

i have the following problem:

I create an PNG image into $im.
Then i want to display it in the browser.
And i use the following code:

header("Content-type: image/png");
ImagePNG($im);
Imagedestroy($im);

The problem is that it doesn't show.
If i output it in a file: ImagePNG($im,'file.png'); i can see the correct image in the file, so the code that generates the image in $im is correct.

The problem occurs when i try to output it with headers.

In Mozilla it says: "The image cannot be displayed becauase it contains some errors".
In IE it show the image with an X like it didn't find it.

So it's not just about Mozilla. (I don't have the Skype Add-on).

PS: I tried with ob_start() before the header(), ob_end_flush() at the end. Also with something like :
header('Last-Modified: '.date('r'));
header('Accept-Ranges: bytes');
header('Content-Length: '.$length);

 

NomikOS




msg:3682091
 3:36 am on Jun 24, 2008 (gmt 0)

$im = imagecreatefromPNG ('file.png'); 
imagePNG($im)

[edited by: NomikOS at 3:40 am (utc) on June 24, 2008]

vladady




msg:3682097
 3:47 am on Jun 24, 2008 (gmt 0)

I know.
It is already created into $im, it's a CAPTCHA image to be more exact.
I put in the post only the code from where the headers sending starts.

That's because i ouputed the image into a file and looked at it with a photo viewer. The script creates the correct image! I tested the png file created in both browsers and it displays correct too.

The problem is to display it with headers....

NomikOS




msg:3682139
 6:20 am on Jun 24, 2008 (gmt 0)

I did a problematic captcha script sometime but now i'dont remember it.
Is something like this right?
<img src="captcha.php"/> 

<?php
# captcha.php

$im = imagecreatefrompng('file.png');

# (BTW: don't forget do it not cacheable for it can change between page loads)
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);

header('Content-type: image/png');

imagepng($im);
imagedestroy($im);
?>

Is the path to script correct? src="{_path_}captcha.php"
Is there subdomains involucred?

[edited by: NomikOS at 6:23 am (utc) on June 24, 2008]

vladady




msg:3682296
 11:57 am on Jun 24, 2008 (gmt 0)

Yes it's like that:
<img src="captcha.php?code=CODE"/>
<?php
# captcha.php

$im = imagecreatefrompng('file.png');
.
.
.
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>

The script's path is : './captcha.php'.

But either if call directly in the browser it doesn't work.
Only if i put the image to a png file, it shows well.
What can be the problem?

I tried with the headers:
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);

and the situation is the same.

wtkad




msg:3682314
 12:23 pm on Jun 24, 2008 (gmt 0)

Have you tried using ImagePNG($im) without setting the header? It will let you see the "raw" PNG output and maybe see if there are any hidden errors.

vladady




msg:3682370
 1:48 pm on Jun 24, 2008 (gmt 0)

It was a very stupid thing: "<?php" , an extra tab before one of the php files.
It was very hard to find because i have a lot of php files. I noticed it only when commenting ImagePNG and selecting what was in the view source. If i wouldn't have selected the text, i would have thinked that the it is ok, and no extra characters. The extra character wasn't appearing in the file when i was exporting it.

Thank you guys for the replies.

NomikOS




msg:3682704
 8:06 pm on Jun 24, 2008 (gmt 0)

Happy to see that you resolve the problem.
That's the fun ...

[edited by: NomikOS at 8:12 pm (utc) on June 24, 2008]

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved