Re: [GENERAL] How to display stored image on web page?

From: Gilles Darold <darold(at)neptune(dot)fr>
To: bfd(at)att(dot)net
Cc: pgsql-general(at)postgreSQL(dot)org, ScotB(at)fine(dot)com
Subject: Re: [GENERAL] How to display stored image on web page?
Date: 1999-01-18 11:43:44
Message-ID: 36A31E70.4B64FA87@neptune.fr
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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">

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:

> 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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Kevin Lo 1999-01-18 14:03:21 Re: [GENERAL] Postgres' DBI(Pg) problems.
Previous Message Rimi Lehn 1999-01-18 09:10:48 [GENERAL] Postgres' DBI(Pg) problems.