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" />  ';
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.
Related
Ok, so here is what I am looking to do...I want to be able to upload a picture to the specified folder, and the path is inserted into the database(That part is done)
Then I need to pull the images from the database and display them on the index.php page. I know it's a simple process but I must be missing something.(grrr) I just can't seem to get the image to display on the index.php page, I can display the file location on getimage.php and echo out the image, but can't seem to reference the location to the img src and get it to display on index.php
Here is the code for the index.php page:
I want to be able to pull the image from getimage.php and display the image on the index.php page.
<a href="gallery.html">
<img src="getimage.php?id=18" alt="" width="200" height="133" /></a>
Here is the getimage.php page:
<?php
include("connect.php");
$id = mysql_real_escape_string($_GET['id']);
$sql = mysql_query("SELECT * FROM pictures WHERE p_id='$id'");
while($row = mysql_fetch_array($sql))
{
header("content-type :image/jpeg");
$image = $row['image'];
echo $image;
}
?>
So what this script is going to do is:
On the index page there are 3 pictures, there will be preset slots in the database for them. The user will be able to change these 3 photos anytime by uploading another photo (the upload script will update the old ones with the new ones.) the pictures will be displayed by their ID.
I could simply run 3 queries, 1 for each ID but I was thinking maybe i'm missing an easier way to do it.
Help is very much appreciated...Thank you.
(Yes I am aware of SQL injection as well as deprecation)
Your code is absolutely not correct correct. You have to change the line to :
echo '<img src="data:image/jpeg;base64,'
.base64_encode($image['file_data']).'" width=300 height=200/>";
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
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.
I have this php code that grabs binary( blob ) data from the database and displays as an image in a browser. However, everything after this is ignored. Is there a way that I can also use text queries/forms/tables? Basic HTML elements on the same page?
Having a page that can only display a picture from the DB and nothing else is useless to me.
<?php
$mysqli=mysqli_connect('localhost','root','','draftdb');
if (!$mysqli)
die("Can't connect to MySQL: ".mysqli_connect_error());
$imageid= 1;
$stmt = $mysqli->prepare("SELECT PICTURE_ID FROM display WHERE INDEX_ID =?");
$stmt->bind_param("i", $imageid);
$stmt->execute();
$stmt->bind_result($image);
$stmt->fetch();
header("Content-Type: image/jpeg");
echo $image;
?>
No, you can only have one Content-Type per page.
What you should do is simply create another page, that contains both an image (with src pointing to the code you showed) and the form you want to display, like this:
<html>
<body>
<div>
<img src="image.php" alt="image retreived from DB" />
</div>
<form>
<input type="text" name="foo" />
...
</form>
</body>
</html>
When using a header content of an image type, the only information that can be displayed on this page is the image itself. It is possible though to use a sub-php script inside of a large script, and have the sub-script return the image header, and then have the regular script do other things with this.
Sou you could do this: <img src="\path\to\php\script" /> inside of your other script and it would work as you want it to.
You can't have multiple content-types in one http response.
To make a page with a picture from your DB you can make a regular page and use one of these options for your img tags:
src="your_picture_script.php?pic_id=xxxx" - 'your_picture_script.php' will be pretty much the example in your question.
src="data:image/jpeg;base64,<?php echo base64encode($image);?>" - $image is the actual jpeg file.
Why aren't you displaying the image in an <img> tag? Then writing the other HTML elements below it?
The Content-Type header tells the browser which type of data it is going to display. You can display images in HTML, not HTML in images (unless it's an image of HTML code).
I save my image name to my database and image itself to upload file.
When the user want to see them, I want to show them to user with in a div.
Here things get messy in my mind.
here is my php code
$con = mysql_connect("localhost","root","");
if (!$con){die('Could not connect: ' . mysql_error());}
mysql_select_db("marmaris", $con);
$bilgi= mysql_query("SELECT * FROM ".$isim." WHERE id='".$id."'");
while($sutun= mysql_fetch_array($bilgi))
{
$name=$sutun["name"];
$bures=$sutun["resbuyuk"];
$kures=$sutun["reskucuk"];
$aciklama=$sutun["aciklama"];
$map=$sutun["map"];
$link=$sutun["link"];
}
$words = preg_split('/[\s?]+/', $kures);
$result = count($words);
for($i=0;$i<$result;$i++){
$city=$words[$i];
}
i got 6 images now $city is one of the image forexample i want to show the user that image.
this is my html part don't worry about the isimid name its just sending 2 words at one time.
<div id="kucukres">
<img src="upload/<?php echo $city ?>" alt="Angry face" width="32" height="32" />
</div>
I want to show the image but it only writes the alt words.
In summary, I want to show image that has saved (the name) in database and itself in the upload folder.
With php part, I take the name of the file from database; in html, I want to show that image.
edit :
this is my new html tag.
<div id="kucukres">
<img src="getres.php?file=<?php echo $isimid;?>"/>
</div>
and my php file is
$bilgi= mysql_query("SELECT reskucuk FROM ".$isim." WHERE id='".$id."'");
while($sutun= mysql_fetch_array($bilgi))
{
$kures=$sutun["reskucuk"];
}
$words = preg_split('/[\s?]+/', $kures);
$result = count($words);
//unlink("upload/$word[$isim]");
for($i=0;$i<$result;$i++){
$city=$words[$i];
header('Content-type: image/jpeg');
$pic = 'upload/'.$city;
echo $pic;
but still i cant see the image in my html file.
There is no shortage of tutorials and samples on this subject.
Essentially, what you're trying to do involves two PHP files, not just the one that you have. You'll need one for the page and one for the image (which can be re-used for all images).
Start by taking a look at the HTML that's rendered for your page. What is the img tag referencing? It's looking for a resource (image file) on the server that isn't there, so naturally it's not displaying anything. Your server logs will also show you the 404 errors for those image requests.
This is because the image isn't really part of the page. It's an entirely separate resource that the page is just referencing, the browser's job is to put it in place. So you need to create a separate resource (PHP file) to serve that image.
My PHP is a bit rusty, so I'll defer to the many examples and tutorials online for the specifics. But the structure would basically be as such:
Page A, the host page:
Fetch from the database information about the image. Its ID, its alt text if applicable, etc. Use this information to build the img tag. Something like:
echo '<img src="/images/dbimage.php?id=' . $id_from_db . '" alt="' . $alt_from_db . '"/>';
Page B, the image page:
Fetch the actual image (blob) from the DB and its content-type if that's also dynamic. Write no output before this is done. Set the response header's content-type to the content-type of the image, write the raw image bytes as output, end the output.
The main thing to remember, again, is that the image isn't being sent to the browser as part of the page. It's an entirely separate resource fetched from an entirely separate request to the server. You just need to handle that request separately from the page itself.
(Note, for example, that if you then navigated to the URL generated for the img tag then the server would display just the image. The host page isn't needed.)