Thread: Getting image from a DB

Getting image from a DB

From
Félix Sánchez Rodríguez
Date:
Hi:
 
Has anybody here had to capture an image from a PostgreSQL Data Base using PHP?? I used a simple query: select photo from people where id = 3, for instance. The problem is that when I write the image to disk it's not a valid image. I opened it using an HEX editor and saw that the question is that for each "strange" character, like ÿ, it writes, for instance, \377, it is, the "\" symbol plus the character's numeric code. I guessed that there were some function for this situation but I couldn't find it. If you can't help me, I'll have to parse the whole image contents, with the subsequent performance impact.
 
PD: The image field is of type bytea,as you recommend me.

Re: Getting image from a DB

From
Andy Shellam
Date:
Hi Felix,

I've not written an image specifically, but I have read/written byte
data which is all an image is.

Your PostgreSQL database column that holds the image should be of type
"bytea."  Your PHP application must then use the following functions:

Writing to the database - http://uk2.php.net/pg_escape_bytea
Reading from the database -
http://uk2.php.net/manual/en/function.pg-unescape-bytea.php

Once you have unescaped the bytea data, you should then have your valid
set of bytes (your image) which you can either write to a temporary file
or stream directly to the user's browser from your application.

Regards,
Andy

Félix Sánchez Rodríguez wrote:
> Hi:
>
> Has anybody here had to capture an image from a PostgreSQL Data Base
> using PHP?? I used a simple query: select photo from people where id =
> 3, for instance. The problem is that when I write the image to disk
> it's not a valid image. I opened it using an HEX editor and saw that
> the question is that for each "strange" character, like ÿ, it writes,
> for instance, \377, it is, the "\" symbol plus the character's numeric
> code. I guessed that there were some function for this situation but I
> couldn't find it. If you can't help me, I'll have to parse the whole
> image contents, with the subsequent performance impact.
>
> PD: The image field is of type bytea,as you recommend me.

Re: Getting image from a DB

From
Félix Sánchez Rodríguez
Date:
Well, using pg_unescape_bytea I was able to get the image. How can I stream
it directly to the users's browser??


----- Original Message -----
From: "Andy Shellam" <andy-lists@networkmail.eu>
To: "Félix Sánchez Rodríguez" <fesanch@ciego.cult.cu>
Cc: <pgsql-admin@postgresql.org>
Sent: Friday, May 08, 2009 8:26 AM
Subject: Re: [ADMIN] Getting image from a DB


> Hi Felix,
>
> I've not written an image specifically, but I have read/written byte data
> which is all an image is.
>
> Your PostgreSQL database column that holds the image should be of type
> "bytea."  Your PHP application must then use the following functions:
>
> Writing to the database - http://uk2.php.net/pg_escape_bytea
> Reading from the database -
> http://uk2.php.net/manual/en/function.pg-unescape-bytea.php
>
> Once you have unescaped the bytea data, you should then have your valid
> set of bytes (your image) which you can either write to a temporary file
> or stream directly to the user's browser from your application.
>
> Regards,
> Andy
>
> Félix Sánchez Rodríguez wrote:
>> Hi:
>>  Has anybody here had to capture an image from a PostgreSQL Data Base
>> using PHP?? I used a simple query: select photo from people where id = 3,
>> for instance. The problem is that when I write the image to disk it's not
>> a valid image. I opened it using an HEX editor and saw that the question
>> is that for each "strange" character, like ÿ, it writes, for instance,
>> \377, it is, the "\" symbol plus the character's numeric code. I guessed
>> that there were some function for this situation but I couldn't find it.
>> If you can't help me, I'll have to parse the whole image contents, with
>> the subsequent performance impact.
>>  PD: The image field is of type bytea,as you recommend me.
>