Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

CGI: print a gif directly from a print statement

by meraxes (Friar)
on Nov 30, 2001 at 21:27 UTC ( [id://128666]=perlquestion: print w/replies, xml ) Need Help??

meraxes has asked for the wisdom of the Perl Monks concerning the following question:

I want to have a CGI script return a simple 1x1 transparent gif. However, I wanted to avoid opening the gif file, reading the contents, printing the contents and then closing the file.

I tried this (on a windows nt box), but it didn't work:

print "Content-type: image/gif\n\n"; binmode(STDOUT); print 0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x01, 0x00, 0x01, 0x00, 0x80 +, 0x00, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x00, 0 +x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x02, 0x02, 0x44, 0x01, 0x00 +, 0x3B;

The print statement is the hex representation of a 1x1 transparent gif file. Why didn't this work and is there something similar I can do?

Many thanks,
meraxes

Replies are listed 'Best First'.
Re: CGI: print a gif directly from a print statement
by impossiblerobot (Deacon) on Nov 30, 2001 at 21:53 UTC
    All you're printing with your print statement is a list of the decimal equivalents of the hex numbers you've listed (instead of the character representation).
    You could use pack, but in the past, I've done this using:
    print "\x47\x49\x46\x38 . . .


    Impossible Robot
Re: CGI: print a gif directly from a print statement
by MZSanford (Curate) on Nov 30, 2001 at 21:52 UTC
    Actually, jcrewn just did something similar here as part of a homenode picture rotation program. It should cover the issue.
    i had a memory leak once, and it ruined my favorite shirt.
Re: CGI: print a gif directly from a print statement
by dws (Chancellor) on Nov 30, 2001 at 22:17 UTC
    On Win32, you need to declare binmode before printing "\r\n", otherwise, you'll actually get "\r\r\n", which will confuse the Browser. You also need to pack the bytes.

    See On-demand single-pixel GIFs for a working example (it was developed on Win32).

Re: CGI: print a gif directly from a print statement
by Anonymous Monk on Nov 30, 2001 at 22:10 UTC
    Thanks MZSanford, impossiblerobot. I see now the error of my ways :)

    Seems I'm going to have to dig deeper into some of my books and do better research before asking these questions.

    Thanks,
    meraxes

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://128666]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-26 00:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found