Displays an image from the database, Picture does not Show using php - php

Good evening experts :), i am learning to display images from database using php, i got problem. No error message but image not showing, please help.
this my code headline.php :
<?php
include 'koneksi.php';
$tabel = 'isiberita';
$sql=$conn->query("SELECT * from $tabel order by tanggal desc limit 3");
while ($tampil = $sql->fetch(PDO::FETCH_ASSOC)) {
echo "<p align='justify>'";
echo "<img src='$tampil[gambar]' aligen='left'>";
echo "<font valign='top'>";
echo "<strong>";
echo $tampil['judul'];
echo "</strong>";
echo $tampil['tanggal'];
$data=substr($tampil['isi'],0,200);
echo $data;
echo "<a href='index.php?menu=detail_headline&id=$tampil[id_berita]'> baca lengkap>>>></a>";
echo "</font></p><br>";
}
?>
This my image directory :
C:\xampp\htdocs\portalberita\gambar
This my headline.php directory :
C:\xampp\htdocs\portalberita
This my value database :
This output :

To show image from a directory in <img> tag, you have to specify the image path and name like:
<img src="/images/imagename.ext"> <!-- If image folder exist on the same path where the code file exists -->
or
<img src="../images/imagename.ext"> <!-- If image folder exist one level up where the code file exists -->
so check the image folder and your code directory and fix the path accordingly.

Try replacing
echo "<img src='$tampil[gambar]' aligen='left'>";
with
echo '<img src="' . $tampil['gambar'] . '" align="left" />';

Related

Why can't I echo using an img tag?

I'm trying to echo my uploaded image from database using img tag and php variable. I've tried this code but it does not display the image at all,
but the other code works fine, the original of the path of the image is https://localhost/admin/uploads
here is my query for file upload and print screen of my code and output,move to upload file,fetching image,output
if($query_run)
{
move_uploaded_file($_FILES["emp_image"]["tmp_name"],'/uploads/'.$_FILES["emp_image"]["name"]);
$_SESSION['success'] = "Employee Record Added";
header('Location: create.php');
}
here is my code to fetch the image
<?php
while($row = mysqli_fetch_assoc($query_run))
{
?>
<td><?php echo $row['id']?></td>
<td><?php echo '<img src="uploads/'.$row["emp_image"].'" width="100px;"
height="100px;" alt="Image"/>'?></td>
<td><?php echo $row['s_name']?></td>
Your code looks good. You should checkout something...
Is output of 'uploads/'.$row["emp_image"] actual path name of image file?
Remove ; in width="100px;" height="100px;"
Check query and check in emp_image image path is correct or not.
Better use like this.
Note: First you have to print the $row["emp_image"] then ensure the path is correct.
<td><img src="uploads/<?php echo $row["emp_image"]; ?>" width="100px;"
height="100px;" alt="Image"/></td>
I am confusing with the single quotes and double quotes between your code. so better use this way.

Retrieve Image from database, display and attach it to a link

I have Images stored in my DB, I want to retrieve them all and display them in the browser. It works just fine, but I want to give the user the ability to click on the picture so that they can have a better view of the picture (so display it with its original size). I tried many things but didn't work. second question: if I want my code to support multiple images type (such gif, jpg, etc..), if there a way to do it without having to save the image type (when I insert the images) and play with a whole bunch of if/else (when I retrieve them)?
This is my code
$count = 0;
echo " <div class=\"row\">";
while($row = $result->fetch_assoc()) {
$imagename = base64_encode( $row['Image'] );
if(($count%3) ==0){
echo "</div>";
echo " <div class=\"row\">";
echo " <div class=\"col-sm-2\">";
echo " <a href=\"$imagename.jpeg\" class=\"thumbnail\">";
echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['Image'] ).'" style=\"width:130px;height:130px\"/>';
echo"</a></div>";
++$count;
}else{
echo " <div class=\"col-sm-2\">";
echo " <a href=\"$imagename.jpeg\" class=\"thumbnail\">";
echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['Image'] ).'" style=\"width:130px;height:130px\"/>';
echo"</a></div>";
++$count;
}
}
echo "</div>" ;
The answer to your first question is "You have to use light-box jQuery plugin", in order to display larger (original size) of a thumbnail you need to use any lightbox plugin, I recommend using this one
http://www.jqueryscript.net/zoom/Responsive-jQuery-Lightbox-Like-Image-Zooming-Plugin-Lighter.html
Or you can search for any other plugin, there are thousands of them.
Regarding your second question, If you will use data as an images source, you have to specify a type, no way you can do that without mentioning types.

PHP Code not binding with img src Tag

I have Tried Many Solution , Inside and Outside PHP Code , None of them are working
I am able to fetch Image Path from Array But Not able to assign it ..
I Tried following solutions :
foreach ($data as $item) {
echo "Image Path is ".$item[0]["Image"] ; //--> Returning - img01.jpg
$imageNumber= "img01";
//sol 1.
echo '<img src="'.$item[0]["Image"]'">';
//sol 2.
// echo "<img src=\"{$imageNumber}.jpg\">";
//sol 3.
//echo '<img src="'.$coverlink.'" alt="Cover">';
}
//sol 4.
// <img src="<?php echo $imageNumber ?>.jpg">
//sol 5.
// <img src="<?php echo $item[0]["Image"]; ?>">
I need some guidence what I am missing ?Any Help would be appreciated
From what you've posted, it seems like $item[0]["Image"] returns the image name along with the extension. E.g: img01.jpg.
So, you can simply put this within the img src.
foreach ($data as $item) {
echo "<img src='".$item[0]["Image"]."' />";
}
If your question is explained clearly, then this should work just fine.

How to echo an image using PHP

How can I echo an image using PHP?
This is what I have:
echo "<img src="Images/Picture.GIF">";//This should echo my image
If by "echo" you mean outputting the image in a browser, you need to read it first then send it with echo. Something like this should work:
$content = file_get_contents('Images/Picture.GIF');
header('Content-Type: image/gif');
echo $content;
You cant echo an image using PHP.
PHP echo
echo — Output one or more strings
echo is for strings only.
However, you can echo the image source - img src=""
Just make sure you put the picture extension at the end of the file you are grabbing. - .jpg .png etc.
You just need to grab the image source from somewhere.
Example (using $_GET):
<?php
if($_SERVER['REQUEST_METHOD']=='GET'){
$img_src=$_GET['img_src']
}
echo "<img src='/images/test/" . $img_src . "' alt='img'>";
?>
Upload the image in your media files
Get the url/path of the uploaded image
Do this!
$image='<img src="https://www.yoursite.com/wp-content/uploads/2022/02/dope.png"/>';
echo $image;

How to echo images from a separate directory?

So I have now worked out how to echo images from MySQL.
Now I want to work out a way to echo images from a separate directory?...In this case WordPress.
My goal is to grab any posts on my WordPress blog which have a featured image and echo them onto a page of my choice.
So here is what I have so far:
echo "
<div class=\"pagination\" style=\"display:inline\"><ul style=\"background-color:#\"><li><div class=\"span3_search\"><h2><a href='$link'><b>$title</b></a></h2><br><a href='$link'><img id=\"result_img\" src=\"$image\" /></a><br /><p>$description</p><br />
<a href='$link'>$link<br /><br /></a><p></div></li></ul></div>
";
how do I echo a image src which is the source to where my featured images are.
i.e
echo "<img src="PHP WHICH SAYS GRAB WORDPRESS FEATURED IMAGES FROM THIS DIRECTORY etc">";
You can fetch the image of the path and then append a new path;
$image = explode('/');
$image = $image[count($image)];
echo 'mynew/path/toimage/'.$image;
To see al parts of the path, do;
echo '<pre>';
print_r($images);
echo '</pre>;

Categories