Doesn't show blob image in php from mysql db - php

Can someone help me with this code? The problem is it doesn't show the image but this weird stuff: OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOÿÀx !ÿÄ¢  }!1AQa"q2‘¡#B±ÁRÑð$3br‚%&'()*456789:CDEFGHIJSTUVW
Here is my code:
<?php
//mysql connect database
mysql_connect('localhost','root','password') or die(mysql_error());
mysql_select_db('database') or die(mysql_error());
$res=mysql_query('select * from img');
while($row=mysql_fetch_array($res))
{
echo "<div id='image'>";
echo "<p/>";?> <img src="<?php echo $row['image'];?>" height='300px' width='468px'>";<?php
echo "<p/>"; echo $row['name'];
echo "</div>";
}
?>

<img> expects a URL pointing at where the image file is. You're trying to dump the raw binary garbage of the image into that image tag. That garbage will naturally contain " and > chars in it, and "close" your img tag, letting the rest of the garbage be treated as plain text.
You either have to serve up your image via separate script, e.g.
html:
<img src="pic.php?id=foo">
php:
header('Content-type: image/jpeg');
echo get_blob_from_database($_GET['id']);
Or embed the image inside the html properly:
<img src="data:image/jpg;base64,<?php echo base64_encode($row['image']); ?>">
And neither of these is particularly a good solution. Storing images directly in the database is almost never a good idea.

it's better to save your images in folder rather than save in blob format
you can save the file name in database table and save the images in folder
when you call the filename, you can combine it with src from and get the content

Related

How to echo images based on data from database? (Mysql, Php)

I am doing some sort of online storefront and each item has a corresponding image in the database. i need to echo the image of that specific item, how do i do it?
This is what i've done, But it doesn't seem to work:
<?php
$prebuy = "SELECT lot_image FROM lots WHERE lot_id= '$lot_id'";
$prebuyres = mysqli_query($mysqli, $prebuy) or die(mysqli_error($mysqli));
$lot_name = mysqli_fetch_assoc($prebuyres);
?>
<img src="C:\\xampp\htdocs\storefront\img\<?php echo ucwords($lot_name['lot_image']); ?>"
The image does not appear but there is no error either. What am i doing wrong? Please, help if you can.
Thanks in advance!
You missed enclosing img tag. try this way...and what about ucwords, to be sure first check result before using it.
echo ucwords($lot_name['lot_image']);
<img src="C:\\xampp\htdocs\storefront\img\<?=ucwords($lot_name['lot_image'])?>"/>
Edit:
set you image source relative to your php script in server like
<img src="img/<?=ucwords($lot_name['lot_image']);?>"/>

Can't display BLOB stored in database

I've got an problem when I want to convert an image in blob format stored in my database.
When iç just echo $content I can actualy see the blob file printed out so there is no problem with my queries.
The problem is that my code only displays an broken image instead of the Image in the database.
Does anyone know how to display the image properly?
Thanks in advance
$content = mysql_result($result,$i,'Image');
echo '<img src="data:image/jpeg;base64,<?php echo base64_encode($content); ?>" width="100" />';
The best way to do it would be to use a separate page to display the image like the following:
<?php
header("Content-Type: image/jpeg");
// Do your query
$content = mysql_result($result,$i,'Image');
echo $content;
?>
Then in another page do
<img src="pagetodisplaytheimage.php" width="100"/>
It's also answered in this question: How to display an BLOB image stored in MySql database?

Cannot display Image from database in php

I am using smarty, mysql and I am just trying to display image.
This is the error i am getting -
Resource interpreted as Image but transferred with MIME type text/html
My index.php file
<img src="http://localhost/admin/image2.php?id={$editWine[0].id}" width="150" height="260" border="0" class="bottle-img" id="smallImageWineEdit" />
My image2.php file
$id=$_REQUEST['id'];
$sql="SELECT * FROM table WHERE id=$id";
$result=mysql_query($sql) or die(mysql_error());
while($row=#mysql_fetch_assoc($result))
{
echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['image_data'] ).'" width="150" height="150" /> &nbsp';
echo $row['image_data'];
}
This echo inside while loop is working fine.
And when i inspect element and open this img link in new tab, Image is displaying. whereas its not displaying in current page.
Issue is that you are again delivering the html tags from the image file. You just need to output the image data with proper image content-type.
Edit image2.php like
if($row=#mysql_fetch_assoc($result))
{
header('Content-type: image/jpg');
echo $row['image_data'];
exit();
}
update to comments below
Do the above changes and request http://localhost/admin/image2.php?id=VALID_ID_HERE in browser and check if its retuning an image. Then you can use it in the src tag of index.php to show it there. If you get errors in rendering the image, whn you request it, make sure you are asking for the correct image id in the db and update the question with the error messages you are seeing.

Take the address of an image from the database and use it to direct to a file in server

<td><div align="center"><?php echo $row['cnvts']; ?></div></td>
<td><div align="center"><?php echo $row['image']; ?><a href='others/$row['image']'></div></td>
This is a part of the html query inorder where the content column image is been is been displayed.
So here now am planning to display the image that is been saved the folder "Other" with the name mohar.jpg
So is it possible to give a link in html to retrive the data from the filed image in db table and use it to open the image while clicking on it
try this.
link
I might have miss understood your question, but you are asking if there is a way to display an image from a saved url / path you fetched from DB right? If so, then the answer is simply
<img src="<?php echo $row['image']; ?>">
This of course imples that $row['image'] contains a valid relative path, like "/Other/mohar.jpg" - if not you need to massage it in PHP before sending it into the HTML.
Edit: After looking more closely over your question, I realize you might simply ask how you make a link out of an image in HTML, in that case you need two things. The URL to the image, and the URL to the pdf file (or target of the link). I assume you have the link to the pdf file from the DB row named $row['pdf']:
<img src="<?php echo $row['image']; ?>" />
Have a look at this Fiddle for a accurate example:
http://jsfiddle.net/z7gS4/2/
Also, you could read about linking images in HTML here

I need my PHP page to show my BLOB image from mysql database

So my last question was on how to have my techID shown from a search :
I am trying to have my "Details" page to reference two seperate parts of my server that are linked via techID
My new question is still on this page. I have added in an echo image as well. but am having trouble using Blob and having it display my image and not binary JPEG data.
I've been trying to find another instance of this but cannot find any that fix my error.
//Header ('Content-type: image/jpeg')
echo "<dt><strong>Technician Image:</strong></dt><dd>" . '<img src='.$row2['image'].' width="290" height="290">' . "</dd>";
and
$query_Recordset2 = "SELECT * FROM technician WHERE techID=" . $row1["techID"] ;
$Rs2 = mysql_query($query_Recordset2) or die(mysql_error());
Are the only changes I have put in so far from my last question (obviously including the fix I was given that worked).
What I do not understand is where and how to put 'Content-type: image/jpeg' to have my page recognize the image being linked is it's MIME TYPE image/jpeg.
What I am seeing on my page is this
Technician Image:
�E��j��i`=7f$D��o"�������b���Ckkc��R��^M�;n~��0&m)J��R��E)JDR��E)JDR��E)JDR��E)JDR��E)JDR��E)JDSjR��)���+��N��.R,u����i��n9,���QX~
����{(����̮�:���2�12��"��aV7�6���{���LP[�W�����گ� R$+�
��LMc'hM�5�o�PA����|���ګ���.8��E��ģ��Rn
��1�[��{��3>�rY��X�ۜ;�Ǖ����u���z��'�vf�N葟
��z�Q�����k��3���O��ܨ�ۀ�?S���,N� �����[{+D�
�;�'�$�$�&�iJR��)JR��)JR��)JR��)JR��)JR��)JR��)JR��)JR��)JR��)JR��)JR��)��
width="290" height="290">
Obviously I have deleted a middle chunk so it's not massive. there is a little "Broken image" box that appears infront and when I right click and choose "Open image in new window" the URL it puts in is simply Content-type: or I get a forbidden access page with the url http:// localhost/Sim5Server/Pages/%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%10JFIF%EF%BF%BD%01%02%EF%BF%BD%EF%BF%BDd%EF%BF%BDd%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BDC%EF%BF%BD
I have put a space in that url since it is not a link for the internet.
I have only used normal BLOB type as I just need it as a small less than 64Kb image
In your current case, you have two upfront options.
The first, and the one I don't recommend if you have numerous images like this, is to use inline base64 encoding. This is done with:
<img src="data:image/jpeg;base64,<?php echo base64_encode($image); ?>" />
A copy/paste version, using your existing code:
echo '<dt><strong>Technician Image:</strong></dt><dd>'
. '<img src="data:image/jpeg;base64,' . base64_encode($row2['image']) . '" width="290" height="290">'
. '</dd>';
The second method is to create an "image" PHP file that takes the ID of the image in the database as a query-string parameter and outputs the image. So, your HTML would look something like:
<img src="image.php?id=<?php echo $image_id; ?>" />
And your PHP page would look something similar to:
<?php
$id = (isset($_GET['id']) && is_numeric($_GET['id'])) ? intval($_GET['id']) : 0;
$image = getImageFromDatabase($id); // your code to fetch the image
header('Content-Type: image/jpeg');
echo $image;
?>
The only way you can output an image from the same page as the document is with a data uri.
echo "<dt><strong>Technician Image:</strong></dt><dd>" .
'<img src="data:image/jpeg;base64,'.
base64_encode($row2['image']).
'" width="290" height="290">' . "</dd>";
The right code should be as below. You have to use stream_get_contents() to change resource id into string.
<img src="data:image/jpeg;base64,<?php echo base64_encode(stream_get_contents($row2['image'])); ?>" />
I think best solution is to store path of the image in the database instead of
storing the whole image as BLOB. And then for showing the image on webpage set the
src property of the img tag to the path stored in database.

Categories