How to display a image from postgreSQL using php

Do you have questions regarding other database enginges (not MySQL) -- ask here!

Moderators: macek, egami, gesf

nandakishor
New php-forum User
New php-forum User
Posts: 6
Joined: Tue Aug 07, 2012 3:31 am

How to display a image from postgreSQL using php

Postby nandakishor » Thu Aug 30, 2012 10:15 pm

I have Images in bytea form in PostgreSQL/PostGIS database, i want to display them in a webpage using PHP - PostgreSQL. I got lot of examples and I worked out but not getting output from them...

Few codes I have used.

Code: Select all

$conn = pg_connect("user = postgres password = nkr@123 host = localhost dbname = ARKREJ");

    $res = pg_query($conn, "SELECT photo FROM public.zdata_4326, public.zdata   WHERE zdata_4326.gid = zdata.gid AND zdata.gid = 1;");

        if (!$res) {
            echo "An error occured at result.\n";
            exit;
        }

        if($res) {
            $Row = pg_fetch_row($res, '0');

            $Object = pg_locreate($conn);
            header("Content-Type: image/jpg");
            $File = pg_loopen($conn, $Object, "r");
            pg_loreadall($File);
            }
        else {echo "The query failed!?! There is no row with id=1!";}


Another one
display.php

Code: Select all

$conn = pg_connect("user = postgres password = nkr@123 host = localhost dbname = ARKREJ");

    if (!$conn) {
        echo "An error occured.\n";
        exit;
    }

    $temp = "C:/Program Files/OSGeo/MapGuide/Web/Apache2/htdocs/tmp.jpg";


    if (!$temp) {
        echo "An error occured at temp.\n";
        exit;
    }


    $result = pg_query($conn, "SELECT (photo, '$temp') FROM public.zdata_4326, public.zdata WHERE zdata_4326.gid = zdata.gid AND zdata.gid = 1;");

    if (!$result) {
        echo "An error occured at result.\n";
        exit;
    }

        if ($result)
            {
                While ($line = pg_fetch_array($result))
                {
                    $ctobj = $line["photo"];
                    echo "<IMG SRC = show.php> </br>";
                }
            }
        else {echo "File does not exists"; }


show.php

Code: Select all

<?
header("Content-type: image/jpeg");
        $jpeg = fopen("C:/Program Files/OSGeo/MapGuide/Web/Apache2/htdocs/tmp.jpg", "r");

        $photo = fread($jpeg, filesize("C:/Program Files/OSGeo/MapGuide/Web/Apache2/htdocs/tmp.jpg"));

        echo pg_unescape_bytea($photo);
    ?>


Can anyone please help me from this... Thanks in advance..

nandakishor
New php-forum User
New php-forum User
Posts: 6
Joined: Tue Aug 07, 2012 3:31 am

Re: How to display a image from postgreSQL using php

Postby nandakishor » Fri Aug 31, 2012 1:21 am

I got this code and it is working fine...

Code: Select all

$conn = pg_connect("user = postgres password = nkr@123 host = localhost dbname = ARKREJ");
$res = pg_query($conn, "SELECT photo FROM public.zdata_4326, public.zdata   WHERE zdata_4326.gid = zdata.gid AND zdata.gid = 1;");
$raw = pg_fetch_result($res, 'photo');
header('Content-type: image/jpeg');
echo pg_unescape_bytea($raw);


Any further suggestions....Please


Return to “Other Database Engines”

Who is online

Users browsing this forum: No registered users and 0 guests