how to show thumb folder images in php
i have a folder name albums
and in album each folder have photos & thumb folder photos
example
uae/thumb/1.jpg
uae/1.jpg
uk/thumb/1.jpg
uk/1.jpg
usa/thumb/1.jpg
usa/1.jpg
and my code is showing this photos only
usa/1.jpg
uk/1.jpg
uae/1.jpg
and i want to show only thumb photos
usa/thumb/1.jpg
uk/thumb/1.jpg
uae/thumb/1.jpg
how can i do this
please help me to fix this issue
thanks in advance
here is code
<?php
//path to directory to scan. i have included a wildcard for a subdirectory
$directory = "albums/*/";
//get all image files with a .jpg extension.
$images = glob("" . $directory . "*.jpg");
array_multisort(array_map('filemtime', $images), SORT_DESC, $images);
?>
<?php $num_of_files = 0; $i=0; foreach ($images as $image):?>
<div class="item"><a href="<?php echo basename(pathinfo($image, PATHINFO_DIRNAME)); ?>.html" target="_blank">
<img class="lazyOwl" src="<?php echo $image ?>" />
<p><?php echo basename(pathinfo($image, PATHINFO_DIRNAME)); ?></p>
</div>
<?php if (++$num_of_files == 3)break; ?>
<?php if(++$i%3==0): ?>
<?php endif ?>
<?php endforeach ?>
Adjust your directory path:
$directory = "albums/*/thumb/";
//get all image files with a .jpg extension.
$images = glob($directory . "*.jpg");
... or try using SPL tools:
$directory = new RecursiveDirectoryIterator('albums/*/'); // or ('albums/*')
$iterator = new RecursiveIteratorIterator($directory);
$images = new RegexIterator($iterator, '/thumb\/\(.jpe?g|.png)$/i', RecursiveRegexIterator::GET_MATCH);
$images = iterator_to_array($images);
...
// iterating over $images
Related
I read the article How to display images from a folder in PHP on https://codehasbug.com/php/display-images-from-folder/. In the line, where you get the image directory, I would like to get the value of an acf field, which value is "/wp-content/uploads/images/", but it doesn't work. It would be wonderful, if you have any advice for me.
<?php
//get current directory
$working_dir = getcwd();
//get image directory
$img_dir = $working_dir . get_field_object('image_url'); // instead of "/images/";
//change current directory to image directory
chdir($img_dir);
//using glob() function get images
$files = glob("*.{jpg,jpeg,png,gif,JPG,JPEG,PNG,GIF}", GLOB_BRACE );
//again change the directory to working directory
chdir($working_dir);
//iterate over image files
foreach ($files as $file) {
?>
<img src="<?php echo $field['value'] /* instead of echo "images/" */ . $file ?>" style="height: 200px; width: 200px;"/>
<?php }
?>
I want to shuffle 2 loops (or function) in the same correct order, in PHP.
The code currently looks like:
<div class="overlay">
<?php
// Get the list of all files with .jpg extension in the directory and save it in an array named $images
$directory = "uploads/_gallery/*.jpg";
// Create an array of images found in the uploads directory
$images = glob($directory);
// Randomise/shuffle the images
shuffle($images);
foreach( $images as $image ):
if ($image) { ?>
<div class="slide">
<?php echo "<img src='" . $image . "'>"; ?>
</div>
<? }
endforeach;
?>
</div>
<div class="grid">
<?php
// Get the list of all files with .jpg extension in the directory and save it in an array named $images
$directory = "uploads/_gallery/thumbs/*.jpg";
// Create an array of images found in the uploads directory
$images = glob($directory);
// Randomise/shuffle the images
shuffle($images);
foreach( $images as $image ):
if ($image) { ?>
<figure class="unit_25">
<?php echo "<img src='" . $image . "'>"; ?>
</figure>
<? }
endforeach;
?>
</div>
I've tried creating two separate image arrays and merging those with Shuffle, but no luck.
I assume the file has the same name in both directory.
So shuffle $images and then use basename($images) to get the filename :
$directory = "uploads/_gallery/*.jpg";
$images = glob($directory);
shuffle($images);
// For full sized images
foreach ($images as $image) {
// Your stuff
}
// For thumbnails
foreach ($images as $image) {
$basename = basename($image);
$thumbnailPath = "uploads/_gallery/thumbs/". $basename;
// Your stuff
}
I'm looking for a way to use the responsive image attribute 'srcset' alongside php. I'm currently using the following code in order to pick a random image from a directory on the server:
<?php
$dir = "img/";
$images = scandir($dir);
$i = rand(2, sizeof($images)-1);
?>
<img src="img/<?php echo $images[$i]; ?>" alt="image">
I've created different versions of the images in the directory with a suffix (i.e., image-1-small.jpg, image-1-big.jpg ... image-2-small.jpg, image-2-big.jpg etc.).
How would I implement the srcset attribute into the php string? I'm looking for an output similar to this:
<img sizes="100vw" srcset="img/image-1-small.jpg 400w, img/image-2-medium.jpg 800w, img/image-1-big.jpg 1600w" src="img/image-1-small.jpg" alt="image-1">
Thanks!
You can try something like this:
<?php
$dir = "img/";
$images = scandir($dir);
$i = rand(2, sizeof($images)-1);
// Get image file name.
$image_name_full = $images[$i];
// Define display types.
$image_display_types = array("-small.jpg", "-medium.jpg", "-big.jpg");
// Remove image display type from image name.
$image_name = str_replace($image_display_types, "", $image_name_full);
?>
<img sizes="100vw" srcset="img/<?php echo $image_name; ?>-small.jpg 400w, img/<?php echo $image_name; ?>-medium.jpg 800w, img/<?php echo $image_name; ?>-big.jpg 1600w" src="img/<?php echo $image_name; ?>-small.jpg" alt="<?php echo $image_name; ?>">
this is the code in order to display a random image on my website,
but for some reason the images are not popping up and there is an error on the line where $rand_image was declared. The error says undefined index. I have 7 images in the directory.
$imagesDir = '\socimages\Badminton'; // my image directory
$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE); // glob
$rand_image = $images[array_rand($images)]; // applying the random function
...
...
<img src="<?php echo $rand_image[0];?>"< alt="" height="246" width="246"></p></div>
$rand_image is the image already you don't need $rand_image[0];
<img src="<?php echo htmlspecialchars($rand_image);?>"< alt="" height="246" width="246"></p></div>
Also, $rand_image will probably be the path on disk, not the web accessible path so you'll need to map it.
I'd do something like this.
$imagesDir = '/socimages/Badminton/'; // my image directory
$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE); // glob
if ($images !== FALSE) {
$rand_image = $images[array_rand($images)]; // applying the random function
} else {
die("This thang ain't working, yo.");
}
...
...
<img src="<?php echo $rand_image;?>"< alt="" height="246" width="246"></p></div>
I wanted to ask how can I populate an HTML list with Images located in a folder automatically?
lets say I have 4 pages with 4 different lists.
lets say the lists are as follows: Cars, Motorcycles, Airplanes, Ships
each list entry has a src as a thumbnail and a href as a link to full size image.
my folder contain images with specific titles:
C=Cars
M=Motorcycles
A=Airplanes
S=Ships
then I have a serial number
and the ending is either
T=Thumbnail
F=Full Image
so a complete file name would be C20T.jpg or C20F.jpg.
I am looking for a script that will update the 4 lists according to their subject and populate them automatically with related file names.
from my search here I have found this piece of code but I believe it needs some tweaking and I am not sure how to do this :(
$dir = '/images';
$files = scandir($dir);
$images = array();
foreach($files as $file) {
if(fnmatch('*.jpg',$file)) {
$images[] = $file;
}
}
foreach($images as $image) {
echo '<img src="images/'.$image.'" />';
}
I hope someone could help me with this!
Thank you for your time reading this.
$type = 'c';
$imagesDir = 'images/';
$images = glob($imagesDir . $type . '*[TYPE].{jpg,jpeg,png,gif}', GLOB_BRACE);
This should get all your full images.
Making a ul is then pretty easy...
<ul>
<?php foreach($images as $image): ?>
<li><img src="<?php echo str_replace('[TYPE]', 'T', $image); ?>" alt="" /></li>
<?php endforeach; ?>
</ul>