I have a php script that goes like:-
<?php
$res = pg_query($db, "SELECT picture FROM userpost WHERE postid='2';");
$raw = pg_fetch_result($res, 'picture');
echo pg_unescape_bytea($raw);
?>
where $db is my postgresql database.
I'm using it to display an image that I inserted in my database using the command
insert into userpost(userid,postid,picture,content) values('244','2',bytea('/home/ankit/Downloads/Photos/0.jpg'),'This is post number 3');
What I get then in my browser is the address of the image, i.e, /home/ankit/Downloads/Photos/0.jpg and not the actual image. I can't understand what's the problem. Please help me figure it out. Thanks!
Related
PHP:
<?php
require "conn.php";
$cid = "6";// $_POST["cid"];
$mysql_qry = "select image2 from cities where ID = '$cid'";
$result = mysqli_query($conn,$mysql_qry);
$row = mysqli_fetch_array($result);
header('Content-Type: image/png');
echo base64_decode($row["image2"]);
$conn->close();
?>
and this is the result
How to solve that? my photo is almost black!
As i can see you are collecting image source from the database table. You must check that weather your entire image source is saved properly after encoding it. So check the database Column Data Type in which you have stored it i doubt that its not saving the full image source hence further you are not able to get the full image.
Advice : Most probably Best Practice is that you should convert it to image and save it on disk and just save the IMAGE SOURCE PATH on your Database Table Column. And then fetch it to display when required.
Otherwise Your DB will exausted and will start taking time in retrieving the records. Also problem will start in DB Backup and migrations.
If you dont want to follow the advise then change your column type to Either Blob depending on your image size.
I have a postgres database with stored images 'bytea' type and I try to display them into a browser with PHP. I found the way to display one of them but I can't make it for more than one. The code I use is the following:
File Name - display_image.php
$conn = pg_connect("dbname=test user=postgres password=postgres");
$temp = '/home/postgres/tmp.jpg';
$query = "select lo_export(image, '$temp') from map ";
$result = pg_query($query);
if($result)
{
while ($line = pg_fetch_array($result))
{
$ctobj = $line["image"];
echo "<IMG SRC=show.php> </br>";
}
}
else { echo "File does not exists."; }
pg_close($conn);
File Name - show.php
header("Content-type: image/jpeg");
$jpeg = fopen("/home/postgres/tmp.jpg","r");
$image = fread($jpeg,filesize("/home/postgres/tmp.jpg"));
echo $image;
The problem seems to be the "tmp.jpg" virtual file which displays only one image. If the result of the query is 7 images then it displays 7 times the same image within a while loop. How can I solve this?
Thanks for the interest!
I did this some time ago for bytea.
You need to run your bytea data through pg_unescape_bytea. See http://php.net/manual/en/function.pg-unescape-bytea.php
Basically your SQL query returns the bytea field in an escaped format.
However this is not what you are doing. And so the above is just for the next poor sap who comes here looking for bytea help. Please amend to note you are using LOB's not BYTEA's.
Also note that your code there is not concurrency safe. If two users request different images, my guess is that you will get both users getting different images. For this reason you should add the oid to the retrieval url, and name your file /tmp/$oid.jpg where $oid is the oid of the large object. You will need to retrieve that info (I believe it looks like it is stored in the image field of map?). On the other hand that assumes that all files are essentially public. if that's not the case, you want to move everything into the show_image.php and clean up when you are done.
I know it's not the greatest technique, but coming from Foxpro and Dbase background, some habits just don't die. I am absolutely fresh in PHP and trying to re-create learning curve of my earlier programming experience.
I have drilled GOOGLE for the issue and tried to come up with the script, only issue is sometimes I came up with garbles, sometime empty screen, sometimes only IMAGE.
The scenario is If I am able to create this simple script, I will probably understand how to handle images issue.
I have a database in my MYSQL called crm. In the database I have a table called mast_cust with fields f_name, l_name and pic (BLOB)
I already have some records in them.
What I understood was:
Images and Data (Textual) cannot be printed together
You have to have two scripts , one gathering the image and the other gathering data
The one which gathers data , calls the one which collects images and prints it.
What I want is, to see data in tabular format
First Name | Last Name | Image
These are my two scripts, which is the one which prints garble
Script 1 : Main PHP file - list.php
Script 2 : Image Storing Script: pix.php
list.php
<?php
$errmsg = "";
if (! #mysql_connect("localhost","root","Admin"))
{
$errmsg = "Cannot connect to database";
}
#mysql_select_db("crm");
$strSQL = "select f_name,l_name,pic from mast_cust";
$rsPix = mysql_query($strSQL);
$numRows = mysql_numrows($rsPix);
$i = 0;
while($i < $numRows){
?>
<img src="pix.php?pixID=<?php echo mysql_result($rsPix,$i,"pic"); ?>"/>
<?php
$i++;
}
?>
Script 2 pix.php
<?php
$errmsg = "";
if (! #mysql_connect("localhost","root","Admin"))
{
$errmsg = "Cannot connect to database";
}
#mysql_select_db("crm");
if (IsSet($_GET['pixID'])){
$gotten = #mysql_query("select pic from pix where cust_id = ".$_GET['pixID']);
header("Content-type: image/jpeg");
while ($row = mysql_fetch_array($gotten))
{
print $row['pic'];
}
mysql_free_result($gotten);
}
?>
Any help resolving this issue is highly appreciated.
Thanks and Regards
It seems like you're storing the jpeg image as a blob in mysql.
1.) You can only send one pic at a time. So no need for a for loop.
2.) If so, you'll need to format the output of the data for the pic, so that a browser can read it.
I would recommend against this approach. You should avoid using a relational database for storing images. Try storing the jpegs on a regular filesystem or a file server (like S3), and store the url to it in the database.
If obfuscating the image url isn't enough, try the following approach:
Echo/print a jpg-image with php, for safety?
It uses:
http://php.net/manual/en/function.readfile.php
I have been struggling with this all day yesterday but can't get to the bottom of it.
I have an existing database that I am trying to retrieve an image from (I know image storing is bulky but it's what I'm stuck with). I am able to query the appropriate database field and get the data out but when I display it, it seems like only part of the data is being displayed.
I am using this code to display the image:
require_once('../odbc_config.php');
if((isset($_GET['ur'])) AND ($_GET['ur'] != "")) {
$aria_query1_ur = $_GET['ur'];
$aria_query1 = "SELECT * FROM Patient WHERE PatientId='$aria_query1_ur' ";
$patient_result = odbc_exec($aria,$aria_query1) or die("Error getting Patient Data");
$patient_data = odbc_fetch_array($patient_result);
}
$ID=$patient_data['PatientSer'];
$query = odbc_exec($aria, "SELECT * FROM Photo WHERE PatientSer=".$ID);
$row = odbc_fetch_array($query);
$content = $row['Picture'];
header('Content-type: image/jpeg');
echo $content;
I am calling this code like this:
echo '<img src="contents/image2.php?ur='.$_GET['ur'].'">';
Unfortunately I only seem to be able to display the image partially. The dimensions of the image are correctly read and I can see the first 50 x 480 or so pixels from the top but after that all I see is a gray background of 640 x 480 pixels.
Like this:
http://i1218.photobucket.com/albums/dd411/Barbs_ldsr/stuff/th_partial_image.jpg
Is it possible the Sybase ODBC driver is only allowing a certain amount of data through? How would I check or fix this? Or is there a problem with the PHP?
Any help appreciated.
Thanks
I commented the solution to our problem has to do with the limited text editing the php.ini solves it. Finds the next row.
odbc.defaultlrl = 4096
Only enlarges the value for example
odbc.defaultlrl = 40960000
and now with this whole picture leaves you. I hope will be of your help and cuerdate we put the answer in the forums where nannies asked for when someone else runs into this will solve
In my Database, the images(jpeg,bmp format) are stored in bytea datatype, showing up in binary code in the database. Now I want to retrieve the images from the database. But i could not get the image in the web page. When i retrieve using this code given below, it shows the binary code value.(ie combination of numbers,characters,symbols). My code was
$dbconn = pg_connect("host=localhost user=xxxx password=xxxx dbname=xxxx")
or die('Could not connect: ' .pg_last_error());
$rs = pg_query($dbconn, "select scan_image from image where cno='4' and imageno='1'");
$image = pg_escape_bytea(pg_fetch_result($rs, 0));
echo $image;
Am i correct with this code? Please help me to find the solution.
Before you echo out the image content, you need to set the headers like:
header('Content-type: image/jpeg');
Then you can call your script in an image tag of the page where you want the fetched image to be displayed:
<img src="name_of_your_script.php">
This link will help you: Managing Images With a Web Database Application
U can use Blob to retrieve image from database. And to store image u have convert image to byte using filestream.
This link may help you - http://www.itpian.com/Coding/6298-How-to-retrieve-the-image-to-database-coding.aspx