How to display stored image on web page?

Started by Bernieabout 27 years ago4 messagesgeneral
Jump to latest
#1Bernie
bfb@att.net

Now that I've stored several jpg's in my
database as large objects, I'm trying to
display them on a web page using a java servlet.
Im my servlet, I've changed the content type
to image/jpeg - response.setContentType("image/jpg");
When the web page is loaded, all I see is the ID
number of the image. How do you display the
stored image on the web page? An example in ANY
language would be appreciated.

-Thanks

#2Gilles Darold
darold@neptune.fr
In reply to: Bernie (#1)
Re: [GENERAL] How to display stored image on web page?

Hi,

You just have to use the read() function directly into your image field.
I think
there is an example into the documentation, perhaps the programmers
guide ?
If not I can send you a perl script which is doing this kind of stuff!

Regards,

Bernie wrote:

Show quoted text

Now that I've stored several jpg's in my
database as large objects, I'm trying to
display them on a web page using a java servlet.
Im my servlet, I've changed the content type
to image/jpeg - response.setContentType("image/jpg");
When the web page is loaded, all I see is the ID
number of the image. How do you display the
stored image on the web page? An example in ANY
language would be appreciated.

-Thanks

#3Oleg Broytmann
phd@sun.med.ru
In reply to: Bernie (#1)
Re: [GENERAL] How to display stored image on web page?

Hello!

On Thu, 14 Jan 1999, Bernie wrote:

Now that I've stored several jpg's in my
database as large objects, I'm trying to
display them on a web page using a java servlet.
Im my servlet, I've changed the content type
to image/jpeg - response.setContentType("image/jpg");
When the web page is loaded, all I see is the ID
number of the image. How do you display the
stored image on the web page? An example in ANY
language would be appreciated.

Looks like you show ID instead of the very image. To pass an image you
should export it from database first as large object. Look into large
objects docs...

Oleg.
----
Oleg Broytmann National Research Surgery Centre http://sun.med.ru/~phd/
Programmers don't die, they just GOSUB without RETURN.

#4Gilles Darold
darold@neptune.fr
In reply to: Bernie (#1)
Re: [GENERAL] How to display stored image on web page?

Hi,

After some request on How to display images stored as large Object with
example
I give you this perl script which is doing this very well. Just call it
from your cgi script,
Html pages, or servlet by inserting the following HREF :

<IMG SRC="http://www.monsite.com/cgi-bin/object.pl?n=1&quot;&gt;

Of course you must have perl install in your machine and mod_perl
working with Apache.

object.pl is the name of the perl script bellow, the CGI parameter n=1
is in fact
the id of an autoincrement field I have when I insert a new image. You
can replace
it by the oid of your large object, it may work.

Here the object.pl script :

---------------------------------------------------------------------------------------

#!/usr/bin/perl
###############################################################################

# Script used to retrieved images or any binary file stored in a
PostgreSQL database.
# Param : Id of the object to return
###############################################################################

# Tested with:
# - PostgreSQL-6.3.2
# - apache_1.3.2
# - mod_perl-1.08
# - perl5.004_04

use CGI;
use Pg;
#use strict;

# Create an instance of the CGI package
my $query = new CGI;

# CGI parameters
my $n; # The id of the object in the database
$n = $query->param('n');
if ($n eq "") {
exit(0);
}

# SQL query and DB instance
my $DBNAME = "database_name";
my $SEL_PHOTO = "SELECT lo_export(t_object.o_photo, '/usr/tmp/image$$')
FROM t_object WHERE l_noauto=$n;";

# Connection to the database
my $conn = Pg::setdb("www.mysite.com", "5432", "","", "$DBNAME");
if ($conn->status != PGRES_CONNECTION_OK) {
exit(2);
}

# Retrieve the binary data
my $cmd = $SEL_PHOTO;
my $result = $conn->exec($cmd);
if ($result->resultStatus != 2) {
&errmsg_select(1, $result->resultStatus, $cmd);
}
my $ntuples = $result->ntuples;
if ($ntuples == 0) {
exit(2);
}

# Print the right header for images (this works for jpg and gif as well)

print $query->header(-type=>'image/jpeg', -expires=>'-1d');

# Now read the file and return its datas on the fly
open(PHOTO, "/usr/tmp/image$$") || die "error opening file
/usr/tmp/image$$\n";
while (<PHOTO>) {
print $_;
}
close(PHOTO);
# delete the temporary file
unlink("/usr/tmp/image$$");
---------------------------------------------------------------------------------------

Hope this help,

Good work...

Gilles Darold

NB: Some other question asked to me :
- Also, which do you like better, using the db to store the images or
disk?
Storing image on disk is more easy and speed, so if I store my images
into the
database, this is because I want a centralized administration.
- Have you tried using http to upload images via the browser directly
into the db?
Yes, all images stores are sent via and HTML Interface by users. For
this I also use
mod_perl. Note that this is why I have inserted an expiration time
(asap) in the header
of the previous script, because changing images need refresh.

Bernie wrote:

Show quoted text

Now that I've stored several jpg's in my
database as large objects, I'm trying to
display them on a web page using a java servlet.
Im my servlet, I've changed the content type
to image/jpeg - response.setContentType("image/jpg");
When the web page is loaded, all I see is the ID
number of the image. How do you display the
stored image on the web page? An example in ANY
language would be appreciated.

-Thanks