473,287 Members | 1,574 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,287 software developers and data experts.

Storing uploaded image in PostgreSQL as a binary 'bytea' type

Hi,

I am trying to write script that is run when a form is submitted. The
form contains an image input field and when submitted, the image is
uploaded, resized and added as binary information to a db table. Please
note, I am using a PostgreSQL database

I have written all the code out below that deals with the submission
processing:
$tmpfilesize = $_FILES['imgfile']['size'];
$tmpfilename = $_FILES['imgfile']['tmp_name'];
$tmpfiletype = $_FILES['imgfile']['type'];

if ($tmpfilesize > 0) {

//check this is an image by using the file type info
if (substr($tmpfiletype, 0, 6) == 'image/') {

//create image from uploaded image
switch ($tmpfiletype)
{
case "image/jpeg":
case "image/pjpeg":
$img = imagecreatefromjpeg($tmpfilename);
break;
case "image/gif":
$img = imagecreatefromgif($tmpfilename);
break;
case "image/png":
$img = imagecreatefrompng($tmpfilename);
break;
}

//resize image
$imginfo = getimagesize($tmpfilename);
$width = $imginfo[0];
$height = $imginfo[1];
$maxsize = 200;
if (($width > $maxsize) || ($height > $maxsize)) {
$ratio = max($width, $height) / $maxsize;
$newwidth = floor($width / $ratio);
$newheight = floor($height / $ratio);
$newimg = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($newimg, $img, 0, 0, 0, 0, $newwidth,
$newheight, $width, $height);
$img = $newimg;
}

//prepare image for database
ob_start();
imagejpeg($img, '', 80);
$imgdata = pg_escape_string(ob_get_contents());
ob_end_clean();

//write to db
$curdate = date("Y-m-d H:i:s");
$insert = "INSERT INTO imglib (createdon, imgdata) VALUES ('" .
$curdate . "', '" . $imgdata . "')";
pg_exec($db, $insert);
I have included all the code just encase but the only line that is
giving any problems is:
pg_exec($db, $insert);
I get the following error:
Warning: pg_exec(): Query failed: ERROR: unterminated quoted string at
or near "'ÿØÿà" at character 89 in
/data/httpd/VirtualHosts/webdev/htdocs/mb_sandbox/_test/img/simplebytea.php
on line 89
If it helps also, I used the following SQL to CREATE the DB table:
CREATE TABLE imglib (imgid serial, createdon timestamp, imgdata bytea);
I am very new to storing images. When I comment out the pg_exec() it
doesnt give me any problems which suggests that everything goes well
(apart from the fact that it doesnt write to db). Im guessing that the
fault is in the preperation of the image data prior to be written to
db. Is bytea the correct data type for this? I have successfully stored
an image as a BLOB to MySQL and the code above is a slightly modified
version of that code. I dont want to convert the code to a string of
byte64 as this will creates uneccessary overhead.

Any suggestions? Cheers

Burnsy

Aug 22 '05 #1
2 9225
Search this newsgroup first.
If memory serves me well, this very question has been on this ng a few times
the last few weeks alone.

Tip: Use google discussiongroup search capabilities.

Regards,
Erwin Moller
Aug 22 '05 #2
Long question, short answer...

The problem was that I was using pg_escape_string() instead of
pg_escape_bytea(). I got pg_escape_bytea() mistaken for something that
'escaped' the format from a bytea type to something else (like a
string) or is that still what it is doing? Works now anyway.

Burnsy

Aug 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: dave | last post by:
Hello there, I am at my wit's end ! I have used the following script succesfully to upload an image to my web space. But what I really want to be able to do is to update an existing record in a...
6
by: bissatch | last post by:
Hi, I am currently writing a news admin system. I would like to add the ability to add images to each article. What I have always done in the past is uploaded (using a form) the image to a...
3
by: bissatch | last post by:
Hi, I am wanting to learn how to store images in a postgreSQL database. I have got as far as uploading the file using a file/browse field on an html form and have been able to catch the file...
1
by: Josué Maldonado | last post by:
Hello list, That's the question, what is the equivalent data type of the msSQL image data type ? TIA, -- Josué Maldonado.
7
by: C G | last post by:
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...
1
by: Együd Csaba | last post by:
Hi All, I use PG 7.3.2 an 7.4.1 on RH 7.1. I'd like to store small (~ 100x100 px jpeg, or gif) images in a field in binary format. I tried to understand the documentation of the large objects but...
2
by: Eduardo Pérez Ureta | last post by:
I was wondering what the best way is to store a file hash (MD5 or SHA1) and make it primary key indexed. I have seen some people storing the hexadecimal encoded MD5 in a CHAR(32) but it may be a...
0
by: NM | last post by:
Hello, I've got a problem inserting binary objects into the postgres database. I have binary objects (e.g. images or smth else) of any size which I want to insert into the database. Funny is it...
0
by: badaboum84 | last post by:
I have to migrate a current PostgreSQL database to MSSQL. For doing this task I uses Python to create script for doing this task. Now, I want to extract the data from the current PostgreSQL...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.