populate an HTML <ul> with images from a folder automatically - php

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>

Related

How can I get the image directory from a value of an acf field?

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 }
?>

PHP: Pull all images from a specified directory using relative image path

I am trying to create a gallery where images are automatically pulled from a images directory.
When I foreach() each image it returns however it pulls the entire root path as the image src
/home/dev/public_html/assets/images/
this causes the images not to show and shows dead linked images on screen. I need the relative path like this:
/assets/images/imagename.jpg
How would I set a base path/URL?
$dirname = "/home/dev/public_html/assets/images/";
$images = glob($dirname."*.jpg");
foreach($images as $image) {
echo '<img src="'.$image.'" /><br />';
}
I tried setting $dirname to this
$dirname = "/assets/images/";
But I get no result and nothing pulls through at all.
The file i am creating this in, is located outside the public_html folder, i believe this could be casing the issue but the file cannot be relocated.
I would edit the loop in order to remove unnecessary string from path:
foreach($images as $image) {
$src = str_replace('/home/dev/public_html' ,'', $image) ;
echo '<img src="'.$src.'" /><br />';
}
You could may make use of scandir() http://php.net/manual/en/function.scandir.php
Somethink like:
Edit: Changed the code a bit, so scandir only take files, otherwise the first two placed in the array are folders for the parent folders.
$dir = "assets/images/";
$images = scandir($dir);
foreach($images as $image) {
if($image != '.' && $image != '..') {
echo ' <img src="assets/images/'.$image.'"><br>';
}
}
Hope this helped. If not, let me know, so I can change my answer. But I think it should be fine, as far as I understood what you're trying.

how to show thumb path photos in php

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

Get all images in a directory except retina

I have a php script which grabs all of the images in a set directory. The script works without fault. My issue is that I want it to only get none retina images. All of the retina images are named in the format 'image1#2x.jpg' whereas all of the regular images are named in the format 'image1.jpg'. With this in mind I want my script to discount any image that contains '#2x' in it's name.
My script is:
<?php
$directory = 'images/';
$dh = opendir($directory);
while (false !== ($filename = readdir($dh)))
{
$files[] = $filename;
};
$images = preg_grep ('/(\.gif|\.GIF|\.jpg|\.jpeg|\.JPG|\.JPEG|\.png|\.PNG)$/i', $files);
foreach ($images as $image)
{
echo '<img src="'.$directory.$image.'" alt="">';
}
?>
The problem is I don't have the vocabulary to know what to search for in Google so if anyone could point me in the right direction I would appreciate it.
Thanks
I would use stripos put this conditional in your foreach loop
if(stripos($image,'#2x') === false){
echo '<img src="'.$directory.$image.'" alt="">';
}
You can make your script a little shorter by using:
$directory = 'images/';
$allImages = glob($directory.'*.{jpg,JPG,jpeg,JPEG,gif,GIF,png,PNG}', GLOB_BRACE);
$nonRetinaImages = preg_grep('/#2x/i', $allImages, PREG_GREP_INVERT);
foreach ($nonRetinaImages as $image)
{
echo '<img src="'.$image.'" alt="">';
}

Gallery Is Missing Files From Directory- How To Retrieve All Files From Directory

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;

Categories