I need your help because I do not know how to fix that little issue by myself.
How can I get the basename() for each image on $remaining but not that image from $recent?
<?php
foreach (glob('upload-gallery/*.{gif,png,jpg,jpeg,csv}', GLOB_BRACE) as $f) {
$list[filemtime($f) . '-' . $f] = $f;
}
$keys = array_keys($list);
rsort($keys);
$recent = basename($list[array_shift($keys)]); // Get newest image
$remaining = $keys; // Get the rest of images (basename)
?>
I got some help already:
<?php
foreach (glob('upload-gallery/*.{gif,png,jpg,jpeg,csv}', GLOB_BRACE) as $f) {
$list[filemtime($f) . '-' . $f] = basename($f);
}
krsort($list);
$recent = array_shift($list); // Recent image
$remaining = array_values($list); // Remaining images
?>
It works perfect now nevertheless thank you.
Related
I really need some help with this... i just cant make it work.
For now i have this piece of code and it's working fine.
What it does is... retuns all files within a directory according to date in their name.
<?php
header('Access-Control-Allow-Origin: *');
$imagesDir = '';
$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
$filteredImages = [];
foreach($images as $image) {
$current_date = date("Ymd");
$file_date = substr($image, 0, 8);
if (strcmp($current_date, $file_date)>=0)
$filteredImages[] = $image;
}
echo json_encode($filteredImages, JSON_UNESCAPED_UNICODE);
?>
But now i need to filter those files (probably before this code is even executed). acording to the string in their name.
files are named in the following manner:
yyyymmdd_xxxxxxx-xxxxxx~yyyymmdd.123456789.jpg
yyyymmdd_xxxxxxx-xxxxxx~yyyymmdd.9.jpg
yyyymmdd_xxxxxxx-xxxxxx~yyyymmdd.458.jpg
i need to filter out only ones that have certain number within that string of numbers at the end (between "." and ".jpg") eg. number 9
$number = 9
i was trying with this piece of code to seperate only that last part of name:
<?php
function getBetween($jpgname,$start,$end){
$r = explode($start, $jpgname);
if (isset($r[1])){
$r = explode($end, $r[1]);
return $r[0];
}
return '';
}
$jpgname = "yyyymmdd_xxxxxxx-xxxxxx~yyyymmdd.12789.jpg";
$start = ".";
$end = ".jpg";
$output = getBetween($jpgname,$start,$end);
echo $output;
?>
and i guess i would need STRIPOS within all of this... but im lost now... :(
You can probably use preg_grep.
It's regex for arrays.
This is untested but I think it should work.
header('Access-Control-Allow-Origin: *');
$imagesDir = '';
$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
$find = 9;
$filtered = preg_grep("/.*?\.\d*" . $find . "\d*\./", $images);
The regex will look for anything to a dot then any number or no number, the $find then any or no number again and a dot again.
Is this what you need ? It will give you 123456789
$string = "yyyymmdd_xxxxxxx-xxxxxx~yyyymmdd.123456789.jpg";
$explode = explode(".", $string);
echo ($explode[1]);
Edit -
As per your requirement Andreas's solution seems to be working.
This is what I tried , I changed the find variable and checked.
$images = array("yyyymmdd_xxxxxxx-xxxxxx~yyyymmdd.12789.jpg");
$find = 32;
$filtered = preg_grep("/.*?." . $find . "./", $images);
print_r($filtered);
I have a piece of code that pulls a set of images from a directory.
Problem is, it allows duplicates which I do not want.
Anybody an idea how to let every image be unique?
Can’t seem to figure it out right away with my limited knowledge. thanks!
<?php
$files = glob("images/*.*");
for($x = 0 ; $x < 4; $x++)
{
$fileNum=rand(1, count($files));
$image = $files[$fileNum];
echo '<img src="'.$image .'" id="lay"/>';
}
?>
It looks like you want four random files from the array to echo out. I suggest you use array_rand(4) to get four random keys from the list.
$files = glob("images/*.*");
$keys = array_rand(4);
foreach ($keys as $key) {
$image = $files[$key];
echo '<img src="'.$image .'" id="lay"/>';
}
Try this instead:
<?php
$files = glob("images/*.*");
foreach ( $files as $file )
{
echo '<img src="'. $file .'" id="lay"/>';
}
?>
Hope I could help you!
EDITED: Debugged some of the code but issue persists:
The issue i'm having with the following code is that the link always takes me to the last image in the set. I've tried reversing the array of photo's but it had no effect. Any help would be appreciated.
Thanks.
<?php
$dir = 'pic';
$max_albums=9;
$albums = array_diff(scandir($dir),array('..', '.', 'thumbs'));
foreach ($albums as $album) {
$albumdir = $dir.'/'.$album;
$coverdir = $albumdir.'/thumbs';
$thumbs = array_diff(scandir($coverdir),array('..', '.'));
//re-index $thumbs
$thumbs = array_values($thumbs);
//1 random cover image from each album
$rnd_min = 0;
$rnd_max = count($thumbs)-1;
$rnd_i = mt_rand($rnd_min, $rnd_max);
$covers = $thumbs[$rnd_i];
//re-index $covers
echo $rnd_i.'<br>';
//populate hrefs
$photos = array_diff(scandir($albumdir),array('..', '.', 'thumbs'));
//re-index $photos
$photos = array_values($photos);
foreach ($photos as $photo) {
echo '<a href="'.$albumdir.'/'.$photo.'" data-lightbox="'.$album.'">';
}
//display cover images
echo '<img src="'.$coverdir.'/'.$covers.'" class="img-responsive"></a>';
}
?>
hmmm try reversing scandir
$photos = array_diff(scandir($albumdir,1),array('..', '.', 'thumbs'));
Not a very elegant solution but it seems to work:
<?php
$dir = 'pic';
$max_albums=9;
$albums = array_diff(scandir($dir),array('..', '.', 'thumbs'));
foreach ($albums as $album) {
$albumdir = $dir.'/'.$album;
$coverdir = $albumdir.'/thumbs';
$thumbs = array_diff(scandir($coverdir),array('..', '.'));
//re-index $thumbs
$thumbs = array_values($thumbs);
//1 random cover image from each album
$rnd_min = 0;
$rnd_max = count($thumbs)-1;
$rnd_i = mt_rand($rnd_min, $rnd_max);
$covers = $thumbs[$rnd_i];
//populate hrefs
$photos = array_diff(scandir($albumdir),array('..', '.', 'thumbs'));
//re-index $photos
$photos = array_values($photos);
$countphoto = 0;
foreach ($photos as $photo) {
if ($countphoto==0) {
echo '<a href="'.$albumdir.'/'.$photo.'" data-lightbox="'.$album.'">'."\n";
//display cover images
echo '<img src="'.$coverdir.'/'.$covers.'" class="img-responsive"></a>';
}
else {
echo ''."\n";
}
$countphoto++;
}
}
?>
So i solved this issue with a simple if $countphotos==0 statement within the foreach ($photos as $photo) loop, if 0, it would display the thumbnail, else it would just output anchors to the other imgs.
I've also done away with the random thumbnail as cover, and just pulled the first thumbnail of the set.
I now another issue, but I will make a separate thread for that.
Thanks!
<?php
$dir = new DirectoryIterator('../images/main_body_image/');
foreach ($dir as $file)
{
$images [] = array (
$file->getPathname() . "\n";
$file->getFilename(). "\n";
);
}
?>
shuffle($images);
Can you help me with the code above? I want to add images to an array using DirectoryIterator, and then shuffle images to generate randomized images. Thank you for your valuable inputs.
This depends on what you are doing with the images, and what path your in at the time, but the following will put their full pathname into an Array.
<?php
$dir = new DirectoryIterator('../images/main_body_image/');
foreach($dir as $file){
$images[] = $file->getPathname();
}
shuffle($images); $imgs = '';
foreach($images as $v){
$imgs .= "<img scr='http://{$_SERVER['SERVER_NAME']}/$v' alt='randomImage' />";
}
//put the below wherever you want in your code
echo $imgs;
?>
I have the following code, which will retrieve a filename from a table and make a link to it. What I want to do, is have it so I can refer to $filesList later on, and it will contain a single block of html code with links to as many files as there are files.
I thought adding to the previous variable would be the easiest way to do this, but it actually outputs nonsense code: 0test.sh">test.sh
if ($getFiles = $con->prepare($filesQuery)) {
$getFiles->bind_param("s", $pk);
$getFiles->execute();
$getFiles->bind_result($FILENAME);
$files = array();
while ($getFiles->fetch()) {
$filename = array(
'FILENAME' => $FILENAME,
);
$files[] = $filename;
}
}
$filesList = '';
foreach ($files as $filenames)
{
$filesList = $filesList + '<p>'. $filenames['FILENAME'] .'' . "\n";
};
Sureley I do not need to have an array for what i want to do?
You need to change that code to:
$filesList = '';
foreach ($files as $filenames)
{
$filesList .= '<p>'. $filenames['FILENAME'] ."</p>\n";
};
Does that help? You cannot concatenate with +.
One thing that I immediately spot is that you have $filesList = $filesList + ... Use a dot and not a + -sign.
Try this
$filesList = $filesList . "<p>{$filenames['FILENAME']}";
Have you tried something like this?
(Untested code, as I am not at home)
if ($getFiles = $con->prepare($filesQuery)) {
$getFiles->bind_param("s", $pk);
$getFiles->execute();
$getFiles->bind_result($FILENAME);
$files = array();
while ($getFiles->fetch()) {
$filesList = $filesList + '<p>'. $FILENAME .'' . "\n";
}