not completed image after decoding it php Android - php

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.

Related

DIsplay image(bytea) from postgresql with php

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.

Display image from sql

I've create a table where I've saved images through "BLOB". I need to show those images along with other items. But I don't know how to show those images together in the same page.
Here's my php code that displays other things in form of a table. Similarily, I wanted to display images accordingly. Any help?
<?php
$display_query = mysql_query("SELECT * FROM eportal");
echo "<table id='pageTable'><thead><tr><th>Item code</th><th>Description</th><th>Cost</th></tr></thead>";
echo "<tbody>";
while($row = mysql_fetch_array($display_query)){
print "<tr><td>".$row['itemid']."</td><td>".$row['description']."</td><td>";
print "₹".$row['cost']."</td></tr>";
}
echo "</tbody>";
echo "</table>";
mysql_close($connection);
?>
Saving images to the DB is not a good idea but if You think You need to it this way, then You can retrieve the data from DB table, encode it to base64 (http://php.net/base64_encode) and then in HTML print it in this way:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot">
Using PHP You would write:
echo '<img src="data:'.$image_mime_type.';base64,'.base64_encode($image_data_from_db).'" alt="My image alt" />';
As other people mentioned, storing the images in the database is usually a bad idea.
Images are not transmitted in the same HTTP response with another page data.
To show images from the database, you would need to implement a script which, given the item id, would read the field and send the image's binary data; and provide the path to the script in your form's <img src="">:
while($row = mysql_fetch_array($display_query)){
print "<tr><td>".$row['itemid']."</td><td>".$row['description']."</td><td>";
print "₹".$row['cost']."</td><td>";
print "<img src=\"image.php?id=".$row['id']."\"></td></tr>";
}
image.php is another script which outputs the value of image_blob given the eportal.id. You would also need to provide correct size and mime type in the headers.
You better just store the images in a file (accessible by the web server) and store the path fo the file in the database.
Read the Blob data and write it into the file with header type image.. and try to print it, It should display the image file.
And yes saving image or any file in DB is really a bad habit as you are increasing DB size and it slowdown the performance also.. I suggest you to just try to convert you Blob into Image but don't apply in your work. Just save the image at desired location and keep its location path into DB to fetch and save next time.
The debate of storing blobs versus storing a path to the image file on disk has been debated over and over again. Microsoft provides a research paper comparing the pros and cons of each here. With that said, to display a blob as an image you need to make a call to a separate page and output header information that tells the browser what type of image is stored.
For example:
connectToDatabase();
$sql = "SELECT image_blob FROM eportal;";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($result);
header("Content-type: image/jpeg");
echo $row['image_blob'];
$db->close();
In case you still want to save your images in database, you will need second script which will get those images from database and pass them to browser with correct headers.
header("Content-type: image/jpeg");
#
# Replace this with database read:
# readfile('myimage.jpg');
Also, you will need to store what kind of image u use. There will be different header for JPEG, GIF or PNG file.

Display image along with other data in a table

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

how to Display Multiple Images (blob) from mysql using php?

I'm trying to display multiple images using PHP and MySql database, even if using the while loop I don't get all of the images, I only get one, I mean the first one in the table. What's the problem ?
I'm using a table ID_IMAGE (int, pk, auto increment) and myImage (blob)
$query = mysql_query("SELECT myImage FROM image");
while($data=mysql_fetch_array($query)) {
header('Content-type: image/jpg');
echo $data['myImage'];
}
A possible way to solve this problem is to have a separate script to dynamically output the contents of the image eg. :
image.php
header('Content-type: image/jpg');
// DataBase query and processing here...
echo $data['myImage'];
and call it whenever you need to show images stored in your DB eg. inside your loop:
echo '<img src="image.php?id=' . $data['id'] . '">';
But storing images in the database will take a toll on your server and unless they're really small or you have a good reason to do so, you should only store their physical location on the disk.
You can also use this approach if you wish to hide image location from your users, or control access, but there are better and faster alternatives for that case.
Just to mention the possibility to embed the images directly in html by encoding them, you can use this:
$query = "SELECT myImage FROM image";
if ($result = mysqli_query($link, $query)) {
while ($row = mysqli_fetch_assoc($result)) {
echo '<img src="data:image/jpg;base64,' . base64_encode($row['myImage']) . '">';
}
}
Pro:
The browser does not need to load the images over an additional network connection
You can query and display multiple images in one request
Con:
If the image(s) are big and/or there will be many images, the page will be load slow
Encoding an image to base64 will make it about 30% bigger.
For more information about base encode images:
http://davidbcalhoun.com/2011/when-to-base64-encode-images-and-when-not-to/
base64 encoded image size

How to Retrieve image from database?

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

Categories