I have some data stored in a MySQL database .. i would like to show the stored image data along with other data in a .php page..
if i fetch data from the database and use header("Content-type: image/jpeg"); its not possible to show the image with other php data.. is there a a other way ?
Read this: displaying an image stored in a mysql blob
If you set the header to image/jpeg that treats your entire page as an image file.. You want the data to be insert into the image holder only.
Try something like this
<img alt="Embedded Image" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIA..." />
Where you will next echo the blob data into the image src
<img alt="Embedded Image" src="data:image/png;base64,<?php echo $image->blob_data; ?> "/>
It depends how you stored you data, but sometimes you have to convert the data to base64. Try this
echo '<img src="data:image/png;base64,' . base64_encode($blob_data) . '"/>
Related
I have this code
echo ' <img class="postedImage" src="data:image/png;base64,'.base64_encode($Img).'"/ type="image/png"></img><br/>';
and I keep getting this display https://gyazo.com/ad1f8096798099d96c0d305a24176182
My database has the images stored in as longblob.
When echoing my base64 encode img, I get this which is basically just the images data. The image being uploaded is a png and I still get same results.
iVBORw0KGgpcMFwwXDANSUhEUlwwXDBcMJVcMFwwXDCNCAZcMFwwXDC+XCc4uVwwXDBcMAlwSFlzXDBcMA7DXDBcMA7DAcdvqGRcMFwwA+xJREFUeF7t3euRozAQRWHHRUCKh2hIhmBY2mtPeTyMB9TXRq0+X1X/2mcVZyWBH3tZXDAxooIcUUGOqCBHVJAjKsgRFeSICnJEBTmighxRQY6oIEdUkCMqyBEV5IgKckQFOaKCHFFBjqggR1SQIyrIERXkiApyRAU5ooIcUUGOqCBHVJAjKsgRFeSICnJEBTmighxRQY6oIEdUkCMqyOWLap6XaRqXUsoyDMM6l+Vy2Zjrjw3XnzeOk/0y7JQjqnlaxjJsx3NwhjIuE4W91HVUs61Iv61E3rFVbFwiri19RrWuTG+L6XmGsq5cXLc/F1fdRTWPmm3u2LBqPeooqvVcMF62LvjnZhgJy3QS1flB3YewOomqlaDuU6bbXyyp+FFNZfPCnjvDknnBCh7Vepe3eVEbmGFcXDflnEJHdc6d3v7Jug0GjqrhVeo+SVeruFG5z1LD10suzxd+ttcHR3ttcOvXHZmcZ6uwUXnu+IZ1X9p7rec13mHj99g7GR8xBI2qfuurusjzWB9Wwi0wZlS1W5/j5Fxcf1NQ1n8CuYSMqu4Ce8838zJWnbHynasCRlV5cQX39+fEHE/AqOrOU5JnRlXbLlG1r+rQLLqwZ/7ZgcSM6vD2pzos16ySRBWPPby0maZlshnL9cMKpfz/4MI1QNltPVHtET+qT6ra/nikgFdqDuo8/MQrVS8NJXyrAlHtduKjjGCIaq+qZ1T5zlOGqHY57yl+RES1Ay/PHENUf6l920vSVcoQ1Uu179vKu0oZovrVGlTVW13WSbxKGaLaVPveKZucd3yPiOoHT1B8OtkQ1Te+oLJve3dE9cUZFNveF6K68gaV+27vGVF57vJuw673Xe6o7GscN1wiOTJ8H9VPeaOqfVL+MAS1LWdUBPVW+aISBMWXxr6WKyrnl23YXR6H8r/liUoQFDvePjmiqnrX5uMUvoD/gP6j8gZl/6PD7bfCPn1H5Q4q75fBevQblfcMtZ7ICapOn1E5HxvY1zeiXn9ReYPiFs+ts6h8r+URlEZHUXnfsUlQKt1E5foKbFYoqT6i8jw64FAuFz8qz8Gc51BvETwqzzmK1/LeJXRUrf8vWvfJtsMGjsr/VuBPDVEFEWWVsiGqEOKsUjZEFUCkVcqGqJrn/eDn54eoWlwn+ODCp4eoGhdt67MhqqbF2/psiKppse767kNULQt4nrIhqoZFPE/ZEFXDiCqGYGcqREBUkCMqyBEV5IgKckQFOaKCHFFBjqggR1SQIyrIERXkiApyRAU5ooIcUUGOqCBHVJAjKsgRFeSICnJEBTmighxRQY6oIEdUkCMqyBEV5IgKckQFOaKCHFFBjqggR1QQW5Z/8Y/csUo9rX1cMFwwXDBcMElFTkSuQmCC
Normally this should work
echo '<img src="data:image/jpeg;base64,'.base64_encode( $data['image'] ).'"/>';
you can also try with data:$mime
where $mime:
mime can be an image of any kind, text, word document, text document,
pdf document, e.t.c... data is blob's column content
Please let know if this works for you, thanks.
Good day
I'm trying to show an image using DATA URIs and encoding a string in base64 as shown below:
<img src="data:image/jpeg;base64,<?php base64_encode($FOTO) ?>" />
The image (JPG) is stored in the database in this way:
The problem is that using that DATA URI, it does not show the image on the screen. I don't know the error or if I have to do something different to be able to show that stored image.
Is there any other way to display these images stored in SQL Server?
Thank you very much for your time.
The data in db is not base64, you need to convert this hexadecimal data to base64. Php, as I know doesn't have a direct function to do that but it can be achieved with pack(). I leave it to you as homework to study this function.
Convert the data from db to base64 like:
$fFoto = base64_encode(pack('H*', $foto));
Then pass this $fFoto to image src like you are already doing.
<IMG src="data:image/jpeg; base64, <?php echo $fFoto; ?>" />
I have a little problem here.
I try to convert an image into string base64, after that I want to save the string into blob in MySQL.
So, the blob can be displayed on the mobile apps.
this is my code :
$data = file_get_contents($_FILES["picture"]["tmp_name"]);
$image = base64_encode($data);
I already successfully save the blob into MySQL, but I can not displayed the image in website.
<td> <img src="<?php echo base64_decode($user->getPicture()); ?>"></td>
because the result is : ������� and many more
Am I wrong ?
Please correct me :)
The src attribute of an image MUST point at a url. You cannot dump the raw binary contents of an image in there and expect it to work. The browser will take that raw binary data and try to hit the page's originating server and request that data as if it was a file url. i.e. you have this on a page loaded from http://example.com/foo/bar/baz.php:
<img src="blahblahblahblah" />
which will result in the browser requesting
http://example.com/foo/bar/blahblahblahblah
If you want to embed your image in the page, then you have to use a Data URI:
<img src="data:image/jpeg;base64,<?php echo $base64_encoded_image ?>" />
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?
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.