14

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

Community
  • 1
  • 1
DBeslan
  • 155
  • 1
  • 1
  • 6
  • 1
    *sidenote:* stop using deprecated `mysql_*` functions. use [MySQLi](http://php.net/manual/en/book.mysqli.php) or [PDO](http://php.net/manual/en/book.pdo.php) instead. – Raptor Dec 06 '13 at 03:45

4 Answers4

39

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;
?>
newfurniturey
  • 37,556
  • 9
  • 94
  • 102
  • Thank you for fixing my post (Not so good at my posting etiquette yet). I tried the second one but I think i was doing something wrong at some point. There isn't many picture so I went with yours and Musa's posts (Musa's since I was able to simply copy and paste it over my pre-existing code). Are you able to give me any incite into what 'base64' de/encoding actually does? – DBeslan Nov 05 '12 at 04:54
  • @DBeslan I've updated with a copy/paste example for you (though you've already received similar from Musa; I still recommend trying to get the second version working and would be glad to help work through that with you. – newfurniturey Nov 05 '12 at 13:59
  • I don't know what is my image type. it could be jpeg, png, gif. Now what is the code for that – Md. Sahadat Hossain Oct 30 '13 at 09:56
  • @Md.SahadatHossain You should post a separate question here on StackOverflow for that; the answer is separate from the answer to this question and can be rather lengthy. – newfurniturey Oct 30 '13 at 14:48
2

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>";
Musa
  • 96,336
  • 17
  • 118
  • 137
  • I tried this, all I get is a 290x290 "empty image" box. I don't udnerstand base64 encoding/decoding. – DBeslan Nov 05 '12 at 04:43
  • IT WORKED! Thank you very much Musa. I would like to ask you how this works though? it would be very handy to understand what I/you have done. – DBeslan Nov 05 '12 at 04:46
2

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'])); ?>" />
  • You saved too many hours of my life, man. Thanks. My code simply used to work with base64_encode($image_blob_file). We recently upgraded to a new host and it stopped working; giving me a bunch of errors (base64_encode expecting string but resource found). Thanks to you, when I encode the input stream instead of the image. It worked. – Emastmagy MastMagy May 26 '16 at 14:16
  • It's surprising how poorly referenced stream_get_contents() is, I never bumped on it with searches like "blob to string php", "convert blob to string...", ... – aurelienC Oct 04 '16 at 12:08
-1

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.

Sikander
  • 447
  • 4
  • 26