Get all img and pdf with php - php

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));
}

Related

How to display all images if they exist in a folder PHP?

I have a PHP script that scans a specified Movie directory then displays it styled on a webpage using for loop and php. The code is below. I tried using glob but once I have all the images in an array how do I compare them to the array with all of the movies then if the image and the movie folder match to display the image with the correct name?
<?php
// set dir to the directiry you want to index
$dir = 'Movies/';
// scanning the directory you set
$scan = scandir($dir);
// I have this value at 11 because the first movie shows up
// in the 11th file in the array
$file = 11;
// This then removes the first useless files from our array
$scanned = array_slice($scan, $file);
// gets the amount of files
$FileNum = count($scanned);
// display all images and fanart
$images = glob('*.jpg');
// for loop that goes through the array
for ($i = 0; $i <= $FileNum; $i++) {
// gives the class for styling
echo '<li class="image">';
this is problem bit
// check if there is fanart/images in the folders
if (file_exists($dir . $scanned[$i] . $images)) {
// if there is images display them styled
echo '<img id="box1" src="' . $dir . $scanned[$i] . '*.jpg' . '" width="280" height="150" />';
} else {
// if not then display default image
echo '<img id="box1" src="http://placehold.it/280x150" width="280" height="150" />';
}
// make the box clickable to where the folder is located
echo '<a href="'. $dir . $scanned[$i] .'">';
// display the name of the movie and some JS
echo '<span class="text-content"><span>' . $scanned[$i] .'<br><br><i class="fa fa-4x fa-play-circle-o"></i><br><br><i class="fa fa-chevron-down" onclick="openNav()" aria-hidden="true"></i></span></span> </a>';
}
The file structure is as follows
`MOVIES---
\--random movies
\--mp4 and .jpg files`
To clarify my question is - Is there a way to check if a file exists, and if it does then put it in an array? I've tried using glob but that can't check if the file exists.
Well there is an * in your echo i don't think it needs to be there.
And if your movie directory gets updated, or the images. Then your script does not work anymore. Because of the hard slicing (11th file).
Maybe this will work for you:
<?php
// movies
$dir = "movies/";
$files = scandir($dir);
$movies = array();
$images = array();
foreach ($files as $file) {
// check for the mime type:
$mime = mime_content_type($dir . $file);
$type = substr($mime, 0,5);
$filename = pathinfo($dir . $file, PATHINFO_FILENAME);
if ($type == "video") $movies[] = $file;
if ($type == "image") $images[] = $filename;
}
foreach ($movies as $movie) {
$placeholder = true;
foreach($images as $image) {
if (strpos($movie, $image) !== false) {
$placeholder = false;
continue;
}
}
if ($placeholder) {
echo $movie . " - placeholder<br>";
} else {
echo $movie . " - image<br>";
}
}
It works with the mime-type.

don't show the same images

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]);

Foreach loop for fotorama

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."\" />
}

No duplicates in list of images

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!

PHP & Lightbox (href in for loop links to last file of lightbox set)

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!

Categories