You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by John Michael <jo...@acadiacom.net> on 2002/01/15 12:54:44 UTC

Printing an image with a script.

I'm trying to print an image to the browser.
I've had no luck using the code below with cgi and was wondering if it would be easier or even possible with mod-perl.
I'm trying to write a script to keep the bandwidth usage.
I call the script with an Action handler.

I found this sniplet of code in one of my books for cgi, but it does not work properly.

my $image_file = $ENV{'PATH_TRANSLATED'};

($file_size) = (stat($image_file))[7];

my (@array) = split(/\//,$ENV{'PATH_TRANSLATED'});
$return_image = pop @array;
($name, $extention) = split(/\./,$return_image);

print "Content-type: image/$extention", "\n";
print "Content-length: ", $file_size, "\n\n";

open(FILE, "<" . $image_file);
 while (<FILE>){
  print;
 }
close (FILE);

I have tried it and some other code sniplets about 50 ways with not much luck.
It will print a page of thumbs on the first on the first call, but then the browser hangs if you click one of the thumbs.

Thanks
John Michael

Re: Printing an image with a script.

Posted by Todd Finney <tf...@boygenius.com>.
If all he's trying to do is track bandwidth usage for image files, 
wouldn't it be simpler to have the handler record the file size, return 
OK, and let the rest of the server worry about getting it out to the 
user?  Is there a performance difference between that and send_fd()?

Todd



At 08:43 AM 1/15/02, Mark Maunder wrote:
>Try doing this under mod_perl (either using Apache::Registry or in 
>your
>own handler - please see the mod_perl guide for details):
>
>my $fh = Apache::File->new($r->filename);
>$r->send_http_header();
>$r->send_fd($fh);
>return OK; #if not running under Apache::Registry
>
>That works for me. To debug the (non-mod_perl) code below, you might
>want to do a test by printing your headers and image data to the 
>console
>to make sure you're doing what you think you're doing. Also, make
>absolutelly sure that you have to send the image manually rather than
>have your web server handle it. The above code is from a compression
>routine which also handles images.
>
>~mark.
>
>John Michael wrote:
>
> > I'm trying to print an image to the browser.I've had no luck using 
> the
> > code below with cgi and was wondering if it would be easier or even
> > possible with mod-perl.I'm trying to write a script to keep the
> > bandwidth usage.I call the script with an Action handler. I found 
> this
> > sniplet of code in one of my books for cgi, but it does not work
> > properly. my $image_file = $ENV{'PATH_TRANSLATED'}; ($file_size) =
> > (stat($image_file))[7];my (@array) =
> > split(/\//,$ENV{'PATH_TRANSLATED'});
> > $return_image = pop @array;
> > ($name, $extention) = split(/\./,$return_image); print 
> "Content-type:
> > image/$extention", "\n";
> > print "Content-length: ", $file_size, "\n\n";open(FILE, "<" .
> > $image_file);
> >  while (<FILE>){
> >   print;
> >  }
> > close (FILE); I have tried it and some other code sniplets about 50
> > ways with not much luck.It will print a page of thumbs on the first 
> on
> > the first call, but then the browser hangs if you click one of the
> > thumbs. ThanksJohn Michael
>
>
>


Re: Printing an image with a script.

Posted by Uwe Voelker <uw...@gmx.de>.
> open(FILE, "<" . $image_file);


binmode FILE;
binmode STDOUT;

>  while (<FILE>){
>   print;
>  }
> close (FILE);


But I'm not sure if this helps.

Good bye, Uwe



Re: Printing an image with a script.

Posted by John Michael <jo...@acadiacom.net>.
Thanks Mark for the reply.  I want to try it under mod perl.  I'm somewhat
new to mod-perl and like to get things working under cgi first.  I do have
some scripts running and handling content and love it though.  I found the
problem with my code below shortly after posting of course.
I was using the file extention to see what content header I needed to send.
Well when it is a jpeg image, I was sending image/jpg and not image/jpeg and
thus the problem.
It is working fine now and I am testing it and will port it to mod-perl
next.
Thanks
JOhn Michael


----- Original Message -----
From: "Mark Maunder" <ma...@swiftcamel.com>
To: "John Michael" <jo...@acadiacom.net>
Cc: <mo...@apache.org>
Sent: Tuesday, January 15, 2002 7:43 AM
Subject: Re: Printing an image with a script.


> Try doing this under mod_perl (either using Apache::Registry or in your
> own handler - please see the mod_perl guide for details):
>
> my $fh = Apache::File->new($r->filename);
> $r->send_http_header();
> $r->send_fd($fh);
> return OK; #if not running under Apache::Registry
>
> That works for me. To debug the (non-mod_perl) code below, you might
> want to do a test by printing your headers and image data to the console
> to make sure you're doing what you think you're doing. Also, make
> absolutelly sure that you have to send the image manually rather than
> have your web server handle it. The above code is from a compression
> routine which also handles images.
>
> ~mark.
>
> John Michael wrote:
>
> > I'm trying to print an image to the browser.I've had no luck using the
> > code below with cgi and was wondering if it would be easier or even
> > possible with mod-perl.I'm trying to write a script to keep the
> > bandwidth usage.I call the script with an Action handler. I found this
> > sniplet of code in one of my books for cgi, but it does not work
> > properly. my $image_file = $ENV{'PATH_TRANSLATED'}; ($file_size) =
> > (stat($image_file))[7];my (@array) =
> > split(/\//,$ENV{'PATH_TRANSLATED'});
> > $return_image = pop @array;
> > ($name, $extention) = split(/\./,$return_image); print "Content-type:
> > image/$extention", "\n";
> > print "Content-length: ", $file_size, "\n\n";open(FILE, "<" .
> > $image_file);
> >  while (<FILE>){
> >   print;
> >  }
> > close (FILE); I have tried it and some other code sniplets about 50
> > ways with not much luck.It will print a page of thumbs on the first on
> > the first call, but then the browser hangs if you click one of the
> > thumbs. ThanksJohn Michael
>
>
>
>
>


Re: Printing an image with a script.

Posted by Mark Maunder <ma...@swiftcamel.com>.
Try doing this under mod_perl (either using Apache::Registry or in your
own handler - please see the mod_perl guide for details):

my $fh = Apache::File->new($r->filename);
$r->send_http_header();
$r->send_fd($fh);
return OK; #if not running under Apache::Registry

That works for me. To debug the (non-mod_perl) code below, you might
want to do a test by printing your headers and image data to the console
to make sure you're doing what you think you're doing. Also, make
absolutelly sure that you have to send the image manually rather than
have your web server handle it. The above code is from a compression
routine which also handles images.

~mark.

John Michael wrote:

> I'm trying to print an image to the browser.I've had no luck using the
> code below with cgi and was wondering if it would be easier or even
> possible with mod-perl.I'm trying to write a script to keep the
> bandwidth usage.I call the script with an Action handler. I found this
> sniplet of code in one of my books for cgi, but it does not work
> properly. my $image_file = $ENV{'PATH_TRANSLATED'}; ($file_size) =
> (stat($image_file))[7];my (@array) =
> split(/\//,$ENV{'PATH_TRANSLATED'});
> $return_image = pop @array;
> ($name, $extention) = split(/\./,$return_image); print "Content-type:
> image/$extention", "\n";
> print "Content-length: ", $file_size, "\n\n";open(FILE, "<" .
> $image_file);
>  while (<FILE>){
>   print;
>  }
> close (FILE); I have tried it and some other code sniplets about 50
> ways with not much luck.It will print a page of thumbs on the first on
> the first call, but then the browser hangs if you click one of the
> thumbs. ThanksJohn Michael