issue with blob image from mysql database - php

i have store an image into my database.
but i am unable to display the image what i have done till now is under.... any help will be appreciated. thanks in advance!
<?
$query="SELECT * from testimonial";
$ret = mysqli_query($mysql,$query);
if (isset($ret) && $ret->num_rows>0)
{
while($row=mysqli_fetch_array($ret))
{
$body=$row['body'];
$name=$row['name'];
$image=$row['img'];
?>
<li>
<div class="frame-icon"><? echo "<img src=test_img.php?id=".$row['id']." width=150 height=150/>";?></div>
<p class="quote"><?php echo $body; ?><span><?php echo $name; ?></span></p>
</li>
<?php }
echo "</table>";
}
?>
and my test_img code is
<?
<?php if (isset($_GET['id'])){
$id=mysql_real_escape_string($_GET['id']);
$query=mysql_query("SELECT *FROM testimonial WHERE id='$id' ");
while($row = mysql_fetch_assoc($query))
{
$image=$row["img"];
}
header("content-type: image/png");?>
hi Abhik i have try this one but i get something like this
and in my case i have png image so just change the jpeg to png the code you have given
but i got this <img src="data:image/png;base64,iVBORw0KGgpcMFwwXDANSUhEUlwwXDBcMGRcMFwwXDBkCAZcMFwwXDBw4pVUXDBcMFwwCXBIWXNcMFwwCxNcMFwwCxMBXDCanBhcMFwwCk9pQ0NQUGhvdG9zaG9wIElDQyBwcm9maWxlXDBcMHjanVNnVFPpFj333vRCS4iAlEtvUhUIIFJCi4AUkSYqIQkQSoghodkVUcERRUUEG8igiAOOjoCMFVEsDIoK2AfkIaKOg6OIisr74Xuja9a89+bN/rXXPues852zzwfACAyWSDNRNYAMqUIeEeCDx8TG4eQuQIEKJHBcMBAIs2Qhc/0jAVww+H48PCtcIsAHvlwwAXjTCwhcMMBNm8AwHIf/D+pCmVxcAYCEAcB0kThLCIAUXDBAeo5CplwwQEYBgJ2YJlNcMKAEXDBgy2Ni41wwUC1cMGBcJ3/m01wwgJ34mXsBXDBblCEVAaCRXDAgE2WIRFwwaDtcMKzPVopFXDBY...6B2flQRKMpIP+DXCJ7HsLarL6Op8HdHo/cKIqsCFA4DsBDRQIS30pzU8aoX9JqqWYOntZjRPR6URKyCQVTCOGPXDDMiu8BWVM0P8z8cAzyOrJpdSgciP8+C8AX4VVUgLKOiHrSGqWC6QdFLGWnXDDsXCeivfHIulwiVdcZGHyV9mig3UVcMJKeKGdE9F/oUmo/Dlww+FmRXhaI6N+6UAzZjx8x875m3kOYV2AIXCJa34ViiLYU1XDWiO/o4jBEm9P9GoWpLFwwfUVV7o1CG/J8UW1nbUi5Qy85GdUkXCLPEtFzhQJcIlwiIFwiiMh1XCKyehwb863MfGHeNWQN25BDdoEyX4nB4+tmmtkUNHdwFxFRPxH93MymmNkxRPQGEf23mR0Xq82tfRqHXDDgpeiwnBj7tdvM2MzSVL5l7n+TmXcDWGdmvYWqy/FSWP3/Rkq7Q9AFpEtdQLqAdKkLyNig/xtcMETk0l30p0FqXDBcMFwwXDBJRU5ErkJggg==">

How about if you store the location of the image in DB then store the image itself in the file system? It's a better choice I think.

Proper way to display the blob images stored in DB is
echo '<img src="data:image/jpeg;base64,' . base64_encode($row['img']) . '">';

Related

How to take an image from My SQL database and add it to a .html page

I have some images in a database which I would like to add to a html page
This is my current code
<div class = "gridrow">
<?php
foreach (range(1, 4) as $value) {
$result = $conn->query("select * from products where product_ID = '".$value."'");
$row = $result->fetch_array();
$name_p1 = $row['product'];
$price_p1 = $row['price'];
$image = "<img src='{$row['image']}'>";
echo "<div class = 'productwindow' >";
echo "<div class = 'productimage'><".$image."></div>";
echo "<div class = 'productvar'><p>".$name_p1."</p></div>";
echo "<div class = 'productvar'><p>$".$price_p1."</p></div>";
echo "</div>";
}
?>
</div>
This is what the page looks like
These are the errors I get
How can I make these images show correctly?
You're storing image data in the database, while the img src tag wants image URLs, that's why it's getting confused and you're getting errors.
The quick way around it is to convert the image data to base64 and pipe it in the src tag like so:
$image = '<img src="data:image/png;base64,'.base64_encode($row['image']).'">';
This is at best a hack, and not a great idea for a host of reasons, it also assumes all your images are PNG.

PHP echo to display image HTML

I am trying to display an image on my webpage using a PHP script to determine which image is displayed.
The image link is as follows:
......
My PHP script is thus:
<?php
$result = $_GET['image'];
echo '<img src="images/gallery/'.$result.'.jpg">';
?>
So what I am trying to achieve in terms of HTML is:
<img src="images/gallery/image01.jpg">
The result I am getting is '"; ?>' displayed on the page.
Any help would be much appreciated!
You have to change your code like this
<?php
$result = $_GET['image'];
?>
<img src="images/gallery/<?php echo $result; ?>.jpg">
<?php
$result = filter_input ( INPUT_GET , 'image' );
if (isset($result) && !empty($result)) {
echo '<img src="images/gallery/'.$result.'.jpg">';
}
?>
You used echo wrong, here is how you should use it.
<?php
$result = $_GET['image'];
?>
<img src="images/gallery/<?php echo $result ?>.jpg">
I would change the gallery.php to this:
<?php $result = $_GET['image']; ?>
<img src="images/gallery/<?php echo $result; ?>.jpg">
That would simply it a little bit. You should echo out the result to see what you are getting when the variable is passed to the gallery page.
echo"<img src='{$image}'>";
$image = uploads/myImage.jpg
I think this is the simplest code. To use a php variable while echoing out html, use curly {} brackets to insert any php variable. For instance, a file upload...
<?php
if(isset($_POST['submit'])){
$filename=$_FILES['file']['name'];
$temp_dir=$_FILES['file']['tmp_name'];
$image = "img/".$filename;
}
?>
<?php if($row2['pack1']==1){ echo "<img src=".BASE_URL."images/1seo.png"; } ?>

php image checkbox deleter

I am having trouble making a image deleter in php i do not know what is wrong
PHP:
<?php if (isset($_POST['dcheck'])) {
$img_dir = "Uploads/";
$image = $_POST['dcheck'];
mysql_query("DELETE FROM Photos WHERE PhotoNumber = '".$image."'") or die(mysql_error());
echo 'The image(s) have been successfully deleted.';
} else{
echo 'ERROR: unable to delete image file(s)!';
}
?>
HTML:
<form action="Admin3.php" method="post">
<?php
while($check = mysql_fetch_array($query2)){
echo '<img class="images2" src= "/PhotographyMML/Uploads/resized' . $check['PhotoName'] . $check['PhotoType'] . '" height="100" width ="100" ><input type="checkbox" name="dcheck[]" value="'. $check['PhotoNumber'] .'" />';
}
?>
<input type="submit" value="Delete Image(s)" />
</form>
Your dcheck variable is an array. You will want to create an outer loop around the existing query code you have and foreach through the array, deleting each time.
<?php if (isset($_POST['dcheck'])) {
$img_dir = "Uploads/";
foreach ($_POST['dcheck'] as $image) {
mysql_query("DELETE FROM Photos WHERE PhotoNumber = '".$image."'") or die(mysql_error());
echo 'The image(s) have been successfully deleted.';
} else{
echo 'ERROR: unable to delete image file(s)!';
}
}
?>
A small optimization would be to alter the query so it uses WHERE A small optimization would be to alter your delete query so that it uses WHERE PhotoNUmber IN (1, 2 ...).
This would cause your deletion to happen in one query rather than N queries.
What seems to be missing is any code to actually remove the original file you're alluding to. That would require some sort of file deletion function typically utilizing http://php.net/manual/en/function.unlink.php

Display an Image from PHP mysql table

I have a PHP script to echo the contents of a Mysql table.
I'm using it as a small CMS for a static page.
What I want to know is how can I go about displaying an Image in my PHP script.
I have a form that submits the Date, Title, Message, and Image.
I'm using the Image field to insert the URL of an image.
Whats the correct way of displaying the image on a page.
Below is my code:
<?php
$con = mysql_connect("localhost","name","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("databaseName", $con);
$result = mysql_query("SELECT * FROM Blog");
while($row = mysql_fetch_array($result))
{
echo $row['Date'];
echo "<br />";
echo $row['Title'];
echo "<br />";
echo $row['Message'];
echo "<br />";
echo $row['Image'];
echo "<br />";
echo "<br />";
echo "<br />";
}
mysql_close($con);
?>
This is the outcome:
17th Feb
Title
Here is my Message
http‍://vickybeeching.com/blog/wp-content/uploads/2011/09/canthearyou.jpeg
18th Feb
Title
Here is my Message
http‍://vickybeeching.com/blog/wp-content/uploads/2011/09/canthearyou.jpeg
but I want the page to display the image not the URL.
I'm guessing just using the <img></img> tags, but I'm not sure where in the PHP.
You can write code in image tag instead of echo directly.
Try this code.
echo "<img src='".$row['Image']."'/>";
Try this code, but give image folder path perfectly
<?php
$imagepath = "../uploads/";
echo "<img src='".$imagepath.$row['Image']. "' alt='' height='200' width='200' /> ";
?>
According to need , you can change height and width of the image. but clarity differs.
Thanks
echo $row['Image'];
replace like this
echo '<img src="'.$row['Image'].'" alt="" />';

image loaded from database does not show

guys.
I tried to load image stored in mysql blob field with php, but the image does not show correctly. In firebug, I got these infos: get-image.php Dimensions0 × 0File size5.35KBMIME typeimage/jpeg
Here is my code
HTML
<html>
<head>
<title>Demo of Database Image in a page</title>
</head>
<body>
Here is your picture:<br>
img src=get-image.php?id=1 width=400 height=300><br>
</body>
</html>
PHP
<?php
include "db.php";
$conn = OpenDbConnection();
$key = $_GET["id"];
$tkey = "" . $key . "";
$strsql = "SELECT * FROM `images` WHERE `image_id` = " . $tkey;
$rs = mysql_query($strsql, $conn) or die(mysql_error());
if (!($row = mysql_fetch_array($rs))) {
die("File not exists.");
}
header("Content-type: image/jpeg");
echo $row["content"];
mysql_free_result($rs);
mysql_close($conn);
?>
Please someone tell me what is wrong with my code?
Please try this code.
Instead of
echo $row["content"];
Use this code
?>
<img scr="<?php echo $row["content"];?>" />
<?php
Thanks,
Kanji
Maybe it's because of blog type. Whenever you upload an image which exceed the limit of blob, then image not displayed correctly. Try to change type from blob to long blob.

Categories