How to display a image from postgreSQL using php

A

Anonymous

Guest
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:
$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:
$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:
<?
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..
 
I got this code and it is working fine...

Code:
$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
 
Back
Top