Get images one by one from directory - php

in php i want to ask how to get images one by one from folder directory and display in the jquery slider i have tried this php code by it's not working as i want?
<ul class="jcarousel-list">
<li class="jcarousel-item">
<?php
$directory = "data/uploads/topslider/";
if (glob($directory . "*") != false)
{
$filecount = count(glob($directory . "*"));
}
else
{
}
$files_index = glob("data/uploads/"."top"."slider/*.*");
for ($i=0; $i<$filecount; $i++)
{
$num2 = $files_index[$i];
?>
<img src="<?php echo $num2;?>" width="50" height="50" alt="" /> <?
}?></li>
</ul>
i want display like this:
Image1 Imag2 Image3......and So On from single folder or directory

There are four main things to check:
Is your $directory path correct relative to your current working directory in PHP? You can determine your current working directory by doing a temporary echo getcwd();. It needs to return the parent directory of your "data" folder for your code to work.
Is the data folder accessible from the current page on the web? e.g. if you manually remove the page from the URL (say, index.php) and add data/uploads/topslider/someimage.png where someimage.png is an image you know exists in your topslider folder, does the image load properly in the browser? If not, you'll need to update the way you build the src attribute in your img tag(s).
You're only adding one jcarousel-item for all your images, which doesn't seem right to me. I'm guessing you're expected to add a list item for each image.
You don't need to call glob twice just to ascertain how many files you have to work with. Just do a foreach statement once:
echo '<ul class="jcarousel-list">';
foreach(glob($directory . '*') as $filename) {
echo '<li class="jcarousel-item"><img src="'.$filename.'" width="50" height="50" alt="" /></li>';
}
echo '</ul>';

Related

Use an image file link to display a random banner

Not sure if this is even appropriate here, but I'm trying to work out how a banner rotator website is using standard html image code, to return a random banner?
<a target=_blank href=http://intellibanners.com/click.php?cid=campaign1>
<img border=0 src=http://intellibanners.com/campaign1.jpg>
</a>
In the example above, that html will display a random image from that campaign.
This started as a bit of a whim to see if I could set up something similar to work with different image sizes, but now it's driving me nuts... trying to figure out how to go from a image url to a database call to send back a different image!?
No lucky searching for ideas or examples, probably because I don't know what sort of processes or functions I should actually be looking for?
I'm thinking there must be some sort of url rewrite for all image calls, that redirects to a handler script...
That script makes the database call, grabs a random image from the nominated campaign, updates stats etc...
And possibly a php header response returns the corresponding image file?
But most of that is new to me and I'm not sure where/how to search for guidance or examples to get me started.
If anyone has any ideas on this, or even the kind of phrasing/functions I need that might help me get there in my own searching, it would be much appreciated!
(I've also installed and tested about 12 different rotator scripts hoping to find one that works the same, but no luck there either).
Thanks!
Matt
I wrote a php script that assumes to exist the wanted folders in the root of your website. Then you can invoke it in a simple way in any location of your website. Here is my script:
rotate.php
<?php
##########################################################
# Simple Script Random Images Rotator • 1.4 • 04.01.2020 #
# Alessandro Marinuzzi [alecos] • https://www.alecos.it/ #
##########################################################
function rotate($folder) {
if ((file_exists($_SERVER['DOCUMENT_ROOT'] . "/$folder")) && (is_dir($_SERVER['DOCUMENT_ROOT'] . "/$folder"))) {
$list = scandir($_SERVER['DOCUMENT_ROOT'] . "/$folder");
$fileList = array();
$img = '';
foreach ($list as $file) {
if ((file_exists($_SERVER['DOCUMENT_ROOT'] . "/$folder/$file")) && (is_file($_SERVER['DOCUMENT_ROOT'] . "/$folder/$file"))) {
$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
if ($ext == 'gif' || $ext == 'jpeg' || $ext == 'jpg' || $ext == 'png') {
$fileList[] = $file;
}
}
}
if (count($fileList) > 0) {
$imageNumber = time() % count($fileList);
$img = $folder . '/' . $fileList[$imageNumber];
}
return $img;
} else {
mkdir($_SERVER['DOCUMENT_ROOT'] . "/$folder", 0755, true);
}
}
?>
Now you can create a index.php file where you want and put this line:
<?php include("your_path/rotate.php"); ?>
<a target="_blank" href="http://intellibanners.com/click.php?cid=campaign1">
<img border="0" src="/<?php echo rotate('campaign1'); ?>">
</a>
other like this:
<a target="_blank" href="http://intellibanners.com/click.php?cid=campaign2">
<img border="0" src="/<?php echo rotate('campaign2'); ?>">
</a>
and so that...
You have only to create the wished folders in the root called campaign1, campaign2...etc...
then in each folder put the banner images of the current campaign...
for example: in the folder campaign1 put image like 001.png, 002,png, 003.png, 004.jpg, 005.jpg... (where these filenames match your campaign banners).
for example: in the folder campaign2 put image like 001.png, 002,png, 003.png, 004.jpg, 005.jpg, 006.gif, 007.png, 008.jpg... (where these filenames match your campaign banners).
Please note that if you don't want rename your banner images you can take the original names, my script will work in any way.
My script will turn all in wished output like this:
campaign1:
<a target="_blank" href="http://intellibanners.com/click.php?cid=campaign1">
<img border="0" src="/campaign1/002.jpg">
</a>
<a target="_blank" href="http://intellibanners.com/click.php?cid=campaign1">
<img border="0" src="/campaign1/007.jpg">
</a>
campaign2:
<a target="_blank" href="http://intellibanners.com/click.php?cid=campaign2">
<img border="0" src="/campaign2/004.png">
</a>
<a target="_blank" href="http://intellibanners.com/click.php?cid=campaign2">
<img border="0" src="/campaign2/005.gif">
</a>
See you and test all into your localhost website... for me works fine on PHP 7.4.5 and Apache 2.4.43.
The script will do the rest!
Hope this helps you!

Lightbox display multiple images from directory

I can not find an answer(that works) to this anywhere on the web.
I am trying to get Lightbox to load images from a directory as they will be frequently updated.
If anyone can correct what I'm doing wrong, or has a solution using either PHP or a hidden div populated automatically by a specific directory it would be greatly appreciated.
Here is what I have come up with but is not seeming to work;
<?php $dirname = "img/love/"; $images = glob($dirname."*.jpg"); foreach($images as $image) { echo '<img data-lightbox="love" src="'.$image.'" /><br />'; } ?>
and here is my test page: http://knowledgeoverfame.com/tslp/leet/alt/index.html
I didn't find any similar questions here with an answer to this but if i may have missed it please enlighten me :)
Thanks!
Try using scandir() to get an array of your image files.
Example:
<?php
$images = scandir($your_folder);
foreach ( $images as $key => $filename ) {
if ( $key > 1 ) { //ignores the first two values which refer to the current and parent directory
echo '<img data-lightbox="love" src="'.$your_folder."/".$filename.'" /><br />';
}
}
?>
for reference: PHP scandir()
What you want to do - use lightbox to display images from a folder - is a problem which has been solved lots of times with varying degrees of complexity. If you do a google search for something like "php image gallery" you will find sample scripts. I did just this about a year ago and after some experimentation I chose Minigal Nano as it was a powerful script but simple enough for it to be quite easy to see how it worked and then rework for my own use. I also found this an effective way to learn php.
This script will pull the images from a directory. I used it with fancybox but you can modify to fit your needs.
<?php
$directory = 'yourDirectory/gallery/thumbs'; //where the gallery thumbnail images are located
$files = glob($directory."/*.{jpg,jpeg,gif,png}", GLOB_BRACE);
natsort($files); //sort by filename
?>
<?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="yourDirectory/gallery/<?= $file ?>"></a>
</div>
<?php
}//end for loop
?>

Pulling random non-recurring image from folder and assigning it to the html tag

I'm working on a website with an image grid with infinite scrolling. For an image grid I'm using Final Tiles Grid Gallery plugin, which has an infinite scrolling feature and works as follows:
It requires PHP file with html structure of elements that should be added when users reaches certain part of the web page
Part of get-images.php:
<div class="tile">
<a class="tile-inner" href="photos/1.jpg">
<img class="item" src="images/3235535.jpg" />
</a>
Now, when certain part of page is reached the plugin calls ajax function to add more pictures. What I'm trying to achieve is automating the process of adding the code to the PHP file. I have too many images to add them all manually. I have very little knowledge in PHP and I found this little piece of code on stackoverflow to help me out:
<?php
$dir = "images/";
$images = scandir($dir);
$i = rand(2, sizeof($images)-1);
?>
<img src="images/<?php echo $images[$i]; ?>" alt="" />
Now, how do I combine those to achieve my goal? Also, I am planning to append the link to every picture added and the link should correspond with the name of the image (For example the name of image is "12345", then the link should be "abc.com/12345." I know I am asking too much and the second part of my question is completely on me to figure out but I will appreciate any help. Thanks.
Try it:
<?php
$count = 20;
function fetch_rand()
{
$images = glob('images/*.*');
$rand = array_rand($images);
return $images[$rand];
}
$my_list = array();
$i = 0;
while($i<$count)
{
$select = fetch_rand();
if(!in_array($select,$my_list))
{
array_push($my_list,$select);
$i++;
}
}
//print_r($my_list); //just for check
foreach($my_list as $image)
{
$image_link = explode('.',end(explode('/',$image)));
$image_link = $image_link[0];
//echo $image_link; //just for check
?>
<div class="tile">
<a class="tile-inner" href="abc.com/<?php echo $image_link; ?>">
<img class="item" src="<?php echo $image; ?>" />
</a>
</div>
<?php
}
?>
it will select random file from your image directory and never show duplicated.
Note: change $count value to your prefer value and image directory address in fetch_rand function.

I am trying to get all photos in current working directory.

I am trying to make a simple gallery script on my page for a photo booth. I have a tethered camera which is dropping pictures into a folder on my local site hosted with MAMP. Each folder is a different groups photos. Path to photos is ('/images/**'). First page searches the folders in my images directory and returns only the first image as a thumbnail/link to all folders containing all images for that group of people.
My structure is as follows.
1st page is:
$i = 1;
foreach (glob('images/*') as $dir) {
echo '<div id="strip' . $i . '" class="polaroid">';
// One div per directory
$p = 1;
foreach (glob($dir."/*.jpg") as $img) {
$p++;
if ($p <= 2) {
echo "<a href='$dir'><img src='$img'/></a>";
} else {
}}
echo "</div>\n";
$i++;
This part works and takes the user to the directory with jpegs in it. which also contains a index.php file with the following code:
$dir = 'images/'.basename('/'.getcwd()).'/';
foreach (glob($dir."/*.jpg") as $img)
echo $img;
{
echo "<img src='$img'/>";
}
This is where it fails. When I echo out $dir it shows the current relative path, but $dir in the foreach statement seems to be returning empty. This just returns an empty img tag. Any opinions where I am going wrong.
Thanks in advance
If you want to list the files in the current directory you have to set
$dir = '.';
You did set $dir to /images/225652/ but you are already in this directory

Using glob on a directory above current?

Currently I have a script running to grab all the images in a directory based on ending with "154x154.jpg". Basically what I'm doing is going through a gallery directory and looking for all the thumbnails of images, which I will then output in a sliding gallery on the main page of a website.
<?php
// loop through the images
$count = 0;
$images = array();
foreach (glob("../../uploads/2012/05/*154x154.jpg") as $filename) {
$images[$count] = $filename;
$count++;
}
for ($i = 0; $i < 7; $i++) {
$random = mt_rand(1, $count - 1);
echo '<li><a class="gal_img" href="#">';
echo '<span class="roll"></span>';
echo '<img class="image" src="'.$images[$random].'" height="154" width="154" alt="" />';
echo '</a></li>';
}
?>
This works perfectly fine as it is, but what I need to do is be able to grab the files from within a directory above the current one. To be more specific:
image folder: http://root/wp-content/uploads/2012/05/
theme folder: http://root/wp-content/themes/theme_folder/
** EDIT **
Everything works now with the above code it executes fine, when I go directly to the file. However using it within the theme generates everything except for the filepaths in the code. Using wordpress 3.3.2
Try:
foreach (glob("../images/*154x154.jpg") as $filename) {
Update:
Of course, this is a relative path and only works if the file you need to include is one level up from your script's current working directory. Solution: use an absolute path, ie. one that begins with /. Also, keep in mind that the absolute path is a path on the disk, not the root folder of your website.

Categories