Getting image from a DB

Getting image from a DB

am 07.05.2009 20:16:43 von fesanch

This is a multi-part message in MIME format.

------=_NextPart_000_005E_01C9CF50.BCFB9F30
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

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 =3D =
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 =FF, 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.=20

PD: The image field is of type bytea,as you recommend me.
------=_NextPart_000_005E_01C9CF50.BCFB9F30
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable



charset=3Diso-8859-1">




Hi:

 

Has anybody here had to capture an =
image from=20
a PostgreSQL Data Base using PHP?? I used a simple query: select photo =
from=20
people where id =3D 3, for instance. The problem is that when I write =
the image to=20
disk it's not a valid image. I opened it using an HEX editor and saw =
that the=20
question is that for each "strange" character, like =FF, it writes, for =
instance,=20
\377, it is, the "\" symbol plus the character's numeric code. I guessed =
that=20
there were some function for this situation but I couldn't find it. If =
you can't=20
help me, I'll have to parse the whole image contents, with the =
subsequent=20
performance impact.

 

PD: The image field is of type =
bytea,as you=20
recommend me.


------=_NextPart_000_005E_01C9CF50.BCFB9F30--

Re: Getting image from a DB

am 08.05.2009 08:26:15 von Andy Shellam

Hi Felix,

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

Your PostgreSQL database column that holds the image should be of type=20
"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 -=20
http://uk2.php.net/manual/en/function.pg-unescape-bytea.php

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

Regards,
Andy

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

--=20
Sent via pgsql-admin mailing list (pgsql-admin@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-admin

Re: Getting image from a DB

am 08.05.2009 23:11:03 von fesanch

Well, using pg_unescape_bytea I was able to get the image. How can I stre=
am=20
it directly to the users's browser??


----- Original Message -----=20
From: "Andy Shellam"
To: "F=E9lix S=E1nchez Rodr=EDguez"
Cc:
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 da=
ta=20
> which is all an image is.
>
> Your PostgreSQL database column that holds the image should be of type=20
> "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 -=20
> http://uk2.php.net/manual/en/function.pg-unescape-bytea.php
>
> Once you have unescaped the bytea data, you should then have your valid=
=20
> set of bytes (your image) which you can either write to a temporary fil=
e=20
> or stream directly to the user's browser from your application.
>
> Regards,
> Andy
>
> F=E9lix S=E1nchez Rodr=EDguez wrote:
>> Hi:
>> Has anybody here had to capture an image from a PostgreSQL Data Base=20
>> using PHP?? I used a simple query: select photo from people where id =3D=
3,=20
>> for instance. The problem is that when I write the image to disk it's =
not=20
>> a valid image. I opened it using an HEX editor and saw that the questi=
on=20
>> is that for each "strange" character, like =FF, it writes, for instanc=
e,=20
>> \377, it is, the "\" symbol plus the character's numeric code. I guess=
ed=20
>> that there were some function for this situation but I couldn't find i=
t.=20
>> If you can't help me, I'll have to parse the whole image contents, wit=
h=20
>> the subsequent performance impact.
>> PD: The image field is of type bytea,as you recommend me.
>=20



--=20
Sent via pgsql-admin mailing list (pgsql-admin@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-admin