You are viewing a plain text version of this content. The canonical link for it is here.
Posted to embperl@perl.apache.org by Daniel Barbar <db...@legato.com> on 2002/10/10 02:02:36 UTC

Generating images on-the-fly (newbie question)

Hi,

	I'd like to generate web-enabled reports (graphs, actually, using
the Perl module GD::Graph) on data from a database using
Apache/mod-perl/Embperl(1.3.4)/DBI/DBD-Oracle.
	I can generate the image and "print" it to a file handle (in
different formats, namely GIF, JPEG, PNG), which will allow me to save it to
a temporary file and then "call" that file in the HTML code using the <img>
tag. However, I'd rather not do that, because of all the complications of
having to deal with temporary files, sessions, clean-up, etc.
	Is there a way, using Embperl, to send the image back to the browser
on-the-fly, that is, without having to save it on a file first? Something
along the lines of (if you'd allow me to write such aberration):

<img src=[- print OUT $gd->png -]>

	I'm aware that the code above isn't HTML, but perhaps it served the
purpose of clarifying what I want to do. Thanks in advance for your help,

---
Daniel Barbar


Re: Generating images on-the-fly (newbie question) / embperl object

Posted by Andrew O'Brien <an...@switchonline.com.au>.
On Wed, Oct 09, 2002 at 10:47:23PM -0700, Wim Kerkhoff wrote:
> Last week I tried a couple of ways of doing this under EmbperlObject.
> That is, from somepage.html try to ignore the output of base.html and
> set the headers and content manually, while still having access to
> things bound to the request object $req.
> 
> Is that even possible? I ended up configuring a /cgi-bin and creating a
> CGI.pm script to dump out my binary data.

(This more or less assumes 1.3.4 as I haven't played with 2.0 yet)

You need a base.html that doesn't output anything itself. Three main
cases here (I'm sure others can contribute alternatives):

1. Simple case of embperl object and you not needing anything from
   init.html:

  - create a subdir with a very simple base.html that contains only
    [- Execute('*') -]
  - your somepage.html then does its own output and all is good

2. Your case of needing things from init.html (I'm assuming that
   init.html is a "global" file and does once-only setup):

   - create a $webbase/common directory and put init.html in there
   - set EMBPERL_OBJECT_ADDPATH to $webbase/common
   - create a normal subdir with an almost as simple base.html
     [- Execute('init.html'); Execute('*'); -]
   - your somepage.html then does its own output and all is good

3. If init.html needs to be different in different parts of the site
then things start getting more complex and you'll have to either
   - copy init.html into the new subdir
   - move things into your own module and use it from somepage.html
   - use EMBPERL_OBJECT_HANDLER_CLASS and have the functions in $req
     already
   - ...

> init.html:
> 
> [- $req = shift; $req->{dbh} = DBI ...; -]
> 
> 
> base.html:
> 
> <html>
> [- Execute ('*') -]
> </html>

Your image data will be surrounded in <html> tags here.

> somepage.html:
> 
> [-
> $req = shift;
> $dbh = $req->{dbh};

$escmode=0;

> $http_headers_out{'Content-Type'} = "image/gif";
> print OUT $dbh->selectrow_array("select imageblob from images where
> id=234");	
> -]

-- 
 Andrew O'Brien
 Senior Engineer                       email: andrewo@switchonline.com.au.
 Switch Online Group Pty Limited       phone: +61 2 9299 1133
 ABN 89 092 286 327                    fax:   +61 2 9299 1134

---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org


Re: Generating images on-the-fly (newbie question) / embperl object

Posted by Gerald Richter <ri...@ecos.de>.
>
> As it sits, my seperate .cgi is working... but I can't handle errors
> nicely, other then calling HTML::Embperl::Execute('error.html').
> However, when I do that then all the EmbperlObject stuff gets ignored.
> See the problem?  As soon as you set up another file extension using
> standard Embperl, then you can't switch back, other then using the
> Location: header to redirect.
>

In this case you really need to do something like Andrew described, or put
at the top of your base.epl

[$if $fdat{image} $][- Execute('*') ; exit ; -][$endif$]

make sure to have no spaces outside of the braces before the exit. Of course
you can use another condition to determinate if it is an image

Gerald


-------------------------------------------------------------
Gerald Richter    ecos electronic communication services gmbh
Internetconnect * Webserver/-design/-datenbanken * Consulting

Post:       Tulpenstrasse 5         D-55276 Dienheim b. Mainz
E-Mail:     richter@ecos.de         Voice:    +49 6133 925131
WWW:        http://www.ecos.de      Fax:      +49 6133 925152
-------------------------------------------------------------




---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org


RE: Generating images on-the-fly (newbie question) / embperl object

Posted by Wim Kerkhoff <wi...@nyetwork.org>.
Thanks 

> My intuitive answer is 'Yes, but fundamentally, you don't 
> want to.  Tell
> apache your file extension for dynamic images should be handled by
> Embperl, not EmbperlObject'.  I believe this is because either the
> difference between Embperl and EmbperlObject is entirely 
> devoted to the
> concept of wrapping pages with a standard template, which is exactly
> what you do not want here, or I'm tired and should go to bed 
> a few hours
> ago.

I do agree... what I was suggesting is really a bad idea from a design
standpoint. On the other hand, EmbperlObject is more about just
templates when you consider things like the application object, using a
Perl syntax, and so on.

The easiest thing in my situation would have been to do as Andrew
suggested, and create a "images/" sub directory which could override
base.html but still pull in the global inititialization routines in
init.html.

As it sits, my seperate .cgi is working... but I can't handle errors
nicely, other then calling HTML::Embperl::Execute('error.html').
However, when I do that then all the EmbperlObject stuff gets ignored.
See the problem?  As soon as you set up another file extension using
standard Embperl, then you can't switch back, other then using the
Location: header to redirect.

Wim


---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org


RE: Generating images on-the-fly (newbie question) / embperl object

Posted by Ed Grimm <ed...@asgard.rsc.raytheon.com>.
On Wed, 9 Oct 2002, Wim Kerkhoff wrote:

<snip>
>
> Last week I tried a couple of ways of doing this under EmbperlObject.
> That is, from somepage.html try to ignore the output of base.html and
> set the headers and content manually, while still having access to
> things bound to the request object $req.
>
> Is that even possible? I ended up configuring a /cgi-bin and creating a
> CGI.pm script to dump out my binary data.

My intuitive answer is 'Yes, but fundamentally, you don't want to.  Tell
apache your file extension for dynamic images should be handled by
Embperl, not EmbperlObject'.  I believe this is because either the
difference between Embperl and EmbperlObject is entirely devoted to the
concept of wrapping pages with a standard template, which is exactly
what you do not want here, or I'm tired and should go to bed a few hours
ago.

The second is probably true in any event.

Ed


---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org


RE: Generating images on-the-fly (newbie question) / embperl object

Posted by Wim Kerkhoff <wi...@nyetwork.org>.
> > 	Is there a way, using Embperl, to send the image back 
> to the browser 
> > on-the-fly, that is, without having to save it on a file first? 
> > Something
> 
> This is something that comes up rather frequently. For future 
> reference, doing a search of the embperl mail archives
> (http://www.ecos.de/~mailarc/embperl/) is usually useful.
> 
> You can output a gif directly from embperl so:
> 
> <img src="graph.html?args=go&here=">

Last week I tried a couple of ways of doing this under EmbperlObject.
That is, from somepage.html try to ignore the output of base.html and
set the headers and content manually, while still having access to
things bound to the request object $req.

Is that even possible? I ended up configuring a /cgi-bin and creating a
CGI.pm script to dump out my binary data.

init.html:

[- $req = shift; $req->{dbh} = DBI ...; -]


base.html:

<html>
[- Execute ('*') -]
</html>

somepage.html:

[-
$req = shift;
$dbh = $req->{dbh};
$http_headers_out{'Content-Type'} = "image/gif";
print OUT $dbh->selectrow_array("select imageblob from images where
id=234");	
-]


---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org


Re: Generating images on-the-fly (newbie question)

Posted by Andrew O'Brien <an...@switchonline.com.au>.
On Wed, Oct 09, 2002 at 05:02:36PM -0700, Daniel Barbar wrote:
> 	I'd like to generate web-enabled reports (graphs, actually, using
> the Perl module GD::Graph) on data from a database using
> Apache/mod-perl/Embperl(1.3.4)/DBI/DBD-Oracle.

> 	Is there a way, using Embperl, to send the image back to the browser
> on-the-fly, that is, without having to save it on a file first? Something

This is something that comes up rather frequently. For future
reference, doing a search of the embperl mail archives
(http://www.ecos.de/~mailarc/embperl/) is usually useful.

You can output a gif directly from embperl so:

<img src="graph.html?args=go&here=">

graph.html:

[-
 $req = shift;
 $http_headers_out{'Content-type'} = 'image/gif';
 $escmode=0; # This is important

 $gd = set up stuff here;

 print OUT $gd->plot(@data)->gif();
-]

Make sure there is no extraneous whitespace in this file as this will
get sent to the browser, ie the first and last characters are [ and
].


-- 
 Andrew O'Brien
 Senior Engineer                       email: andrewo@switchonline.com.au.
 Switch Online Group Pty Limited       phone: +61 2 9299 1133
 ABN 89 092 286 327                    fax:   +61 2 9299 1134

---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org


RE: Generating images on-the-fly (newbie question)

Posted by Wim Kerkhoff <wi...@nyetwork.org>.
Daniel, 

You should be able to do something like this. I haven't tested this
example,
but I've done this before on code I no longer have access too.

In your page: <img src="me.html?your=params&go=here">

In me.html, have a block of Emberl like the following. The first [ will
have to be
the very first character. Otherwise, any whitespace inbetween will
become part of the image
which will corrupt it. 

[- 
$http_headers_out{'Content-Type'} = "image/gif";

$gifdata = .... ; # GD generated image

binmode OUT;
print OUT $gifdata;

exit;
-]



> Is there a way, using Embperl, to send the image back to the browser 
> on-the-fly, that is, without having to save it on a file first? 
> Something along the lines of (if you'd allow me to write such
aberration):
> <img src=[- print OUT $gd->png -]> 
>
> I'm aware that the code above isn't HTML, but perhaps it served the 
> purpose of clarifying what I want to do. Thanks in advance for your
help,


---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org