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
}
Related
$dirname = "media/images/iconized/";
$images = glob($dirname."*.jpg");
foreach($images as $image) {
echo '<img src="'.$image.'" /><br />';
}
With the above code I can fetch all images in a folder.But, I am not able to fetch particular images from a folder.
For example:
I have images in a folder as:
1. low_0.jpg
2. low_1.jpg
3. low_2.jpg
4. med_0.jpg
5 med_1.jpg
6. med_2.jpg
From these images how to grab only the images that starts with med? Thanks for your time
Change the glob() command from:
$images = glob($dirname."*.jpg");
To:
$images = glob($dirname."med_*.jpg");
The * is a wildcard; it'll fill in the gap (in this case, numbers).
Full code:
$dirname = "media/images/iconized/";
$images = glob($dirname."med_*.jpg");
foreach($images as $image) {
echo '<img src="'.$image.'" /><br />';
}
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
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; ?>">
I finally got my code to populate a gallery from a directory while using fancybox. It's a basic thumbnail gallery that shows the larger image upon clicking. The only problem is that it is missing a lot of files from the thumbnail directory.
The code retrieves all of the links for the larger images but it won't retrieve ALL of the thumbnails, only a few of them and they're not even in order.
What is wrong in my code?
<?php
$directory = 'thumb'; //where the gallery thumbnail images are located
$allowed_types=array('jpg','jpeg','gif','png');//allowed image types
$file_parts=array(); $ext=''; $title=''; $i=0;//try to open the directory
$dir_handle = #opendir($directory) or die("There is an error with your image directory!");
while ($file = readdir($dir_handle)) //traverse through the files
{ if($file=='.' || $file == '..') continue; //skip links to the current and parent directories
$file_parts = explode('.',$file); //split the file name and put each part in an array
$ext = strtolower(array_pop($file_parts)); //the last element is the extension
$title = implode('.',$file_parts); //once the extension has been popped out, all that is left is the filename
$title = htmlspecialchars($title); //make the filename html-safe to prevent potential security issues
natsort($file_parts); //sort by filename--NOT WORKING
$nomargin='';
if(in_array($ext,$allowed_types)) //check if the extension is an allowable type
{
if(($i+1)%4==0) $nomargin='nomargin'; //the last image on the row is assigned the CSS class "nomargin"
//Begin thumbs containers with fancybox class
echo '<div class="thumbs fancybox '.$nomargin.'" style="background:url('.$directory.'/'.$file.') no-repeat 50% 50%;"> <a rel="group"
href="images/'.$file.'" title="'.$title.'">'.$title.'</a>
</div>';
$i=0; //increment the image counter
} } closedir($dir_handle); //close the directory
?>
An alternative approach you may want to use to secure your list of thumbnails:
$directory = 'thumb'; //where the gallery thumbnail images are located
$files = glob($directory."/*.{jpg,jpeg,gif,png}", GLOB_BRACE);
natsort($files); //sort by filename
Then to render it out simply do this:
<?php
for($x = 0; $x < count($files); $x++):
$thumb = $files[$x];
$file = basename($thumb);
$nomargin = $x%4 == 0?" nomargin":"";
$title = htmlspecialchars($file);
?>
<div class="thumbs fancybox<?= $nomargin ?>"
style="background:url('<?= $thumb ?>') no-repeat 50% 50%;">
<a rel="group" href="images/'.<?= $file ?>" title="<?= $title ?>"><?= $title ?></a>
</div>
<?php
endfor;
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>