Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old November 23rd, 2005, 12:27 AM
C G
Guest
 
Posts: n/a
Default 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 majordomo@postgresql.org)

  #2  
Old November 23rd, 2005, 12:27 AM
Rod K
Guest
 
Posts: n/a
Default Re: Storing jpgs

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?
[color=blue]
> -----Original Message-----
> From: pgsql-general-owner@postgresql.org
> [mailto:pgsql-general-owner@postgresql.org]On Behalf Of C G
> Sent: Monday, April 05, 2004 12:20 PM
> To: pgsql-general@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 majordomo@postgresql.org)
>
>[/color]



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

  #3  
Old November 23rd, 2005, 12:27 AM
scott.marlowe
Guest
 
Posts: n/a
Default Re: Storing jpgs

On Mon, 5 Apr 2004, C G wrote:
[color=blue]
> 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?[/color]

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

  #4  
Old November 23rd, 2005, 12:27 AM
Gavin M. Roy
Guest
 
Posts: n/a
Default Re: Storing jpgs

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:
[color=blue]
> 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 majordomo@postgresql.org)[/color]



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

  #5  
Old November 23rd, 2005, 12:27 AM
Joe Lester
Guest
 
Posts: n/a
Default Re: Storing jpgs

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:
[color=blue]
> 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:
>[color=green]
>> Dear All,
>>
>> What's the best way to store jpgs in postgresql to use in a web page?[/color][/color]



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

http://archives.postgresql.org

  #6  
Old November 23rd, 2005, 12:27 AM
Bill Moran
Guest
 
Posts: n/a
Default Re: Storing jpgs

Joe Lester wrote:[color=blue]
> 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.[/color]

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 majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

  #7  
Old November 23rd, 2005, 12:28 AM
Gavin M. Roy
Guest
 
Posts: n/a
Default Re: Storing jpgs

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:
[color=blue]
> 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:
>[color=green]
>> 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:
>>[color=darkred]
>>> Dear All,
>>>
>>> What's the best way to store jpgs in postgresql to use in a web page?[/color]
>>[/color]
>
>[/color]


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

  #8  
Old November 23rd, 2005, 12:28 AM
David Garamond
Guest
 
Posts: n/a
Default BYTEA maximum sizes (Re: Storing jpgs)

Rod K wrote:[color=blue]
> I've found bytea works better for me than large objects.[/color]

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

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles