How to echo an image using PHP - 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;

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.

custom code for how to upload product image from backend to front end like ecommerce in php

I am new to PHP. I want to know how to be able to upload a product image from the back end to the front end of a static website using PHP. Please help me. Thank you in advance.
Select image :
<?php
if(isset($_POST['Submit1']))
{
$filepath = "images/" . $_FILES["file"]["name"];
if(move_uploaded_file($_FILES["file"]["tmp_name"], $filepath))
{
echo "<img src=".$filepath." height=400 width=300 />";
}
else
{
echo "Error !!";
}
}
?>
I have tried the code above. It is running, but the image is displaying only on the same page. However, I need the image to display on another page when I click on submit button.
You can redirect this page when image is uploaded;
header( 'Location:http://anypage.com/?filepath='.$file_path );
and you can use get method;
$filepath=$_GET['filepath']
echo "<img src=".$filepath." height=400 width=300 />";
You can NOT pass the variable from a page to another until you use Session in PHP. But that is not a good practice for product image.
You should store your $filepath in the database and fetch it every where in your application when you need that.

how do i echo an image in another page based on its id?

How do I get my image id to be echoed in another page?
Page Portfolio:
[ image one ]
[ image two ]
[ image one ] has id of image1. When clicking on image1, it will direct to a new page to display in a larger size.
so far, here is my code.
Page Portfolio
<img src="image1.jpg"/>
<img src="image2.jpg"/>
view.php
<?php $id=$_GET['id']; ?>
<?php if($id == 'image1'){
echo '<img src="orange.jpg"/>';} ?>
<?php if($id == 'image2'){
echo '<img src="milk.jpg"/>';} ?>
It is acceptable if I use this code for a few pictures, but I am going to use it for a lot of pictures. Any suggestion or tips? Is it possible to echo based on the id of the images?
Any help is appreciated. Thank you for your time.
I'm really sorry that I forgot to mention that I am not getting the image from database. Please take a look at the view.php I've edited above. I'm not sure if I explain myself clear enough.
Try this
<?php $id=$_GET['id'];
echo '<img src="'.$id.'.jpg"/>';
?>
You could use the GET value as part of the src attribute, like so:
<?php
echo '<img src="' . $_GET['id'] . '".jpg" />';
?>
But note that this requires every image to have the same jpeg extension.
You CAN'T echo img src=... in the php because you already doing this in the original HTML as a source, you need to print the picture.
//phpFile.php
<?php echo "<img src..">;
will be parsed as an HTML document, so in the html
the source will be an html document and NOT an Image.
you need to replace img to iframe or
use this in the php
$file = $_GET['id']; //or full path to file
$type = 'image/jpeg';
header('Content-Type:'.$type);
header('Content-Length: ' . filesize($file));
readfile($file);
While it might be a bit overkill and the other answers having a more straight approach, I figured I'd give you this option as well. (This is for your view.php file of course :) )
<?php
$id = $_GET['id'];
$validImages = array
(
'image1.jpg',
'image2.jpg',
'image3.jpg'
);
if(in_array($id,$validImages)){
<-- DISPLAY IMAGE HERE -->
}else{
die('Invalid image');
}
And if you desire #Daniel Krom's edition with the header(); part it's just a simple matter of putting it inside of the if(){} clause instead of the echo :)
Thank you for all your suggestions! I manage to get it work using the following codes. :) - Nurul
Page Portfolio
<img src="image1.jpg" id="image1"/>
<img src="image2.jpg" id="image2"/>
view.php
<?php $id=$_GET['id']; ?>
<img src="<?php echo $id; ?>.jpg"/>
(if and only if the extension of all images are the same just like FDekker said.)

echo images using php

what am i trying to do is echo an image using php,
my code is really simple..
i have stored the path of the image in mysql db..
the path of the image is: ../users/profiles/23/images/dps/1409947526.jpg
now i am using the following code to output this picture:
mysql_connect("localhost", "root", "") or die("error!");
mysql_select_db("xone");
$query = mysql_query("SELECT * FROM userdpcover WHERE id='23'");
$result = mysql_fetch_array($query);
$dir = $result['dp_address'];
$dp_name = $result['dp_name'];
$dp = $dir.$dp_name;
echo $dp;
echo "<img src='$dp' />";
but when i run this code, all i get is an broken image!
thanks in advance!
Have you tried this?
echo "<img src='".$dp."' />";
Please try this and please check your image path url correct....
<?php
//here your code
?>//close php tag and try this ...
<img src="<?php echo $dp; ?>"/>
<?php
//your code here
?>
View the source of the output HTML. This will tell you the image path. You probably need to modify it.
Also, remember to set your base URL in a PHP config file to avoid repeating URL paths.

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