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

Dear brothers and sisters,
I have a problem with display a generated image, which is not saved on disk, at Mason generated page. My code is:
<& Elements/Header &> %$r->content_type('image/png'); %$r->send_http_header(); %$m->out($image); <& Elements/Footer &> <%INIT> my $image = generate_image(type => 'png'); </%INIT>
Maybe someone knows how define content type for this image if it should display between header and footer? Because if I run script with my code it makes an effort to redefine main content type of page (text/html) and display image without header and footer.
I wouldn't like to do this feature:
<img src=my_script_which_produce_image.pl>
I'd like to display a generated image directly.

TIA

      
--------------------------------
SV* sv_bless(SV* sv, HV* stash);

Replies are listed 'Best First'.
Re: Display a generated image using Mason
by simon.proctor (Vicar) on Jun 09, 2003 at 21:41 UTC
    I've only just started using Mason myself but one thing I picked up from the docs is that you should put all your code into the INIT block and then immediately call $m->abort(OK). If you wish to dynamically generate an image that is.

    Apparently this prevents any additional whitespace being sent. In otherwords you set the content type to the type of the image, print it and immediately quit. Looking at your code you seem to want to have header and footer component calls. If this is correct then I reckon that won't work for you as you'll be mixing content types as image requests form their own HTTP transaction.

    In other words the browser grabs the html, figures out the resources (CSS, images etc) and grabs each in turn.

    The only way you can display your image directly is to type the script name into the url bar of your browser and code according to the method I have described. Otherwise you are stuck with HTML references (anchors et al).

    HTH in some small way :)

    SP
Re: Display a generated image using Mason
by flounder99 (Friar) on Jun 09, 2003 at 17:10 UTC
    Look at URI::data
    I'm not that familiar with Mason but with CGI you can do something like this:
    #assuming $image contains the gif and $q is a CGI object use URI; my $u = URI->new("data:"); $u->media_type("image/gif"); $u->data($image); print $q->img(-src => "$u");
    Note: this code is untested and not all browsers can handle this.

    --

    flounder

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Display a generated image using Mason
by ctilmes (Vicar) on Jun 09, 2003 at 18:10 UTC
    Try something like this:
    $r->content_type('image/png'); $m->clear_buffer; $m->out($image); $m->abort(200);
    A reply falls below the community's threshold of quality. You may see it by logging in.