Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

$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... Thanks in advance..

share|improve this question
    
stackoverflow.com/questions/2219824/… There you go –  simplexio Aug 31 '12 at 6:37
    
@simplexio , I have tried that, but from that code we get result in text form not in image format. –  kishor Aug 31 '12 at 6:53

2 Answers 2

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

share|improve this answer
up vote 1 down vote accepted

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);
share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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