Images from mysql does not display using php - php

Image is successfully fetching from database but it not be shown by the PHP. In place of image it shows a iamge thumbnail.
If I use header('Content-type:image/jpg'); it will show the only thumbnail all page contents disappear.
include 'functions/connect.php';
$user = $_SESSION['email'];
$sql = "SELECT photo FROM user WHERE email='$user'";
$run_sql = mysqli_query($conn,$sql);
$row = mysqli_fetch_array($run_sql);
$user_photo =$row['photo'];
echo"<p><img src='$user_photo'></p>";`

You can try something like this:
<img alt="<?php echo $user_photo; ?>" src="uploads/<?php echo $user_photo; ?>" /> // Upload is folder where image was uploaded

It depends on what the field "photo" contains. If the photo field contains the location of the picture on your system, then the way you have done it should work. However, if the field contains the actual photo in binary format (having field data type as 'blob') then you'll need to do the following:
file_put_contents("image.jpg", $user_photo);
echo "<p><img src='image.jpg' ></p>";

Related

Alter profile picture using <img> tag and PHP

I'm still new on php. I'm currently building a website which a user can upload an image to change their profile picture.
The file of image will we insert into table user, row image location on database and directory "upload". The formatting is like abc.jpeg
My code for the <img> tag, which is where I display the profile image. But the image does not appear. Only the thumbnail image is displayed and not the exact image.
<img src=<?php
$current = $fgmembersite->UserEmail();
if ($handle = opendir('upload/')) {
$sql = "SELECT image_location FROM user WHERE email='$current'";
//$file = mysql_real_escape_string($sql);
if ((file_exists('upload/'.$sql) == $sql)) {
echo 'upload/'.$sql.'.png';
} else if (file_exists('upload/'.$sql)) {
echo 'upload/'.$sql.'.jpg';
}
}
closedir($handle);
?> alt="">
If you don't mind, can you check my code and see where I went wrong? Thank you for your helping.

How to display images from a database using PHP?

My program needs to upload images, so an image (varchar) location is saved in a MySQL database. It's working so far.
Now I want to display images and this does not work. Here is the code:
include ('connect.php');
if(isset($_POST['submit'])){
$filetemp= $_FILES['image'] ['tmp_name'];
$filename= $_FILES['image'] ['name'];
$filepath= "images/".$filename;
move_uploaded_file($filetemp,$filepath);
$sql=mysqli_query($con,"insert into images (image) value ('$filepath')");
if($sql){
echo "uploaded";
}
else{
echo " not uploaded";
}
}
$sql=mysqli_query($con,"select * from images");
while($row=mysqli_fetch_array($sql)){
echo "<img src=' images/".$row['image']."'>"; // the problem is here, its just displaying img icon, not actual image
}
?>
remove images from image path, you already store this in image column in images table
echo "<img src='/".$row['image']."'>"; // the problem is here, its just displaying img icon, not actual image
Correction :
$sql=mysqli_query($con,"select * from images");
while($row=mysqli_fetch_array($sql)){
echo "<img src='".$row['image']."'>"; // the problem is here, its just displaying img icon, not actual image
}
Also you need to get the actual path via __FILE__ in case if needed.
You are adding images/ into the database then when you call the row to display the image you're adding images/ adding making it look in images/images/filename

How view image from database by using php

I am using localhost. I insert image in database blob. I am trying for show image. But I am fail. Image does not show. Database have three field id,name,image. Trying this code.
uimage.php
<?php
mysql_connect("localhost","root","");
mysql_select_db("task_database");
$image=mysql_query("SELECT * FROM image WHERE id=1");
$image=mysql_fetch_assoc($image);
$image=$image['image'];
header("Content-type: image/jpeg");
echo $image;
?>
and using this code in index.php page:
echo "Image Uploaded.<p />Your image:<p /><img src=uimage.php>";
But image did not show. Please help me.
My Full Code here: http://codepad.org/hJ8qucml
The easiest type of handling images in PHP-based websites is to save the images with the its id as name in a separated folder and just saving the id in database!
i really recommend you this type of working with images!
These are quick examples and you should validate any data being passed that is being used in a query. Check out Mysqli.
Using inline base64 encoding:
<?php
mysql_connect("localhost","root","");
mysql_select_db("task_database");
$result=mysql_query("SELECT name, myimg FROM image WHERE id=1");
$image=mysql_fetch_row($result);
$imagesrc=$image[1];
echo '<img src="data:image/jpeg;base64,'.base64_encode($imagesrc).'"/>';
Using PHP file to call the image:
<img src="image.php?id=<?php echo $id; ?>" />
image.php
<?php
mysql_connect("localhost","root","");
mysql_select_db("task_database");
$result=mysql_query("SELECT name, myimg FROM image WHERE id=1");
$image=mysql_fetch_row($result);
$imagesrc=$image[1];
header('Content-Type: image/jpeg');
echo $imagesrc;
?>

how to show blob image in html?

I have a image stored in mysql as mediumblob and now I want to show it in html page, so I am doing this with it:
$c = base64_encode($resu[0]->image);
$image = '<img src="data:image/jpeg;base64,'.$c.'" />';
echo $image;
But I am getting only half of original image, so am I missing something here ?
Just an idea, may be you can seperate the logic to display the image.
What I mean is that you can create a file like image.php that accepts the id or filename
of the image and then display the image. Then you can simply refer the image in your HTML
by, for example, doing something like this:
<img src="image.php?imgId=12547"/>
in image.php file something like the following
$imgId=isset(GET['imgId'])?GET['imgId']:0;
$sql = "SELECT * FROM theBlogs WHERE ID = $imgId;";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($result);
header("Content-type: image/jpeg");
echo $row['imageContent'];
Just perform a quick test with strlen on $resu[0]->image variable and check blob size in DB and check if they are different sizes for sure.
Display in li tag means use this
<li data-thumb='<?php echo "data:image/jpeg;base64,".base64_encode($img1 ); ?>'>
<div class="thumb-image">
<img <?php echo 'src="data:image/jpeg;base64,'.base64_encode( $img1 ).'"';?>
data-imagezoom="true" class="img-responsive" alt="">
</div>
</li>

Read multiple image php

I have two separate files, one is to display the html/php document image, and the other is a php file that renders the image using the header function content-type:image/jpeg.
I tried using it with one image and it works well. However, I need to display multiple images. How could I do this?
The html/php doc has an img tag that points out to the php file that renders the image
echo "<image src=Image.php>";
The image.php
$selectimage = mysql_query("SELECT Image from ImageTbl", $con);
if($selectimage)
{
header("Content-type:image/jpeg");
while($row = mysql_fetch_array($selectimage))
{
echo $row["Image"];
}
}
make two files one for image another for fetching the row like this
image.php
$image_id = $_GET["id"];
header("Content-type:image/jpeg");
//query database to get only one image from id
echo $row["Image"];
another file
getimages.php
//query for image data
while($row = mysql_fetch_array())
{
echo "<img src='image.php?id=$row[id]' />";
}
You can't output all the images together, because to the browser, it will look like the data of multiple images mushed together, which is nonsensical. Also, each image tag can only display one image. To solve this, give the image table an ID field to identify the image.
Then in the file that outputs HTML, do something like this (passing the ID for the image you need):
echo "<image src='Image.php?id=1>";
echo "<image src='Image.php?id=2>";
echo "<image src='Image.php?id=3>";
And then in the file that outputs the image, do:
$id = intval($_REQUEST['id']); // intval will validate the ID to be an int
$selectimage = mysql_query("SELECT Image from ImageTbl WHERE id=$id LIMIT 1", $con);
if ($selectimage) {
$row = mysql_fetch_array($selectimage);
if ($row) { // check if the image really exists
header("Content-type:image/jpeg");
echo $row["Image"];
}
}
Use a foreach loop to loop through the requested records and echo them out independently to the img tags which your using.
That would be the best way in my opinion.
If you want to display number of different images using one script, try to add some unique hash to the script name ( for e.g. md5( microtime() ) )
$seed = md5( microtime() );
echo '<image src="Image.php' . $seed . '">';

Categories