I have the following code to search the folder /images/ for images and echo them. However, it displays the images from a random order everytime I refresh the page. The images are named 1, 2, 3, 4 and so on. Any way to make it so that the last number (ex: 4) is the first one being displayed and so on?
<?php
$dirname = "images";
$images = scandir($dirname);
shuffle($images);
$ignore = Array(".", "..");
foreach($images as $curimg){
if(!in_array($curimg, $ignore)) {
echo "<img src=\"". $dirname . '/' . $curimg ."\">" ;
}
}
?>
Thanks in advance.
This is due to your shuffle. You are randomizing your array. Let me introduce you to: http://php.net/manual/en/function.array-reverse.php which is
<?php
$dirname = "images";
$images = scandir($dirname);
$images = arsort(array_reverse($images, true));
$ignore = Array(".", "..");
foreach($images as $curimg){
if(!in_array($curimg, $ignore)) {
echo "<img src=\"". $dirname . '/' . $curimg ."\">" ;
}
}
?>
Update:
$dirname = "Images";
$images = scandir($dirname);
sort($images,SORT_NUMERIC);
krsort($images);
$ignore = Array(".", "..");
foreach($images as $curimg){
if(!in_array($curimg, $ignore)) {
echo "<img src=\"". $dirname . '/' . $curimg ."\"> \n" ;
}
}
What I have been working with:
Without the sort(); and krsort(); i return:
<img src="Images/1.png">
<img src="Images/10.png">
<img src="Images/11.png">
<img src="Images/2.png">
<img src="Images/3.png">
<img src="Images/4.png">
<img src="Images/5.png">
<img src="Images/6.png">
<img src="Images/7.png">
<img src="Images/8.png">
<img src="Images/9.png">
With the krsort and sort.. I return:
<img src="Images/11.png">
<img src="Images/10.png">
<img src="Images/9.png">
<img src="Images/8.png">
<img src="Images/7.png">
<img src="Images/6.png">
<img src="Images/5.png">
<img src="Images/4.png">
<img src="Images/3.png">
<img src="Images/2.png">
<img src="Images/1.png">
Which I presume is what you are looking for.
http://www.php.net/manual/en/function.array-reverse.php
should be the right function instead of shuffle
UPDATE:
Better would be to sort it directly via scandir:
$images = scandir($dirname, SCANDIR_SORT_DESCENDING);
You could try to load image names into array, than sort array and then echo image tags
<?php
$dirname = "images";
$files = scandir($dirname, 1); // using SCANDIR_SORT_DESCENDING PHP 5.4+ ONLY!
foreach ($files as $file) {
if ($file !== '.' && $file !== '..') {
echo "<img src=\"". $dirname . '/' . $file ."\">" ;
}
}
?>
Related
I'm develop my websites and want to display all the images in a folder. I used the following code but occurred an error.
<?php
$sesi = Session::get('id');
$gambar = glob('archive_gambar/formulir/pendaftaran/'.$sesi.'/*');
for ($i=0; $i<count($gambar); $i++)
{
$single_image = $gambar[$i];
?>
<img src="<?php echo $single_image; ?>" class="img-responsive" style="margin-left: auto;margin-right: auto; margin-top: auto;margin-bottom: auto;width:75%;" alt="Tri">
<?php
}
?>
there is no error, the number of images is the same, but the picture does not appear.. like this
picture not appear
$dirname = "media/images/iconized/";
$images = glob($dirname."*.png");
foreach($images as $image) {
echo '<img src="'.$image.'" /><br />';
}
If the archive_gambar directory is at the root of the web site, try adding slash in front of the path
$gambar = glob('/archive_gambar/formulir/pendaftaran/'.$sesi.'/*');
if not, you must write the full path
Try with this, adjust the $dir variable to your needs:
/**
* This file: images.php
* This script read content of "images" directory as same level, and return filelist in array format
**/
$dir = "images/";
$images = array();
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if($dir){
if(in_array($file, array('.', '..')) == FALSE){
$images[] = $file;
}
}
}
closedir($dh);
}
}
/**
* Print array to browswer
*/
echo '<pre>';
print_r($images);
/**
* Iterate $images array in foreach loop and display images with img tag (need to adjust directory to your needs).
*/
foreach ($images as $key => $image){
echo '<img src="images/'.$image.'" class="img-responsive" alt="image_'.$key.'">';
}
I try to dynamically populate an HTML page with 2 images, the first a full image and the second a reduced image, but as I can not do AND with a foreach.
In fact, the code is ok, but I want populate my HTML with twos pics of the same folder; but this code bring back all pictures, it doesn't no the diff between the Full image and the Thumbnail.
The script bring back all picture. My folder contain images with specific titles:
Full Image = *.jpeg
Thumbnail = *_Low.jpeg
I would like to be able to modify my code to insert the full in the first line and the thumbnail in the second line.
<?php
$dir = './style/images/art/mairie/';
$files = scandir($dir);
$images = array();
array().sort();
$nb = 1;
foreach($files as $file) {
if(fnmatch('*.jpg',$file)) {
$images[] = $file;
}
}
var_dump($images);
foreach ($images as $image) {
echo '<div class="cbp-item">'.'<a class="cbp-caption fancybox-media" data-rel="portfolio" href="style/images/art/mairie/'.$image.'">'."\n"
.'<div class="cbp-caption-defaultWrap">'.'<img src="style/images/art/mairie/'.$image.'" alt="" /> </div>'."\n"
.'<div class="cbp-caption-activeWrap">'."\n"
.'<div class="cbp-l-caption-alignCenter">'."\n"
.'<div class="cbp-l-caption-body">'."\n"
.'<div class="cbp-l-caption-title"><span class="cbp-plus">'.'</span></div>'."\n"
.'</div>'."\n"
.'</div>'."\n"
.'</div>'."\n"
.'<!--/.cbp-caption-activeWrap --> '."\n"
.'</a> </div>'."\n";
}
?>
for the second line I would like to bring back the reduced photo, so to have
<div class="cbp-caption-defaultWrap"><img
src="style/images/art/mairie/Maurine_Tric-7399_Low.jpg" alt="" />
</div>
$images
Part of my $images array would look like this:
$images = [
"0" => "Maurine_Tric-7399.jpg",
"1" => "Maurine_Tric-7399_Low.jpg",
"2" => "Maurine_Tric-7407.jpg",
"3" => "Maurine_Tric-7407_Low.jpg",
"4" => "Maurine_Tric-7414.jpg",
"5" => "Maurine_Tric-7414_Low.jpg",
];
Desired Output
I'm trying to add one of the URLs in my array with large images, and the other with its thumbnail which are being differentiated with _Low:
<div class="cbp-item"><a class="cbp-caption fancybox-media" data-rel="portfolio" href="style/images/art/mairie/Maurine_Tric-7399.jpg"> <div class="cbp-caption-defaultWrap"><img src="style/images/art/mairie/Maurine_Tric-7399_Low.jpg" alt="" /> </div>
Just use preg_match() instead of fnmatch(), matching only the thumbail images and marking the part before _Low.jpg as Subpattern.
Then you can easily construct the filename of both images from that stub:
<?php
$dir = './style/images/art/mairie/';
$files = scandir($dir);
$images = array();
array().sort();
$matches = NULL;
foreach ($files as $file) {
if (preg_match('/^(.+)_Low\.jpg$/', $file, $matches)) {
$images[] = $matches[1];
}
}
foreach ($images as $image) {
echo '<div class="cbp-item"><a class="cbp-caption fancybox-media" data-rel="portfolio" href="' . $dir . $image . '.jpg"> <div class="cbp-caption-defaultWrap"><img src="' . $dir . $image . '_Low.jpg" alt="" /></div>';
}
?>
If we wish to just add one URL for our images and one for their thumbs in the foreach, we might be able to define a $html variable and step by step add to it, then we would finally echo that, and our code would look like something similar to:
<?php
$html = '<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Document sans titre</title>
</head>
<body>';
$html .= '<ul>';
$dir = './style/images/art/mairie/';
$files = scandir($dir);
$images = array();
$nb = 1;
foreach ($files as $file) {
if (fnmatch('*.jpg', $file)) {
$images[] = $file;
}
}
foreach ($images as $key => $image) {
if ($key % 2 == 0) {
$html .= '<div class="cbp-item">
<a class="cbp-caption fancybox-media" data-rel="portfolio" href="style/images/art/mairie/' . $image . '">
<div class="cbp-caption-defaultWrap"><img src="style/images/art/mairie/' . $images[$key + 1] . '" alt="" /> </div>
<div class="cbp-caption-activeWrap">
<div class="cbp-l-caption-alignCenter">
<div class="cbp-l-caption-body">
<div class="cbp-l-caption-title"><span class="cbp-plus"></span></div>
</div>
</div>
</div>
</a>
</div>';
}
}
$html .= '</ul></body></html>';
echo $html;
Modifications
What we are modifying here is that we add an if to run every other time.
if ($key % 2 == 0) {
}
We add $key var in our foreach and finally we have two image links which we replace one of them with $images[$key+1], which one of them is for thumbs:
$image
$images[$key+1]
this script show 50 random images
that change turning on themselves.
but sometimes show the same images
how don't show the same images?
my code
<?php
$all_images = glob("wp-content/themes/mysite/img-company/{*.jpg, *.JPG, *.JPEG, *.png, *.PNG}", GLOB_BRACE);
$images = glob("wp-content/themes/mysite/img-company/{*.jpg, *.JPG, *.JPEG, *.png, *.PNG}", GLOB_BRACE);
shuffle($all_images);
foreach ($all_images as $index => $image ) {
if ($index == 50) break; // Only print 50 images
$image_name = basename($image);
$randomImage = $images[array_rand($images)];
echo "<li><img src='/wp-content/themes/mysite/img-company/{$image_name}' /><img src='/$randomImage' /></li>";
}
?>
The obvious way would be to delete a presented image from the array:
$randomImage = $images[array_rand($images)];
$images = array_diff($images, array($randomImage));
Another solution: Simply use unset for removing element from array like this:
$random = array_rand($images);
$randomImage = $images[$random];
unset($images[$random]);
I have the next code:
<?php
$path = 'imgsFor';
$files_array = scandir($path);
for ($x=0; $x<=4; $x++)
{
echo '<img src="imgsFor/$files_array[$x]" <br>';
}
?>
In order to display all images in the folder imgsFor.
For some reason, I see the just boxes and not the actual images.
What can be the reason?
The best way for me is to use glob function:
foreach (glob($path) as $filename) {
echo '<img src="' . $path . '/' . $filename . '"/><br/>';
}
You messed up some things. Your correct script would be
<?php
$path = 'imgsFor/';
$files_array = scandir($path);
foreach($files_array as $f) {
if(is_dir($path . $f) === false)
continue;
echo '<img src="' , $path , $f , '"><br>';
}
/* EOF */
The reason is that your URL is invalid. Your variable wont echo out if you use single quotes. You also forgot to end the tag. Try this:
echo "<img src='http://yourwebsite.com/imgsFor/{$files_array[$x]}'/><br/>";
Please check you directory path and use is_dir which returns false when the file doesn't exist. you can try like this
$path = 'imgsFor';
$scan = scandir($path);
foreach($scan as $file)
{
if (!is_dir($path))
{
echo $file.'\n';
}
}
I am currently using SimpleXML to reading an xml file to generate my first array. This array displays a list of images that are found in a directory and written to xml file.
This has worked for me in the past until I upload new photos to the same album. I like to know what one does to get a complete list of images that got uploaded to the end of that first array. Any suggestions?
XML File:
<album>
<image path="/albums/1/images/100_4124.jpg" />
<image path="/albums/1/images/100_4307.jpg" />
<image path="/albums/1/images/100_4335.jpg" />
</album>
SimpleXML to get array:
$xml = simplexml_load_file('/albums/1/photos.xml');
foreach ($xml->image as $image) {
echo '<li><img src="'.$image.'"></li>';
}
What I am getting for my results:
<li><img src="/albums/1/images/100_4124.jpg"></li>
<li><img src="/albums/1/images/100_4307.jpg"></li>
<li><img src="/albums/1/images/100_4335.jpg"></li>
Scanning the directory for all images:
if ($handle = #opendir('/albums/1/')) {
$filenames = array();
while (false !== ($file = readdir($handle))) {
$ext = substr($file, strrpos($file, '.') + 1);
if ($file != '.' && $file != '..') {
$filenames[] = $file;
$total++;
}
}
}
closedir($handle);
foreach ($filenames as $filename) {
echo '<li><img src="'.$filename.'"></li>';
}
What I would like to get for results using both arrays:
<li><img src="/albums/1/images/100_4124.jpg"></li>
<li><img src="/albums/1/images/100_4307.jpg"></li>
<li><img src="/albums/1/images/100_4335.jpg"></li>
<li><img src="/albums/1/images/100_9000.jpg"></li>
<li><img src="/albums/1/images/100_9001.jpg"></li>
<li><img src="/albums/1/images/100_9002.jpg"></li>
The last 3 images that are missing from xml file would be added it to the end of the list.
Perhaps this would work for you?
<?php
$images = array();
foreach ($xml->image as $image) {
$images[] = $image;
}
foreach (array_diff($filenames, $images) as $image) {
$images[] = $image;
}
foreach ($images as $image) {
echo '<li><img src="' . $image . '" /></li>';
}
?>
This is assuming that $image from the $xml->image and $filename from your dir search are formatted the same. From what you've shown they are. If they aren't should be easy enough to run them through a quick regex to get them in a state where they are comparable.
Any who it looks like array_diff() is what you're looking for.
If you get a full list of the file names in the directory you could use PHP in_array and search for it in an array of your paths
$paths = array();
foreach ($xml->image as $image) {
$paths[] = $image['path'];
}
foreach ($filenames as $filename) {
if (!in_array($filename, $paths) {
$newImage = $xml->addChild('image');
$newImage->addAttribute('path', $filename);
}
}
// save adjusted xml file, assuming you have permission
$xml->asXML('/albums/1/photos.xml');
foreach ($xml->image as $image) {
echo '<li><img src="'.$image.'"></li>';
}