How to fetch images from folder? - php

I have a folder named as images. I just want to fetch those images from the folder and show them randomly on web page. When the user refreshes the page, the image should change.
Thank you.

An idea could be:
you fetch all the images names frome the images folder
you randomly pick one
you visualize that one
so you can go like this
<?
$dir = '/my_directory_location';
$files = scandir($dir);
$rand_img = array_rand($files, 2);
$imcolumn = "<img src=$rand_img alt=$rand_img><br>";
?>
<?= $imcolumn ?>

<?php
$folder = "templates/images/";
$search = glob($folder."/*");
shuffle($search);
foreach ($search as $image) {
print "<img src='".$image."'><Br>";
}
?>

Related

System hit counter SaaS - Via TXT - Imagecopymerge

I am doing an hit counter (view) system, and now I need to find a way to merge all number images into one to be possible to embed in another pages, could someone help me out?
Here is the code I have:
$number = trim(file_get_contents('visitas.txt'));
$file = 'visitas.txt';
$views = file_get_contents ($file);
$fdata = intval($views)+1;
file_put_contents($file, $fdata);
$array = str_split($number);
if(!empty($array)){
foreach($array as $single){
echo '<img style="height:20px" src="'.$single.'.png">';
}
}else{
echo '<img src="0.png">';
}
$image = imagecreatefromstring(file_get_contents('sample.jpg'));
At the moment everything is working, each refresh is counting and sum the number writed by image, but I need to embed this counter in another pages, something like:
<img src='https://countersite.com.br/img-counter-385503.jpg'>
Here is a image of the counter working:
UPDATED
I need help to merge the image already created with the numbers (0.png, 1.png...) into one image (counter1.png).

Display images from ftp server with php

How can i display all images from an ftp server in website using php?
It should also sort automatically so i could just add more images to the ftp server and it would work.
I'm trying to make a gallery type thing
I've already looked through google and found nothing that worked.
I'm still learning php and dont know too much about it.
Any help would be much appreciated. Thanks!
As already commented, scandir does the trick. The below code gets all the images and output them as <img> elements.
index.php:
// Here enter the base url of the images folder
$base_images_url = "http://example.com/images/";
$dir = dirname (__FILE__) . "/images/";
// Get the files in the folder. Sort in ascending order - this is the default
$images = scandir($dir);
// Or is you need to sort in descending order
//$images = scandir($dir,1);
// Output an img tag for every image.
foreach($images as $image){
// skip the . and .. that starts the folder listing
if($image === '.' || $image === '..'){
continue;
}
$image_url = $base_images_url . $image;
echo '<img src="' . $image_url . '">';
}
This works with the following folder setup:
-index.php
-images
-image1.png
-anotherimage.jpg

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

Pull dedicated images from folder - show image + filename (strip part of filename + file-extension)

based on the original code in this question and Tims correction over there (answer 1) I'm trying to achieve the following:
I have a folder "img" containing images as follows:
image_123.jpg
image_123_alternate.jpg
image_456.jpg
image_456_alternate.jpg
image_789.jpg
image_789_alternate.jpg
and so on...
(Note: all images have the same size of 200px/200px)
When pulling from the "img" folder the simple HTML page should display images and filenames in the following fashion:
The actual images that contain the "_alternate" part in their filename (and only these) + the image filename not containing the "_alternate" part (without the file-extension) underneath the image in a textbox. All pairs should be pulled in an alphabetical order. In the example below bold capital letters indicate the actual image to be displayed:
IMAGE_123_ALTERNATE
textbox: "image_123"
IMAGE_456_ALTERNATE
textbox: "image_456"
IMAGE_789_ALTERNATE
textbox: "image_789"
This is what I have so far but this displays all images and filenames:
<?php
$dir = opendir("img");
while (($file = readdir($dir)) !== false)
{
echo "<a href='img/".$file."' target='_blank'> <img src='img/".$file."'width='200' height='200' /></a><br />";
echo "<textarea>$file</textarea><br /><br />";
}
closedir($dir);
?>
Any help would be much appreciated!
Thanks a lot!
This code should display a list of all images in alphabetical order (using PHP's GLOB function).
It then outputs a HTML image (leading to the "_alternate" image) and the image's filename (without the path, "_alternate" or "extension").
<?php
foreach (glob("img/*_alternate.jpg") as $file) {
$filename_parts = pathinfo($file);
$filename = $filename_parts['filename'];
$filename = str_replace("_alternate", "", $filename);
echo "<div>";
echo "<img src=\"/$file\" width=\"200\" height=\"200\" />";
echo "<p>$filename</p>";
}
?>
I have broken apart the functions that find the new filename and the parts that output the HTML code to make it easier to read and understand, you could merge these into one function if you wanted to.
Just through an if statement in your while loop. Check if the file name contains alternate or or not. If it doesn't contain alternate run your same code replacing the file extension with "_alternate.ext" while keeping the text area the same. If it does contain alternalte, do nothing. That way you are only processing half the files.
$files = new \DirectoryIterator('img'); // <- this should be the full absolute path!
$images = array();
// pick up jpg images that end in 'alternate'
foreach($files as $file){
$name = $file->getBasename('.jpg');
if(strripos($name, 'alternate') === (strlen($name) - 9))
$images[] = $name;
}
// sort them
sort($images, SORT_STRING);
// and do your thing here with $images...
var_dump($images);

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

Categories