How to echo images from a separate directory? - php

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>;

Related

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.

Displays an image from the database, Picture does not Show using 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" />';

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.

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;

Wordpress PHP replace image with site title if default value is empty

I am making options in the 'customization api' for Wordpress. However for things such as the logo image, if no image has been uploaded (default value is blank) i want it to display the title of the Wordpress site. I have searched google a lot, but I just cant seem to get it to work.
I know the PHP code for the Wordpress site title, but I can't get the php 'if' and 'else' to work.
current code:
<img src="<?php echo get_theme_mod('logo_image'); ?>" alt="<?php bloginfo('name'); ?>"/>
..obviously this is just the option to upload the image, which WORKS, but I want it to display the site title if the default value is empty.
Thank you!
I don't know how you are setting the theme mod. I.e. are you using 'set_theme_mod' ?
But assuming you are complying to WordPress standards the code snippet below should be what you are seeking.
if (!empty(get_theme_mod('logo_image'))) {
echo "<img src='" . get_theme_mod('logo_image') . "' title='" . bloginfo('name') ."'>"
} else {
echo "<h2>" . bloginfo('name') . "</h2>";
}
Hope this helps you out,
Dave.
Can you try this?
<img src="<?php if (!isset(get_theme_mod['logo_image'])) {
echo ('Your custom image');
}
else{
echo get_theme_mod('logo_image');
}
?>" alt="<?php bloginfo('name'); ?
>"/>

Categories