1

I have Images in bytea form in PostgreSQL/PostGIS database, I want to display them in a webpage using PHP - PostgreSQL.

I have a lot of examples that have worked out but I am not getting output from them...

Few codes I have used.

$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

$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

<?
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?

2

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

$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);
  • 1
    If you need more help, it would probably be useful if you can explain what type of help? For example, if that is "working fine", then why do you need more suggestions? – BradHards Aug 31 '12 at 11:13
  • @BradHards Actually I need to display a image in a popup window, as click feature info for a point layer in MapGuide... – Kishor Sep 1 '12 at 4:01
  • I stole your code here, thanks: dba.stackexchange.com/a/193823/6219 – dezso Dec 22 '17 at 11:29
1

Maybe you should take a look at tools such as mapnik, openJump, maperitive, qgis or one of the few other ones depending on what you want to do?! - I guess mapnik is the most appropriate...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.