How to display image from RAM with some text around

A

Asterbing

Hello,

I would like to display a serial of tiny images (as the one here in $img
of 55 bytes) from RAM, without to go through any temporary disk file
(unless if possible to create a ramdisk with some lines of Perl ;-)).
So, I've tried this :

#!/usr/bin/perl -T
use strict;
use warnings;

my $img = pack("C*", 0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x08, 0x00,
0x08, 0x00, 0x91, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xA6, 0xCA, 0xF0, 0x2A,
0x5F, 0xFF, 0x00, 0x00, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00,
0x08, 0x00, 0x00, 0x02, 0x10, 0x8C, 0x7F, 0xA2, 0x3B, 0xB1, 0xEC, 0x9E,
0x68, 0x72, 0xC6, 0x47, 0x65, 0x1B, 0xBC, 0x8F, 0x02, 0x00, 0x3B);

print "Content-type: text/html\n\n";
print "<p>This is an image :</p>";
print $img;
print "<p>That's all !</p>";
exit 0;

If I remove the "... This is an image ..." line, the image is well shown
in browser, but the last line of text is ignored.

If I let the "... This is an image ..." line, I get the sentence,
then the image content as text and the last line "That's all".

Maybe ideal would be to go through something like "print <img
src="display_image_value.cgi"></img>"; with POSted image (while
impossible to pass image data through GET in url itself), but how to
"simulate" a POST without any form and its action ?

Am I on the wrong way(s) ? How to do ?
 
B

Brian Wakem

Asterbing said:
Hello,

I would like to display a serial of tiny images (as the one here in $img
of 55 bytes) from RAM, without to go through any temporary disk file
(unless if possible to create a ramdisk with some lines of Perl ;-)).
So, I've tried this :

#!/usr/bin/perl -T
use strict;
use warnings;

my $img = pack("C*", 0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x08, 0x00,
0x08, 0x00, 0x91, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xA6, 0xCA, 0xF0, 0x2A,
0x5F, 0xFF, 0x00, 0x00, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00,
0x08, 0x00, 0x00, 0x02, 0x10, 0x8C, 0x7F, 0xA2, 0x3B, 0xB1, 0xEC, 0x9E,
0x68, 0x72, 0xC6, 0x47, 0x65, 0x1B, 0xBC, 0x8F, 0x02, 0x00, 0x3B);

print "Content-type: text/html\n\n";
print "<p>This is an image :</p>";
print $img;
print "<p>That's all !</p>";
exit 0;

If I remove the "... This is an image ..." line, the image is well shown
in browser, but the last line of text is ignored.

If I let the "... This is an image ..." line, I get the sentence,
then the image content as text and the last line "That's all".

Maybe ideal would be to go through something like "print <img
src="display_image_value.cgi"></img>"; with POSted image (while
impossible to pass image data through GET in url itself), but how to
"simulate" a POST without any form and its action ?

Am I on the wrong way(s) ? How to do ?


An html page does not contain image data, it contains urls of images, which
your browser then fetches.

You can indeed use <img src="myimage.cgi"> where myimage.cgi prints your
image data, but you must print the correct header first.
 
X

xhoster

Asterbing said:
Hello,

I would like to display a serial of tiny images (as the one here in $img
of 55 bytes) from RAM, without to go through any temporary disk file
(unless if possible to create a ramdisk with some lines of Perl ;-)).
So, I've tried this :

#!/usr/bin/perl -T
use strict;
use warnings;

my $img = pack("C*", 0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x08, 0x00,
0x08, 0x00, 0x91, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xA6, 0xCA, 0xF0, 0x2A,
0x5F, 0xFF, 0x00, 0x00, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00,
0x08, 0x00, 0x00, 0x02, 0x10, 0x8C, 0x7F, 0xA2, 0x3B, 0xB1, 0xEC, 0x9E,
0x68, 0x72, 0xC6, 0x47, 0x65, 0x1B, 0xBC, 0x8F, 0x02, 0x00, 0x3B);

print "Content-type: text/html\n\n";
print "<p>This is an image :</p>";
print $img;
print "<p>That's all !</p>";
exit 0;

If I remove the "... This is an image ..." line, the image is well shown
in browser,

If you use an actual web-browser, the image is not shown, because you
explictly told it you were sending html, not an image. If you use IE, it
might be shown as an image. But then again, IE might do just about
anything.
but the last line of text is ignored.

Like I said, IE might do just about anything.
If I let the "... This is an image ..." line, I get the sentence,
then the image content as text and the last line "That's all".

Maybe ideal would be to go through something like "print <img
src="display_image_value.cgi"></img>"; with POSted image (while
impossible to pass image data through GET in url itself)

It is possible to pass image data through GET if you encode it first.
Am I on the wrong way(s) ? How to do ?

You could use the data uri, but it doesn't work with IE.

my $png = get_image_binary_somehow();
my $u=URI->new("data:");
$u->media_type("image/png");
$u->data($png);
print qq{<img src="$u" alt="Use Fire Fox (or other RFC 2397 compliant
browser) to see images">};

Xho
 
X

xhoster

It is possible to pass image data through GET if you encode it first.


You could use the data uri, but it doesn't work with IE.

my $png = get_image_binary_somehow();
my $u=URI->new("data:");
$u->media_type("image/png");
$u->data($png);
print qq{<img src="$u" alt="Use Fire Fox (or other RFC 2397 compliant
browser) to see images">};

Following up to myself here. I hacked up a cgi which will render the
inline image data for you, so it works (with some mods) on non RFC 2397
browsers. It requires a round trip to the server for each image, but
the server doesn't need to persist the data to be rendered, as the client
provided it each time.

This auxillary cgi needs to be in runnable:

$ cat exploder_render.cgi
#!/usr/bin/perl -T
use strict;
use warnings;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use URI;
## This script allows you to use the data URI method (with some modifications)
## in browsers that do not support data URI
my $u = new URI CGI::param('data');
print CGI::header($u->media_type);
print $u->data;


Now you do this:
cat image_test.cgi
#!/usr/bin/perl -T
use strict;
use warnings;
use URI;
use URI::Escape;

my $img = pack("C*", 0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x08, 0x00,
0x08, 0x00, 0x91, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xA6, 0xCA, 0xF0, 0x2A,
0x5F, 0xFF, 0x00, 0x00, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00,
0x08, 0x00, 0x00, 0x02, 0x10, 0x8C, 0x7F, 0xA2, 0x3B, 0xB1, 0xEC, 0x9E,
0x68, 0x72, 0xC6, 0x47, 0x65, 0x1B, 0xBC, 0x8F, 0x02, 0x00, 0x3B);

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

my $u=URI->new("data:");
$u->media_type("image/png");
$u->data($img);

## works only on RFC2397
print qq{<img src="$u" alt="Use Fire Fox (or other RFC 2397 compliant
browser) to see images">};

print "<hr>";

## works everywhere, at the expense of a server trip per image.
my $new_u = "exploder_render.cgi?data=". uri_escape($u);
print qq{<img src="$new_u" alt="should work on any browser">};

print "<p>That's all !</p>";
exit 0;



Xho
 
M

Michele Dondi

You could use the data uri, but it doesn't work with IE.

I have also seen using JavaScript for this, but I don't have the
slightest idea how portable it could be, and one has to have JS
enabled in any case, which... may not be the case!


Michele
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top