Place images from a folder into list with preview - php

I need to build a dynamic image gallery plugin for Joomla! that will go into a specific folder and pull out all images from the folder and show first of them as a large preview image and the rest in a list. Afterwards I will need to make the preview image open in a lightbox if clicked and in the lightbox I need to have the small thumbnails of listed images as well.
But know I need just php to go to the folder and pull out all the images from the folder specified. I have googled a bit and found some solution but this does not work for some reason I don't understand. Could someone tell me please, what is wrong with the code?
Thanks!
<div id="images">
<?php
$images_dir = 'images/';
$scan = scandir($images_dir);
echo '<img src="' . $images_dir . $scan[2] . '"alt="image" />';
?>
<ul id="smallimages">
<?php
for($i=0; $i<count($scan); $i++){
$ext = substr($scan[$i], strpos($scan[$i], '.'), strlen($scan[$i]-1));
$filetypes = array('.jpg', '.JPEG', '.jpeg');
if(in_array($ext, $filetypes)){
echo '<li><img src="' . $images_dir . $scan[$i] . '" alt="' . $scan[$i] . '"></li>';} }?></ul>
</div>

I think it's because you haven't defined the path correctly. Try using the following:
$images_dir = JUri::root() . 'plugins/content/plg_new/images';
JUri::root() is the root of your Joomla site so change the path from there on accordingly to where ever your images are located
Hope this helps

Does this work for you?
<?php
$image_directory="hrevert/images/";
$pictures = glob("$image_directory*.{gif,jpg,png}", GLOB_BRACE);
//displays the first image
$first_img=reset($pictures);
echo "<img src=\"".$first_img."\" width='20px' />";
//loops through other images and prints them
echo "<ul>";
foreach($pictures as $picture) {
echo "<a href='$picture'><img src=\"".$picture."\" width='20px' /></a>";
}
echo "</ul>"
?>

Related

Echoing image tag in PHP using path

<?php
$dir= 'C:\xampp\htdocs\img';
echo "<img src='".$dir."\america.jpg' alt='icon'>";
?>
I just written this code for test and I couldn't get able to display image on browser.
The path you are using is not correct, try this:
$path = '/img/';
$imgName = 'america.jpg';
echo '<img src="'. $path.$imgName .'" alt="icon">';
// If img folder is present in the same directory
or
$path = '../img/';
$imgName = 'america.jpg';
echo '<img src="'. $path.$imgName .'" alt="icon">';
// If img folder is present one directory upwards from current directory

PHP no image display from directory

When I run the PHP file, the image is not show, instead just the altattribute value shown which is 14.JPG . The page source is also accurate and the image is also exist in that folder.
Note - there are also some echo statements before this code. they are for other purposes.
Do I need to use headers also when displaying image this way?
$dir = "C:/xampp/htdocs/PHP";
$file = "14.JPG";
if ( file_exists($dir) == false ){
echo 'Directory \''. $dir. '\' not found!';
}
else{
echo '<img src="'. $dir. '/'. $file. '" alt="'. $file. '"/>';
}
The problem is you're getting the file path mixed up with the URL. The file path is C:/xampp/htdocs/PHP and the URL is http://localhost/PHP. You need to use the URL when setting links to file and images. Your image source should be http://localhost/PHP/14.JPG but you've set it to C:/xampp/htdocs/PHP/14.JPG. Change it and it'll work for you.
You apply wrong approach please use relative url make uploads folder in your PHP folder
$dir = "uploads/";
$file = "14.JPG";
if ( file_exists($dir) == false ){
echo $dir .'not found!';
}
else{
echo '<img src="'. $dir. '/'. $file. '" alt="'. $file. '"/>';
}
Change This
$dir = "C:/xampp/htdocs/PHP";
To This
$dir = "PHP/";

using 'glob' to get all images from a directory

I'm trying to get all images from a directory and print them. I know my URL is correct because I am echoing the the $dirname along with a .jpg image I have saved in the directory. What am I doing wrong?
<?php
$dirname = "uploads/{$site_country}_{$site_state}_{$site_name}/{$record_id}/";
$images = glob($dirname . "*.jpg");
foreach($images as $image) {
echo '<img src="'.$image.'"/>';
}
//check to see if $dirname is correct
echo '<img src="' . $dirname . "IMG_0002.JPG" . '"/>';
?>
//edit
I have solved my problem, by removing the jpg extention and just pulling "*" for everything in the folder. Although it works for me now, its not best practice. It must be something to do with different .jpg file formats?
You used lowercase file extension here.
$images = glob($dirname . "*.jpg");
And uppercase JPG in your test output.
echo '<img src="' . $dirname . "IMG_0002.JPG" . '"/>';
If you're using a linux system then file extensions are case sensitive.
You need to prepend the dirname to glob's result to get the full paths.
So something like:
$dirname = "uploads/{$site_country}_{$site_state}_{$site_name}/{$record_id}/";
$images = glob($dirname . "*.jpg");
foreach($images as $image) {
echo '<img src="'.$dirname.$image.'"/>';
}

Loop through directory

I'm trying to use PHP to bring in every image from a specific directory and turn them into HTML objects by wrapping some markup around them.
Right now I am just trying to get the images on the page. In the future I would also like to bring in a text file that has the same file name as the image(slide#4.jpg and slide#4.txt).
This is what I have so far.
<?php
$dir = "includes/pages/products/*.jpg";
$images = glob( $dir );
foreach( $images as $image ):
$file = basename($image);
$file = basename($image, ".jpg");
echo "<div class='"prod-cont"'>" . "<h4>" . $file . "</h4>" . "<img src'" . $image "' />" . "</div>";
endforeach;
?>
I'm not having much luck getting this going, any help is appreciated.
Remove the second $file = line, and make sure you're generating correct HTML.
echo "<div class='prod-cont'><h4>".$file."</h4><img src='".$image."' /></div>";

Displaying Images from a folder in PHP

I am currently creating an e-commerce website where I retrieve the vehicle details from the database and search for its image in a folder. I used the code below for searching the folder for the image, but it only show the name of the JPG file and not the image of it. Could anyone help to to determine how I could display the image of the vehicle
$imageName=$_POST['car'];
if ($handle = opendir('Images/$imageName')) {
$dir_array = array();
while (false !== ($file = readdir($handle))) {
if($file!="." && $file!=".."){
$dir_array[] = $file;
}
}
echo $dir_array[rand(0,count($dir_array)-1)];
closedir($handle);
}
You have to put html tags around the result. Try:
echo "<img src='PATH_TO_IMAGE/".$dir_array[rand(0,count($dir_array)-1)]."'>";
where PATH_TO_IMAGE is the relative/absolute url address to your images folder
Ex
echo "<img src='/images/".$dir_array[rand(0,count($dir_array)-1)]."'>";
You need to print the path to the image inside an img HTML tag, otherwise it is just plain text.
echo '<img src="Images/' . $imageName . $dir_array[rand(0,count($dir_array)-1)] . '" alt="" />';
I hope you're doing some validation/sanitisation on $_POST['car'] somewhere?
Hmm... If you have the name of the JPG, use <img src="NAME_OF_THE_IMAGE.JPG" /> ...

Categories