Jump to content

Archived

This topic is now archived and is closed to further replies.

nandakishor

How to display a image from postgreSQL using php

Recommended Posts

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 this post


Link to post
Share on other sites

What output do you get?  Are there any errors reported?  Make sure you check for errors after every call that could fail.

Share this post


Link to post
Share on other sites

I am not getting any error report....I got only a blank page...

 

If I run this code...

//header("Content-type: text/xml");

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

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


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


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

	$xml_result="<resultxml>";

	$num = pg_num_fields($result);

	while($row = pg_fetch_array($result)){

	$xml_result.="<record>"; 
		for ($i=0; $i < $num; $i++)
			{
				$xml_result .= "<".pg_field_name($result, $i).">".$row[$i]."</".pg_field_name($result, $i).">"; 
			} 
	$xml_result.="</record>";
	}
	$xml_result.="</resultxml>";

	echo $xml_result;

I got this message,

\211PNG\015\012\032\012\000\000\000\015IHDR\000\000\002X\000\000\003 \010\006\000\000\000\377`\034\304\000\000\000\004sBIT\010\010\010\010|\010d\210\000\000 \000IDATx\234\354\275g\260e\347u%v\316\271\347\346|_\316\261sB7\272\033\000\0014\032\000\021\230I\220"\305\242J\2264\242F\326\370\327x\254*\377\231qy\224\353"\024\011\222\255R\306\337\366\017\365\223-\275\221m\2653\365\002\331\212\305<\035\007\374\230C\251d\221l\265*\306\267-\024!\333\211\307\236h\265\347Vg\311vov\272\325>\371\311\347\3116\273|\257\325\316\257\255\220mu\021\353r\337\316\375d3k\242o\274\334O\233\353K\255vr\223\237\241\247w\220\216\007\3331/g\026o\223\255\321\304\342\333\312s?%\267r\370;7\257\303b\021\327t\273ym{\374\376V\373\311\023'\310\026\010\343o\003\021^\277\271\262\270\276\213\347lW\317X\253m\326cds\232\274\236\336\377\331+\260y\370\276\333b\270\267}\273x\016\305\375\330\313\247V\374d{\375\265\227[\355\236.\276~E\354s\303\273\306\310\326\031\303:<\371\324sd[[\307\274\250\346*d\373\311\017N\265\332\231F\206l=\275\001:.\227\360\374\355\011\336\333\016\355\374L\253}\357\366\025\262\031>\254\203[\2233dJm\341=\023\351\340\275%\354\303\272Lf\370}\324\336\205\347M\004\023d\273y\363f\253\375\310\236\207\311\366\301\207\347[\355\202\305}\021\353\340\376\356\212\340\031\363y\236\373\005\261~\027fx\2559^\274\037\022.\017\331<\021\314\267\272\233\337c\231u\321\377\016\367\357\263_\372T\253=\024\341sV\362\370N(\224\330\346\363\241o\034\337\026\331\252M\276F\320\205yyo\341>\331\274\015\354QM\336"\214\232\201\276x\360\251a\2629i\254=3\334C\266\225\371t\253]*g\311V(\315\266\332==\374\273\301\235\335\255v(\320I\266\213\257\\\3029\304\367\204a\030\206ehhhhhhhhhl+\364\007\226\206\206\206\206\206\206\206\3066\303>v\344!\372\207H\260\267\325\376\360\3129\262%\353p\257]\271v\206ln/\\v\265"\363b\3652(\226W\337\177\231lM/h\223\006{\325\015\333\017w\261\345a\267z1'h)\2056\261\005ew}\362\006\331\254\000\177S\332>\270,\255\032\273\334#A\\?\231bZ\254T\200\353\272;\312\356\342\216\301=\255v\303E\014\2541\227\237o\265\233e\266Y%\\\277Ye\232\265^\303\337z\025j\240&h\014\237B\373\271l\366\255\266Y\302E\353\342\276\260\004\015\231Th\233\325\034\\\275\256&\217\257\317\302\365\233\036\246T>\3754\334\370\363\327\316\223\355\334\231k\255\366\257\376\346\257\223\255\267\003\363\260s J\266\215u\214\305\315\313\323d\353\353\353\246\343h'\346\215\307\303\317\264%\306\264\273\257\215l\207\016\203\322ZZe\012zq\006\024\326s\317|\212l\177\371\343\277n\265\033\312\177_\232\302;_\3632EX\312n\264\332\305:\217o|\000\356jo2M\266\265&S\016M\003\363$\036a\372\303\033\303qn\235]\367k9\\?c\362z2\274\2303\367\356~H\246dZ\272\334sd\223{\202\255P\014\335m]\255\366\355\233g\311f\211\276\011\273\231R\370\344g\237\244\343\211\373S\255\366\316\360\003d\273?\207\371\025S\326E\265\204\315\246\250\320p^1\237k\312\377Akb\020\223kI\262\271}\230\263N\271\213le\261\234#\026\357e37\357\266\332=\273\231\342.\2572\205\026\014\243o6\222Ly\257\324\361\267\375\375Lcl\212\363\234\277\265@6+\002\232\306\3661}8\330\215\271\327\027\344g\362\373\260.#a\236\317N\005\324mt\210\347\241\337\205\363|\353O\377\003\337\347\272\302\005\011\372ix\204i\374}\373\320W\235\275\274~\177\360\243\357\266\332\231\025\246\323\306\206\020~pi\356.\331\232%\364\223\337\305s/\273\211\361\316\265q\377:E\374\355Bz\222lG\0369\334j\237\277\310\353\307\3600e\027H\340\235\343R\350i\267\203q\252\344\224P\201*\366\201\366X\210l\265:\336\225\363\253\274\177\324\014\364\257m\362\363\336\271v\253\325\356~\226\327]r\031}a*a)\353\311\331V;\237\347\367\246K\011\365\031\034\302\275\266\205\007\310\266\266<\327j\207B\334\337\345\022\236\327i\360\234)40\207\253i\336\037\2139\334O\256\304\353\267\322\304ZO\245\231\036\266\026\004U\035\345ws\321\213\353\205\303\274\317i\017\226\206\206\206\206\206\206\206\3066C\177`ihhhhhhhl3\354d\221\335]\221n\270zw\037?J\266\2719\250t\\Ev\331\335\256\300U_\251\260[\256$\350\255z\225]\336^\033.QO\224\335kM\037(\206\252\305\212\212\272p\3257\374\354\242\264\033\370]I\271\236\355\345kT\005\025g\331l[[Y\304\365j\354\206t\233\370\335c\217|\232l/|\003\307o\374\224\325q\035\033\240b\256\31725\222\016\010\005\002?\222\341\255\011J\264\252\320\200\202q`\015\216a\370\275\254\004\265jp-w\266\263\313\377\3322\250\260\260OQ\220\006\3207\226\315\224]_\327p\253}\353\326%\262\275w\365\375V\273\243\316\224h_\337\276V\333\251\360\003\267u\342\032\246\305\024\316\370^\\\317\353eJ\243\177\027\323\265M!?\011y\331\366\365\337\372l\253}\3654S\005{\367\356\3059\207\373\310\366\356\273\240:/\317\263\362e\337\356C\255\366\315\373LO\273\304c\264G\270\017\253[pW[u\246\015\214\014\236\341\311\347\036'\323w\276\373_\351\330\024\364S\335\247\250\343\322\030{\267\315s\250)\266\201f\205\307\311l\340~\362Y\036\213p\017TP\315y\246-\252.P\034\263\267o\222\315s\030\367\331\250s_\204\204\233=c\314\221\355G\177\376\227t\374\310\323_m\265/\\y\205l\201(\316\373\310#\307\310\366\332\253\257\266\332\205\034\323\030\206\240\023\033\312P\330\006:\352\341\0079\274\302\347\001\335\3215\310\373\343\231{\357\212\353-\221-..............

What to do, I am not understanding....

Share this post


Link to post
Share on other sites

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);

 

Any further suggestions....Please

Share this post


Link to post
Share on other sites

It'll only work with JPG images, but other than that I don't see any problems with it.

 

What suggestions are you looking for, besides working code?

Share this post


Link to post
Share on other sites

I got this message,

---

\211PNG\015\012\032\012\000\000\000.....

---

 

Can anyone please help me from this...

Thanks in advance..

 

Well... I see PNG in the data. But you try to use it as JPEG. Maybe this is a problem?

Try to change

header("Content-type: image/jpeg");

to

header("Content-type: image/png");

Share this post


Link to post
Share on other sites

Better to detect the MIME type automatically, based upon the picture/data itself, than to have it hardcoded.

That said, I assume (and hope) that you've validated the MIME-types and verified that the data is indeed images, before storing them in the database?

Share this post


Link to post
Share on other sites

@ChristianF,

Actually, I am very new to PHP and also for software sector, so I didn't aware about validating the MIME-types. Thank u very much for showing me a right path. Any doc is there to study about this.

I will try it out.

Any thing I have to work out other than validating.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.