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]);
Related
I'm trying to get all images from folder with php and link them with another folder where I have pdf files. The names of both are the same.
My code looks like this:
$dir_jpg = "../wp-content/uploads/newspaper/jpg/";
$dir_pdf = "../wp-content/uploads/newspaper/pdf/";
$images = glob($dir_jpg."*.*");
$pdfs = glob($dir_pdf."*.*");
$array = array_combine($images, $pdfs);
foreach($array as $image => $pdf){
echo '<ul>';
echo '<li>';
echo '<img src="'.$image.'" height="100" width="100" />';
echo '</li>';
echo '</ul>';
}
I did var_dump to check the array, but it was empty. What could be the reason my code is not working?
Update
I store my files in a wordpress folder system, but I didn't upload them through
wordpress media, so there is no records about them in database. I want to avoid it, and upload those files through ftp account and list them with a php code.
Update 2 On chat with #Gabor Klement we have got to this
$wp_root = wp_upload_dir()['baseurl'];
$dir_jpg = $wp_root."/newspaper/jpg/";
$dir_pdf = $wp_root."/newspaper/pdf/";
$images = glob($dir_jpg."*.*");
$pdfs = array();
$imgNum = count($images);
$list = '<ul>';
for ($i = 0; $i < $imgNum; $i++) {
$filename = substr(basename($images[$i]), 0, -4);
$pdfs[$i] = $dir_pdf.$filename.".pdf";
if (file_exists($dir_pdf.$filename.".pdf")) {
$list .= '<li><img src="'.$images[$i].'" height="100" width="100" /></li>';
}
}
$list .= '</ul>';
echo $list;
but for some reason wordpress is not showing those files. Funy part is that <img src="<?php echo $dir_jpg ?>/july2012.jpg" /> placed before the $list works
Update 3
the only thing that is passing paths to $images is wp_upload_dir()['basedir']; but then the wordpress creates a path like this domain.com/home/user/domains/domain/public_html/wp-content/uploads/newspaper/jpg/december2012.jpg and the image is not found.
I found a solution for my problem. I had to use wp_upload_dir()['basedir'] to pass the path to glob, and then wp_upload_dir()['baseurl']to pass the link to src.
Maybe it not the best solution, but it works.
$base_dir = trailingslashit(wp_upload_dir()['basedir']);
$base_url = wp_upload_dir()['baseurl'];
$dir_jpg = '/newspaper/jpg/';
$dir_pdf = '/newspaper/pdf/';
$images = glob($base_dir.$dir_jpg.'*.*');
foreach($images as $image) {
$url = $base_url.$dir_jpg.basename($image);
$filename = substr(basename($image), 0, -4);
$pdfs = $base_url.$dir_pdf.$filename.".pdf";
printf('<img src="%s" alt="'.$filename.'.pdf"><div class="newspaper-hover"></div>', esc_url($url));
}
I have this code that shows uploaded images:
<?php
$dir = "img/*.jpg";
$images = glob( $dir );
foreach( $images as $image ):
echo "<img src='" . $image . "' />";
endforeach;
?>
but I need my last uploaded image to appear first in line. I know it's right there in front of me, but I can't see it. How do I do that?
Try reversing the array before your foreach.
<?php
$dir = "img/*.jpg";
$images = glob( $dir );
$images = array_reverse($images);
foreach( $images as $image ):
echo "<img src='" . $image . "' />";
endforeach;
?>
A safer bet would be to use the filemtime() function to get the modification time of each file and sorting according to that. See this code:
<?php
$dir = "img/*.jpg";
$images = glob( $dir );
//Add this portion
foreach( $images as $image ) {
$imagesModTimeArray[filemtime($image)] = $image;
}
krsort($imagesModTimeArray);
$images=array_values(imagesModTimeArray);
//New Portion End
foreach( $images as $image ):
echo "<img src='" . $image . "' />";
endforeach;
?>
im trying to use a foreach loop with the plugin fotorama.
What im trying to do is load one half sized image for the main gallery image. Which i have working in a foreach, but i want to use a full image for the data-full tag but i cant get it to work.
This is the working code.
<div class="fotorama"
data-allowfullscreen="native"
data-nav="thumbs"
data-fit="scaledown"
data-width="100%"
data-height="100%"
data-arrows="true"
data-click="true"
data-swipe="true">
<?php
$dirname = "admin/image-upload/uploads/";
$images = glob($dirname."*.*");
foreach($images as $image) {
echo '<img src="'.$image.'" /><br />';
}
?>
</div>
this is what im trying to do.
<div class="fotorama"
data-allowfullscreen="native"
data-nav="thumbs"
data-fit="scaledown"
data-width="100%"
data-height="100%"
data-arrows="true"
data-click="true"
data-swipe="true">
<?php
$dirname = "admin/image-upload/uploads/";
$images = glob($dirname."*.*");
$dirname2 = "admin/image-upload/full/";
$images2 = glob($dirname2."*.*");
$fullImgs = "<img data-full=".$image2." src=".$image." /><br />";
foreach($fullImgs as $fullImg) {
echo $fullImg;
}
?>
</div>
thanks in advanced guys
Try this:
$dirname = "admin/image-upload/uploads/";
$images = glob($dirname."*.*");
$dirname2 = "admin/image-upload/full/";
$images2 = glob($dirname2."*.*");
$array = array_merge($images, $images2);
// Supossing both array have same length
$length = count($images);
for($i = 0; $j = $length; $i < $length; $i++, $j++) {
echo '<img data-full=".$images2[$j]." src=".$images[$i]." /><br />';
}
I think what you want is this:
<?php
$dirname = "admin/image-upload/uploads/";
$images = glob($dirname."*.*");
$dirname2 = "admin/image-upload/full/";
$images2 = glob($dirname2."*.*");
//assuming $images and $images2 are the same length...
foreach ($images as $k => $v) {
echo "<img data-full=".$images2[$k]." src=".$v." /><br />";
}
?>
Haven't tested, but should give the idea...
Your code won't work like this. If I understood you correctly you have the same image in big and small format in two different directories. To show them as pair you need to make a connection between those two files - because how should your script know which images belong together? You could use a database, but in your case I think it is easier to make a connection in the filename. For example, name the pictures in the directory for the small images
Image_7263.png
Image_0172.png
And so on. For the big images you simply append _BIG to the end of the name.
Then you use your foreach loop to loop through the directory for the small images. For each image in there, you append _BIG to the end of the filename and include it from the directory for the big ones.
$smallImages = glob ("/path/to/small/images/*.*");
foreach ($smallImages as $img)
{
$name = substr ($img, 0, strlen ($img)-4); //Remove the .png or .jpg
$bigImg = "/path/to/big/images/".name."_BIG.jpg"; // or whatever image type you are using
echo "<img data-full=\"".$bigImg."\" src=\"".$img."\" />
}
This question already has answers here:
PHP file listing multiple file extensions
(5 answers)
Closed 9 years ago.
I am very new to php and I would like to iterate through an images directory in the root folder and post these images to a webpage. I am using the following code to do so:
<?php
$dir = "images/*.jpg";
$images = glob( $dir );
foreach( $images as $image ):
echo "<img src='" . $image . "' />";
endforeach;
?>
My question is how can I modify this code to post more than .jpg images, such as .gif and jpg with the .jpeg extension without creating multiple directory variables?
Also, is there a more efficient way to post these images?
Thank you very much!
The non clever brute force way to do this would be to just run the loop for all different image types.
$dir = "images/*.jpg";
$images = glob( $dir );
foreach( $images as $image ):
echo "<img src='" . $image . "' />";
endforeach;
$dir = "images/*.png";
$images = glob( $dir );
foreach( $images as $image ):
echo "<img src='" . $image . "' />";
endforeach;
$dir = "images/*.gif";
$images = glob( $dir );
foreach( $images as $image ):
echo "<img src='" . $image . "' />";
endforeach;
etc...
An elegant way would be creating an array and then iterate it.
<?php
$imgs = array("images/*.jpg","images/*.gif","images/*.pnj");
foreach( $imgs as $type ){
$images = glob( $type );
foreach( $images as $image ):
echo "<img src='" . $image . "' />";
endforeach;
endforeach;
?>
As you are building an Array ($images is an array) you can push new files to this array.
$file_extensions = ['jpg', 'png'];
$images_total = [];
foreach ( $file_extensions as $extension ) {
$dir = "images/*." . $extension;
$images = glob( $dir );
foreach $images as $image {
$images_total[] = $image;
}
}
And now use your final code:
<?php
foreach( $images_total as $image ):
echo "<img src='" . $image . "' />";
endforeach;
?>
I didnt test it, but this is an idea :)
I reply appeared while I typed: As a difference, in my method you get to build an array containing every single file in the folder before start printing HTML. This may allow you to shuffle or maybe manipulate the array before outputting to the user :)
<?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;
?>