Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to output image from a perl script 1

Status
Not open for further replies.
Your perl script needs to output a header for an image adn then echo back the data for that image directly.
Code:
open FH, "photo_${photoid}.jpg" or handle_error();
print "Content-type: image/jpeg\n\n";
while(<FH>) {
  print;
}
close FH;
Assuming the filenames are photo_100.jpg and similar and that $photoid holds that 100.


Trojan.
 
I did something like this, but gives me "premature end of script headers"

Can u help?

#!/usr/bin/perl

open FH, "/shop/images/full.gif" or handle_error();
print "Content-type: image/gif\n\n";
while(<FH>) {
print;
}
close FH;
 
I just checked it on my system and it works fine for me.
My code:
Code:
#!/usr/bin/perl -w

use strict;

open FH, "/var/[URL unfurl="true"]www/icons/PythonPowered.png"[/URL] or die "Failed to load image";
print "Content-type: image/png\n\n";
while(<FH>) {
  print;
}
close FH;


Trojan.
 
Are you sure your file is opening the file correctly?
I would suggest that your code is possibly executing the "handle_error()".


Trojan.
 
No,
Not if it's on a different webserver.
You'll need to read the image from the other webserver and echo it out as shown before.
You'll need to use somthing like "use LWP::UserAgent;" to access the other webserver.
Code:
my $ua = LWP::UserAgent->new;
my $response = $ua->get(’[URL unfurl="true"]http://www.xyz.com/shop/images/full.gif’);[/URL]
if ($response->is_success) {
    print $response->content;  # or whatever
}
else {
    handle_error($response->status_line);
}
Something like that maybe.




Trojan.
 
premature end of script headers" error, am I doing something wrong?

#!/usr/bin/perl

my $ua = LWP::UserAgent->new;
my $response = $ua->get(’print "Content-type: image/gif \n\n";
if ($response->is_success) {
print $response->content; # or whatever
}else{
print $response->status_line;
}
 
You are missing the "use LWP::UserAgent;" line at the top of your code. Add that and then test your code with "perl -c photo.pl" to ensure it compiles and should run.


Trojan.
 
It worked .. it worked!! Thank you very much!!

I added the "use" thing had problem again.
The problem was with ftp, file was transferd from desktop to server in binary mode and some extra stuff was added after each line.

Thank you.
 
Of course it worked!
;-)
Now you need to consider your error checking very carefully.


Trojan.
 
I would also point out that you should not really be pulling data from a server unless you have permission.
I hope that the server where the images are is yours or you have permission to pull images from it.


Trojan.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top