Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

png content type not working 1

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
I seem to be having problems displaying png's I am using the following mime type header...
Code:
print "Content-type: image/png\n\n";

but the image is not shown, what am i doing wrong, jpeg and gif work fine in the same script!

thanks
1DMF

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
well i'm using IE7 and if i type the url to the png I can see it fine, if i read it via perl and print to STDOUT binary, the browser doesn't show it, just an image place holder with an [x].

why?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Sledgehammer/nut approach.

[ol][li]Install Wireshark, if you dont already have it (easy, free).[/li][li]Start a monitoring session. Point your browser at which has a .png image in it.[/li][li]See what they send you.[/li][li]Copy it.[/li][li]Er, that's it...[/li][/ol]

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
don't understand, i've got a load of info, what's it telling me?
0000 00 0c 41 92 1c 86 00 0c 6e d8 03 c4 08 00 45 00 ..A..... n.....E.
0010 01 c6 d5 97 40 00 80 06 25 fa c0 a8 00 02 42 23 ....@... %.....B#
0020 fa d2 07 dc 00 50 ed 4d d9 ff 72 ea e5 94 50 18 .....P.M ..r...P.
0030 ff ff 02 f4 00 00 47 45 54 20 2f 70 75 62 2f 70 ......GE T /pub/p
0040 6e 67 2f 20 48 54 54 50 2f 31 2e 31 0d 0a 41 63 ng/ HTTP /1.1..Ac
0050 63 65 70 74 3a 20 2a 2f 2a 0d 0a 52 65 66 65 72 cept: */ *..Refer
0060 65 72 3a 20 68 74 74 70 3a 2f 2f 77 77 77 2e 74 er: http ://0070 65 6b 2d 74 69 70 73 2e 63 6f 6d 2f 76 69 65 77 ek-tips. com/view
0080 74 68 72 65 61 64 2e 63 66 6d 3f 71 69 64 3d 31 thread.c fm?qid=1
0090 33 31 34 38 30 32 26 70 61 67 65 3d 31 0d 0a 41 314802&p age=1..A
00a0 63 63 65 70 74 2d 4c 61 6e 67 75 61 67 65 3a 20 ccept-La nguage:
00b0 65 6e 2d 67 62 0d 0a 55 41 2d 43 50 55 3a 20 78 en-gb..U A-CPU: x
00c0 38 36 0d 0a 41 63 63 65 70 74 2d 45 6e 63 6f 64 86..Acce pt-Encod
00d0 69 6e 67 3a 20 67 7a 69 70 2c 20 64 65 66 6c 61 ing: gzi p, defla
00e0 74 65 0d 0a 49 66 2d 4d 6f 64 69 66 69 65 64 2d te..If-M odified-
00f0 53 69 6e 63 65 3a 20 54 75 65 2c 20 32 38 20 4e Since: T ue, 28 N
0100 6f 76 20 32 30 30 36 20 31 37 3a 30 36 3a 32 31 ov 2006 17:06:21
0110 20 47 4d 54 0d 0a 49 66 2d 4e 6f 6e 65 2d 4d 61 GMT..If -None-Ma
0120 74 63 68 3a 20 22 38 32 63 65 39 36 2d 37 31 65 tch: "82 ce96-71e
0130 34 2d 34 35 36 63 36 63 38 64 22 0d 0a 55 73 65 4-456c6c 8d"..Use
0140 72 2d 41 67 65 6e 74 3a 20 4d 6f 7a 69 6c 6c 61 r-Agent: Mozilla
0150 2f 34 2e 30 20 28 63 6f 6d 70 61 74 69 62 6c 65 /4.0 (co mpatible
0160 3b 20 4d 53 49 45 20 37 2e 30 3b 20 57 69 6e 64 ; MSIE 7 .0; Wind
0170 6f 77 73 20 4e 54 20 35 2e 31 3b 20 2e 4e 45 54 ows NT 5 .1; .NET
0180 20 43 4c 52 20 32 2e 30 2e 35 30 37 32 37 3b 20 CLR 2.0 .50727;
0190 2e 4e 45 54 20 43 4c 52 20 31 2e 31 2e 34 33 32 .NET CLR 1.1.432
01a0 32 29 0d 0a 48 6f 73 74 3a 20 77 77 77 2e 6c 69 2)..Host : 01b0 62 70 6e 67 2e 6f 72 67 0d 0a 43 6f 6e 6e 65 63 bpng.org ..Connec
01c0 74 69 6f 6e 3a 20 4b 65 65 70 2d 41 6c 69 76 65 tion: Ke ep-Alive
01d0 0d 0a 0d 0a

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Also i have tried using FireFox and get the following info..
The image “url/blah/blah.cgi?LOGO=logo.png” cannot be displayed, because it contains errors.

So what are these errors, the code works fine for jpg and gif , so what error is it likely to be ?

if you point the browser to the image via url it displays fine?

any ideas?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
I don't think it's a problem with the header:

print "Content-type: image/png\n\n";

can you post a link to this image?

- Kevin, perl coder unexceptional!
 
OK, the image looks to be good. Lets see the perl script.



- Kevin, perl coder unexceptional!
 
This is a stab in the dark, but a common error is that you're not writing in binary.

Code:
binmode STDOUT;
print "Content-Type: image/png\n\n";

open (PNG, "./png.png");
my @bin = <PNG>;
close (PNG);
chomp @bin;

print join "",@bin;

(if that doesn't work, try moving the binmode command after the content-type header, or print join "\n",@bin)

-------------
Cuvou.com | The NEW Kirsle.net
 
Wireshark reports the following header in response to a GRT for a .png
Code:
HTTP/1.1 200 OK
Date: Wed, 20 Dec 2006 19:18:53 GMT
Server: Apache/1.3.33 (Unix) PHP/4.3.10
Last-Modified: Sat, 04 Sep 1999 21:46:38 GMT
ETag: "8dce6d-436c-37d1933e"
Accept-Ranges: bytes
Content-Length: 17260
Connection: close
Content-Type: image/png
##blank line here##
binary png data starts here
Kirsle was right about the binmode, but you can't change it until you've written the header. Does this give you an idea (like specifying the content length maybe).

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
For GRT read GET. My bad.

Also, I'm a bit confused. If you use a standard web server, why can't you configure it to serve up the .png on its own like the one above, without using perl? The only reason you should need to use a programmatic solution is if you are generating the .png image on the fly, like a graph or something. The sample you posted doesn't seem to meet that criterion.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
hang , hang on guys, the image I provided was for test only.

this is a members system and ONLY members can upload, and ONLY members can see the images.

So I use perl to serve up the images after checking their login credentials, the why and wherefor's are completely legit!

the perl code is this (and works fine for JPG & GIF)

Code:
sub get_img {

# Work out extension
my ($fn,$ext) = split(/\./, $logo);
$ext = lc($ext);

# Check file type and print relevant MIME header
for ($ext) {
             /png/     && do { print "Content-type: image/png\n\n"; last; };
             /jpg/     && do { print "Content-type: image/jpeg\n\n"; last; };
             /gif/     && do { print "Content-type: image/gif\n\n"; last; };
             
}

my $file = AR_DIR_TO_WEB . "/$AR/logo/$fn.$ext";

# Open Image File
open(FILE, "<$file") || die "Error opening image file, seek support!";

# Prepare STDOUT for binary data
binmode(STDOUT);

# Print File
print <FILE>;
close(FILE);

}



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
It might be an idea to set a value for Content-Length:

Also, you should probably set binmode(FILE) after you open it. It's all very well writing in binary mode, but you have to read it that way too...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Spot on steve, that was exactly the problem!

as soon as I added binmode(FILE); it worked fine.

Why did it not error for gif & jpg or why does it not read as binary for png automatically?

anyway thanks for the help, have a great christmas.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
I think it just depends on the contents of the file, as to whether or not perl 'interprets' the contents in the way you expect. Probably safest to do it as a matter of course for any binary file you want to serve up, .gif, .jpg, Adobe pdfs, etc.

Merry Xmas, thanks for the star...[smile]

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top