473,385 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Storing jpgs

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
7 2746
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Waguih Boctor | last post by:
Hi, I have a number of ASP pages where some JPGs and GIFs are displaying in the browser and some are not. I have included an excerpt from the HTML below. In this example, the gif file at the...
4
by: walterbyrd | last post by:
Let's suppose I have dozens, or hundreds of JPGs. I want to put them in photo album sort of format. With thumbnail and all. Do I have to specically spell out the full name of each file? Or is there...
1
by: Ecohouse | last post by:
I have created a MS Access Project linking to a SQL database. I created a table that I want to hold jpgs in. The actual field I created is a datatype of image. I created a form in Access. ...
3
by: David A. Osborn | last post by:
I'm looking to write a program that can scale down JPGs to 640x480 resolution. I know that XP has a feature that when you right click on a JPG and select send to a recepient it prompts you about...
5
by: Garry Jones | last post by:
I need to create a page with a password where I show photos. How do I stop people from accessing the jpgs directly without going through the password function. I am using Windows XP and have a...
13
by: Rhino | last post by:
Is it possible to store Java objects in DB2 V8.2 for Windows/Unix/Linux via JDBC? Specifically, if I have a 4-dimensional boolean array, i.e. boolean, can I store it directly in a column of a...
27
by: Chris Tomlinson | last post by:
Hi, is there any way to specify the sequence in which images load on a web page? More specifically, here is what we need to achieve: Image1 starts loading first and the browser does not...
2
by: VivekR | last post by:
Hi All Please help me out with this thing in javascript. I am dumb in the world of JS/Html/... I have a url, say www.someurl.com. It has a list of jpgs that can accessed as...
8
by: J. Frank Parnell | last post by:
Hi there, I got this directory tree listing function somewhere, it works great, but I want it to only list directories that do have one or more .jpgs inside. I tried getting a list of each dir's...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.