For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > August 2005 > JPEG and Perl









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author JPEG and Perl
CSUIDL PROGRAMMEr

2005-08-30, 6:58 pm

Folks
I am new to perl
ALl want to do is to display a image on my html page using perl .
Here is my code

#!/usr/bin/perl
print "Content-type:image/jpeg\n\n";
print " </html>";
print "<body>";
print " <IMG SRC='emb.jpeg'> " ;
print "</body>";
print"</html>";
Is it right or am i missing something
Thanks

John Bokma

2005-08-30, 6:58 pm

"CSUIDL PROGRAMMEr" <syedamjad_a@yahoo.com> wrote:

> Folks
> I am new to perl
> ALl want to do is to display a image on my html page using perl .
> Here is my code
>
> #!/usr/bin/perl


use strict;
use warnings;

> print "Content-type:image/jpeg\n\n";


use CGI;

> print " </html>";
> print "<body>";
> print " <IMG SRC='emb.jpeg'> " ;
> print "</body>";
> print"</html>";
> Is it right or am i missing something


If your Content-Type is a jpeg image, why are you sending out HTML?

--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html

Jürgen Exner

2005-08-30, 6:58 pm

CSUIDL PROGRAMMEr wrote:
> Folks
> I am new to perl
> ALl want to do is to display a image on my html page using perl .
> Here is my code


As far as Perl is concerned some comments below

> #!/usr/bin/perl


You are missing
use strict;
use warnings;

> print "Content-type:image/jpeg\n\n";
> print " </html>";
> print "<body>";
> print " <IMG SRC='emb.jpeg'> " ;
> print "</body>";
> print"</html>";
> Is it right or am i missing something


Although technically there is nothing wrong with your Perl code (it will
print what you are telling it to print) I would replace the phletoria of
print statements with a single "here" document:

use warnings;
print <<EOT;
Content-type:image/jpeg

</html>
<body>
<IMG SRC='emb.jpeg'>
</body>
</html>
EOT

jue


Paul Lalli

2005-08-30, 6:58 pm

CSUIDL PROGRAMMEr wrote:
> Folks
> I am new to perl


That's okay, because this question doesn't have anything to do with
Perl.

> ALl want to do is to display a image on my html page using perl .
> Here is my code
>
> #!/usr/bin/perl
> print "Content-type:image/jpeg\n\n";


Here you are telling the client (the webbrowser) that you're about to
send a JPEG - binary data.

> print " </html>";
> print "<body>";


Here, you start sending the client *text*, not a JPEG.

> print " <IMG SRC='emb.jpeg'> " ;
> print "</body>";
> print"</html>";
> Is it right or am i missing something


You are missing something. If you want to send an HTML page that
contains an image source link, as you seemed to do above, tell the
client you're sending HTML:

print "Content-type: text/html\n\n";

If you want to send *just* the actual JPEG file, not an HTML page that
contains a link to the file, send the actual file:

[UNTESTED]
if (! open my $img, '<', 'emb.jpeg') {
print "Content-type: text/plain\n\n";
print "Error attempting to open emb.jpeg: $!\n";
} else {
binmode $img;
print "Content-type: img/jpeg\n\n";
my $buffer;
while (my $bytes = read ($img, $buffer, 1024) {
print $buffer;
}
}

Hope this helps,
Paul Lalli

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com