Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Generating pngs with Mason

by water (Deacon)
on Dec 04, 2005 at 01:33 UTC ( [id://513877]=perlquestion: print w/replies, xml ) Need Help??

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

Hi --

I am trying to generate dynamic pngs with Mason, like the photo gallery article on perl.com.

I'm trying to get the simple bits working before folding in the full application logic, so for now, I'm just trying to use a dhandler to server a red square for any file requests ending in .png in a certain directory. (That directory has no real png files.)

The dhandler looks like this:

<%shared> return unless $r->uri =~ /\.png$/; # die "made it here!"; use GD; my $image = new GD::Image(100, 100); my $red = $image->colorAllocate( 255, 0, 0); $image->filledRectangle( 25, 25, 75, 75, $red); $m->clear_buffer; $r->content_type('image/png'); $m->print($image->png); $m->abort(200); </%shared>
and the directory in question is set to use Mason on .png files.

The dhandler is running, as I can reach the die message (by uncommenting that line).

But no image is displayed: firefox displays the file name requested (whatever.png); IE shows a broken image.

The related Mason book example on "generating something besides html" mentions  $r->send_http_header, which I seems to be an Apache1.x-ism. I'm using Apache 2, which I suspect is relevant. The Apache2.x docs seem to imply that  $r->send_http_header isn't necessary, that is, that just calling  $r->content_type is sufficient.

In short, I'm not getting my red square.

I am not sure if my error is Mason or Apache or what.

Any ideas, wise monks?

Thanks!

water

Replies are listed 'Best First'.
Re: Generating pngs with Mason
by Ovid (Cardinal) on Dec 04, 2005 at 01:51 UTC

    I don't think this is what you're wanting, but on the off chance you're trying to embed the image data directly in a Web page rather than just linking to an image, you can check out the "data" URL scheme:

    <img src="data:image/gif;base64,R0lGODdhMAAwAPAAAAAAAP///ywAAAAAMAAw AAAC8IyPqcvt3wCcDkiLc7C0qwyGHhSWpjQu5yqmCYsapyuvUUlvONmOZtfzgFz ByTB10QgxOR0TqBQejhRNzOfkVJ+5YiUqrXF5Y5lKh/DeuNcP5yLWGsEbtLiOSp a/TPg7JpJHxyendzWTBfX0cxOnKPjgBzi4diinWGdkF8kjdfnycQZXZeYGejmJl ZeGl9i2icVqaNVailT6F5iJ90m6mvuTS4OK05M0vDk0Q4XUtwvKOzrcd3iq9uis F81M1OIcR7lEewwcLp7tuNNkM3uNna3F2JQFo97Vriy/Xl4/f1cf5VWzXyym7PH hhx4dbgYKAAA7" alt="Larry">

    Cheers,
    Ovid

    New address of my CGI Course.

      Thanks, Ovid!

      The PNG is short enuff to stuff in the tag itself as you suggested; works like a charm.

      I could struggle to figure out what was wrong with the dhanlder, but this moves me along, so I'll go with the simple.

      thanks!

        IE is certainly a consideration one should ponder and after giving this quite a bit of thought, I can only conclude

        I friggin' hate IE!

        What a rotten, stinkin' miserable piece of crap!

        I hate that browser with a passion. In all fairness, I'm not as "anti-Microsoft" as most of my compatriots but when it comes to IE, my zealotry is limitless. I hate that damned browser. It's caused me no end of pain in its lack of support for standards. Unfortunately, as the lowest common denominator, we've no choice but to pander to it. How incredibly frustrating.

        Side note: anyone else notice how they said their users don't want tabs but they're adding them in the latest version?.

        Cheers,
        Ovid

        New address of my CGI Course.

        Hmmm ... I hit 50,000 XP (archbishop) when I posted this. Maybe the universe is trying tell me something. Damned if I know what it's saying, though.

Re: Generating pngs with Mason
by BUU (Prior) on Dec 04, 2005 at 01:44 UTC
    What is the *exact* output the server returns to you, including HTTP headers and so forth? You should be able to use something like LWP or netcat to get this.

    If you run your GD code and print to a file, can you display that file?

    What does the error log say?
      yes, the GD code to a file works fine.

      the issue is headers, I think: the response is a 400 code:

      GET /test/sss.png HTTP/1.1 HTTP/1.1 400 Bad Request Date: Sun, 04 Dec 2005 01:49:37 GMT Server: Apache/2.0.49 (Gentoo/Linux) mod_perl/2.0.1 Perl/v5.8.2 Vary: Accept-Encoding Content-Length: 346 Connection: close Content-Type: text/html; charset=iso-8859-1 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>400 Bad Request</title> </head><body> <h1>Bad Request</h1> <p>Your browser sent a request that this server could not understand.< +br /> </p>
        And the error log?
Re: Generating pngs with Mason
by fmerges (Chaplain) on Dec 04, 2005 at 10:36 UTC

    Hi,

    Try this:

    <%init> use Apache::Constants qw(NOT_FOUND); use GD; my ($file) = $m->dhandler_arg =~ /(\w+\.png)$/; $m->clear_and_abort(NOT_FOUND) unless $file; return if $m->cache_self( key => $file ); $r->content_type('image/png'); my $image = new GD::Image(100, 100); my $red = $image->colorAllocate(255, 0, 0); $image->filledRectangle( 25, 25, 75, 75, $red); $m->clear_buffer; print $image->png; </%init>

    Regards,

    fmerges at irc.freenode.net

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-25 09:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found