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!
Related
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.
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));
}
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."\" />
}
Okay, I am currently using this:
<?php
$dirname = "cards/collecting/";
$images = glob($dirname."*.png");
foreach ($images as $image) {
$title = pathinfo($image);
echo '<img class="deck" src="'.$image.'" alt="'. $title['filename'].'" title="'.$title['filename'].'">';
}
?>
This pulls a bunch of small images out of a folder and displays them. It works great, but I want there to be a after each 5th image, so it doesn't stretch across the entire div in which the image are being shown. I'm unsure what I need to add to do this.
Thanks in advance!
Using the index, you can write <br/> every time the index+1 is a multiple of 5: index 4, 9, 14, etc... Notice that I use $i=> in the foreach to get the value of $i.
<?php
$dirname = "cards/collecting/";
$images = glob($dirname."*.png");
foreach ($images as $i=>$image) {
$title = pathinfo($image);
echo '<img class="deck" src="'.$image.'" alt="'. $title['filename'].'" title="'.$title['filename'].'">';
if(($i+1)%5 == 0) echo '<br/>';
}
?>
The easiest way is use array_chunk.
array_chunk
array array_chunk ( array $array , int $size [, bool $preserve_keys = false ] )
Chunks an array into arrays with size elements. The last chunk may contain less than size elements
Code example:
$dirname = "cards/collecting/";
$images = glob($dirname."*.png");
$grp = array_chunk($images, 5, true);
foreach ($grp as $items) {
echo '<div class="item_grp">';
foreach ($items as $image) {
$title = pathinfo($image);
echo '<img class="deck" src="'.$image.'" alt="'. $title['filename'].'" title="'.$title['filename'].'">';
}
echo '</div>';
}
I want to show a variable (which is an image) a certain amount of times depending on the number from a different column.
So I want to have $image shown $numberofratings times (which is up to 5). I'm pretty new to SQL, so I'm probably missing something quite basic, but thankyou to anyone who helps!
<?
$query = mysql_query("SELECT * FROM alex_demo23");
while ($row = mysql_fetch_array($query)){
$rating=$row['rating'];
$numberofratings=$row['numberofratings'];
$image = '<img src="images/star.png">';
echo ("addMarker(Rated: $rating $image from $numberofratings reviews');\n");
}
?>
Just use a for() or a str_repeat:
$image = '';
for($i=0; $i<$numberofratings; $i++){
$image .= '<img src="images/star.png">';
}
Or
$image = str_repeat('<img src="images/star.png">', $numberofratings);
This should do it:
$image = "";
for ($i = 0; $i < $row['numberofratings']; $i++) {
$image .= '<img src="images/star.png">';
}
There's no error checking to make sure that the data is valid, but it should be a start.