Re: Storing jpgs

From: "Gavin M(dot) Roy" <gmr(at)ehpg(dot)net>
To: Joe Lester <joe_lester(at)sweetwater(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Storing jpgs
Date: 2004-04-06 16:40:12
Message-ID: 4072DD6C.8090708@ehpg.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Richard Huxton 2004-04-06 17:09:28 Re: Error on deleting
Previous Message Jan Wieck 2004-04-06 15:54:35 Re: Cursors and Transactions, why?