By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,133 Members | 1,092 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,133 IT Pros & Developers. It's quick & easy.

Storing jpgs

P: n/a
C G
Dear All,

What's the best way to store jpgs in postgresql to use in a web page?

I tried to use large objects, but how would you extract them from a table to
be viewed in a web-page without having to write them to a scratch file
somewhere first?

Thanks

Colin

__________________________________________________ _______________
Stay in touch with absent friends - get MSN Messenger
http://www.msn.co.uk/messenger
---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
I've found bytea works better for me than large objects.

As far as how to retrieve and display, that depends. What scripting
language are you using?
-----Original Message-----
From: pg*****************@postgresql.org
[mailto:pg*****************@postgresql.org]On Behalf Of C G
Sent: Monday, April 05, 2004 12:20 PM
To: pg***********@postgresql.org
Subject: [GENERAL] Storing jpgs
Dear All,

What's the best way to store jpgs in postgresql to use in a web page?

I tried to use large objects, but how would you extract them from
a table to
be viewed in a web-page without having to write them to a scratch file
somewhere first?

Thanks

Colin

__________________________________________________ _______________
Stay in touch with absent friends - get MSN Messenger
http://www.msn.co.uk/messenger
---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)


---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #2

P: n/a
On Mon, 5 Apr 2004, C G wrote:
Dear All,

What's the best way to store jpgs in postgresql to use in a web page?

I tried to use large objects, but how would you extract them from a table to
be viewed in a web-page without having to write them to a scratch file
somewhere first?


There are several ways to skin this cat, and your choice depends largely
on what type of system you'll be deploying.

Will you have more than one front end server? If so, will they be able to
share a network file system mount for the files? Then the preferred
method for many people is to store the jpg in the file system with a path
in the database. If you can't mount the same point from multiple servers
(or don't want to) then you'll need to store them in the database.

However, maybe you want to be able to update multiple jpegs at the same
time in a transaction? then storing them in either a bytea field or
base64 encoded in a text field will work well.

Storing them as base64 or as a path with a file system is likely more
portable than using large objects. Also, you have to dump large
objects seperately, so your backup process may be more complicated than
you want.

As for displaying them whether you store them as bytea, base64 encoded
text, or large objects, most languages will allow you to build and deliver
an image without having to write it to some temporary place.

Here's a simple example from PHPBuilder on doing it with the file path in
the database, and using a directory of files that may lie outside the
documentroot of apache:

http://www.phpbuilder.com/board/show...5#post10497815
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #3

P: n/a
I'm one for using base64 encoded text in a text field. It's easy to
deal with queries, it's easy to deal with spitting out images, and it's
easy to back up. Others do it differently, there are many ways to skin
a cat, and each person who skins cats most likely thinks their way is
best.

Gavin

C G wrote:
Dear All,

What's the best way to store jpgs in postgresql to use in a web page?

I tried to use large objects, but how would you extract them from a
table to be viewed in a web-page without having to write them to a
scratch file somewhere first?

Thanks

Colin

__________________________________________________ _______________
Stay in touch with absent friends - get MSN Messenger
http://www.msn.co.uk/messenger
---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)


---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 23 '05 #4

P: n/a
Would anyone have some example code they could share using libpq to
encode an image into a text field? Right now, I'm converting my image
into a hexadecimal string representation in my SQL statement. I'm sure
there must be a better (faster) way. The hex encodeing/decoding slows
things down for me since my app deals with a lot of images.

On Apr 5, 2004, at 2:03 PM, Gavin M. Roy wrote:
I'm one for using base64 encoded text in a text field. It's easy to
deal with queries, it's easy to deal with spitting out images, and
it's easy to back up. Others do it differently, there are many ways
to skin a cat, and each person who skins cats most likely thinks their
way is best.
Gavin

C G wrote:
Dear All,

What's the best way to store jpgs in postgresql to use in a web page?


---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #5

P: n/a
Joe Lester wrote:
Would anyone have some example code they could share using libpq to
encode an image into a text field? Right now, I'm converting my image
into a hexadecimal string representation in my SQL statement. I'm sure
there must be a better (faster) way. The hex encodeing/decoding slows
things down for me since my app deals with a lot of images.


Is this what you're looking for?:
http://www.postgresql.org/docs/7.4/s...C-ESCAPE-BYTEA

I don't have actual numbers to support it, but I would expect that using
Postgres' escape function will be faster since it only escapes what
absolutely must be escaped.

HTH

--
Bill Moran
Potential Technologies
http://www.potentialtech.com
---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postgresql.org so that your
message can get through to the mailing list cleanly

Nov 23 '05 #6

P: n/a
What language? Here's a quick example in php that expects a HTML Form
that has a input type=file name=userfile:

<?php

// assumes pg_Connect has been called and the connection is $conn

// check to make sure it's an uploaded file
|if (is_uploaded_file(|$_FILES['userfile']['tmp_name']|))
{
// Get Image Information for JPEG Validation
$imgInfo = getimagesize(|$_FILES['userfile']['tmp_name']);

// Make sure it's a JPEG before moving on
if ( $imgInfo[2] == 2 )
{
|// Read the file in to a variable
$fp = fopen(|$_FILES['userfile']['tmp_name']|, "rb");
$textData = base64_encode(fread($fp,
filesize(|$_FILES['userfile']['tmp_name'])));
fclose($fp);

/* Insert into into a table called media with the following schema:
filename varchar(30) not null primary key
mimetype varchar(30) not null default 'image/jpeg'
filedata text
*/
pg_Query($conn, "INSERT INTO media VALUES('" .
$_FILES['userfile']['name'] . "','image/jpeg', '$textData');
} else {
echo "Uploaded file isn't a valid JPEG.\n";
}
} else {
echo "Invalid file upload.\n";
}
}
|
?>
|
And to send it:

<?php
// assumes a GET variable called image
(http://yoursite.com/displayImage.php?image=picture.jpg and
// pg_Connect already called with $conn as connection

$result = pg_Query($conn, "SELECT mimetype, filedata FROM media WHERE
filename = '" . $_GET['image'] . "';");
if ( pg_NumRows($result) > 0 )
{
$data = pg_Fetch_Object($result, 0);
Header("Content-type: $data->mimetype");
echo base64_decode($data->filedata);
exit();
} else {
echo "404: File Not Found.";
}
?>

Hope this helps, I've not tested it but it should work ok and at best it
illustrates the principles.

Gavin
Joe Lester wrote:
Would anyone have some example code they could share using libpq to
encode an image into a text field? Right now, I'm converting my image
into a hexadecimal string representation in my SQL statement. I'm sure
there must be a better (faster) way. The hex encodeing/decoding slows
things down for me since my app deals with a lot of images.

On Apr 5, 2004, at 2:03 PM, Gavin M. Roy wrote:
I'm one for using base64 encoded text in a text field. It's easy to
deal with queries, it's easy to deal with spitting out images, and
it's easy to back up. Others do it differently, there are many ways
to skin a cat, and each person who skins cats most likely thinks
their way is best.
Gavin

C G wrote:
Dear All,

What's the best way to store jpgs in postgresql to use in a web page?


---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 23 '05 #7

P: n/a
Rod K wrote:
I've found bytea works better for me than large objects.


Speaking of BYTEA, what is the recommended maximum sizes for data to be
put in there. I've played with megabytes-sized BYTEA and the memory
requirement is pretty huge... Is it possible for BYTEA to be efficient
and supporting chunk processing in the future? Is BYTEA and TEXT the
future of all lobs?

--
dave
---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.