I'm trying to sort these echo results by using only time, and I can't seen to find a way to read when was the image placed on the folder (to sort them by the specific time).
<?php
$all_files = glob(DIR_PATH."*.*");
for ($i=0; $i<count($all_files); $i++)
{
$image_name = $all_files[$i];
$supported_format = array('gif','jpg','jpeg','png');
$ext = strtolower(pathinfo($image_name, PATHINFO_EXTENSION));
if (in_array($ext, $supported_format))
{
echo '<div class="col" ><img src="/led/images/autoplay/'.str_replace(DIR_PATH,'',$image_name) .' " /><div align="center"><br />Delete</div><br /></div>';
} else {
continue;
}
}
?>
Maybe using usort() and filetime() function:
$files = glob(DIR_PATH."*.*");
usort( $files, function( $a, $b ) {
return filemtime($a) - filemtime($b);
} );
foreach ($files as $file) {
$image_name = $file;
$supported_format = array('gif','jpg','jpeg','png');
$ext = strtolower(pathinfo($image_name, PATHINFO_EXTENSION));
if (in_array($ext, $supported_format))
{
echo '<div class="col" ><img src="/led/images/autoplay/'.str_replace(DIR_PATH,'',$image_name) .' " /><div align="center"><br />Delete</div><br /></div>';
} else {
continue;
}
}
Related
hey so i'm running into the problem that i want to make a image open on a new tab, but with the way i open images into the code i don't know how to do it
<?php
$all_files = glob("commissions/*.*");
for ($i=0; $i<count($all_files); $i++)
{
$image_name = $all_files[$i];
$supported_format = array('gif','jpg','jpeg','png');
$ext = strtolower(pathinfo($image_name, PATHINFO_EXTENSION));
if (in_array($ext, $supported_format))
{
echo ' <img class="comimg" src="'.$image_name .'" alt="'.$image_name.'" />';
} else {
continue;
}
}
?>
just put the image''
if (in_array($ext, $supported_format))
{
echo ' Image Name Here';
} else {
continue;
}
or
if (in_array($ext, $supported_format))
{
echo ' <img class="comimg" src="'.$image_name .'" alt="'.$image_name.'" />';
} else {
continue;
}
You need a new page to display your images for example show-image.php
<?php
if(isset($_GET['image']) && file_exists("commissions/".$_GET['image'])) {
$image_name = $_GET['image'];
$supported_format = array('gif','jpg','jpeg','png');
$ext = strtolower(pathinfo($image_name, PATHINFO_EXTENSION));
if (in_array($ext, $supported_format))
{
echo '<img class="comimg" src="'.$image_name .'" alt="'.$image_name.'" />';
} else {
echo "no preview available";
}
} else echo "image not found";
then change your links to point to that page
echo ' <img class="comimg" src="'.$image_name .'" alt="'.$image_name.'" />';
I've got these two script off of stackoverflow db for inserting images from a folder. Both are 'half working correctly.
<?php
$image_dir = 'uploads/images/';
$per_column = 10;
$validExt = array(
'png' => 'image/png',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpg',
'gif' => 'image/gif',
);
if ($handle = opendir($image_dir)) {
while (false !== ($file = readdir($handle)))
{
$ext = strtolower(substr($file, -3));
if (isset($validExt[$ext]))
{
$stats = stat($image_dir.$file);
$files[$stats['mtime']] = $file;
}
}
closedir($handle);
}
$count = 0;
krsort($files);
$cnt = count($files);
if($cnt)
{
foreach($files as $file)
{
$count++;
echo '<li><img src="' . $image_dir . $file . '" width="20" height="20" title="' . substr($file, 0, -4) . '"/></li>'.chr(10);
if($count % $per_column == 0) { echo '<div class="clear"></div>'; }
}
}
else
{
echo 'no pictures yet...';
}
?>
This one outputs the file, but when there are no images in the folder, I get this error.
'Warning: krsort() expects parameter 1 to be array, null given'
This one below doesn't output the images that are in the folder. I don't get an error message either. If the folder is empty, it does echo 'no picture yet...'
<?php
$image_dir = 'uploads/images/';
$per_column = 10;
$files = glob($image_dir . "*.jpg|png|gif|jpeg");
if(count($files))
{
foreach($files as $file)
{
$count++;
echo '<li><img src="',$image_dir,$file,'" width="20" height="20" title="',$file,'"/></li>';
if($count % $per_column == 0) { echo '<div class="clear"></div>'; }
}
}
else
{
echo 'no pictures yet...';
}
?>
In the first script, you open the dir 'upload/images' but if the folder is empty the while that goes through all the files never executes and the $files variable is never initialized, this is why you're getting that error.
Try to initialize the $files variable before the loop, something like this:
$files = array()
In the second one you are using , instead of . when echoing the image because of that I think you're only getting "/></li> printed. Try replacing , with .
Edit
Try to call glob like this:
glob( $image_dir."*.{jpg,png,jpeg}", GLOB_BRACE );
I am wondering about a "better" way of pulling a random image from a folder.
Like say, to have php just select a random image from folder instead of searching and creating an array of it.
here is how I do it today
<?php
$extensions = array('jpg','jpeg');
$images_folder_path = ROOT.'/web/files/Header/';
$images = array();
srand((float) microtime() * 10000000);
if ($handle = opendir($images_folder_path)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$ext = strtolower(substr(strrchr($file, "."), 1));
if(in_array($ext, $extensions)){
$images[] = $file;
}
}
}
closedir($handle);
}
if(!empty($images)){
$header_image = $images[array_rand($images)];
} else {
$header_image = '';
}
?>
Try this:
<?php
$dir = "images/";
$images = scandir($dir);
$i = rand(2, sizeof($images)-1);
?>
<img src="images/<?php echo $images[$i]; ?>" alt="" />
Below code validate image list by image extension.
<?php
function validImages($image)
{
$extensions = array('jpg','jpeg','png','gif');
if(in_array(array_pop(explode(".", $image)), $extensions))
{
return $image;
}
}
$images_folder_path = ROOT.'/web/files/Header/';
$relative_path = SITE_URL.'/web/files/Header/';
$images = array_filter(array_map("validImages", scandir($images_folder_path)));
$rand_keys = array_rand($images,1);
?>
<?php if(isset($images[$rand_keys])): ?>
<img src="<?php echo $relative_path.$images[$rand_keys]; ?>" alt="" />
<?php endif; ?>
function get_rand_img($dir)
{
$arr = array();
$list = scandir($dir);
foreach ($list as $file) {
if (!isset($img)) {
$img = '';
}
if (is_file($dir . '/' . $file)) {
$ext = end(explode('.', $file));
if ($ext == 'gif' || $ext == 'jpeg' || $ext == 'jpg' || $ext == 'png' || $ext == 'GIF' || $ext == 'JPEG' || $ext == 'JPG' || $ext == 'PNG') {
array_push($arr, $file);
$img = $file;
}
}
}
if ($img != '') {
$img = array_rand($arr);
$img = $arr[$img];
}
$img = str_replace("'", "\'", $img);
$img = str_replace(" ", "%20", $img);
return $img;
}
echo get_rand_img('images');
replace 'images' with your folder.
I searched the internet for hours on end to implement the code to what I wanted. I put together bits of various answers I found online. Here is the code:
<?php
$folder = opendir("Images/Gallery Images/");
$i = 1;
while (false != ($file = readdir($folder))) {
if ($file != "." && $file != "..") {
$images[$i] = $file;
$i++;
}
}
//This is the important part...
for ($i = 1; $i <= 5; $i++) { //Starting at 1, count up to 5 images (change to suit)
$random_img = rand(1, count($images) - 1);
if (!empty($images[$random_img])) { //without this I was sometimes getting empty values
echo '<img src="Images/Gallery Images/' . $images[$random_img] . '" alt="Photo ' . pathinfo($images[$random_img], PATHINFO_FILENAME) . '" />';
echo '<script>console.log("' . $images[$random_img] . '")</script>'; //Just to help me debug
unset($images[$random_img]); //unset each image in array so we don't have double images
}
}
?>
Using this method I was able to implement opendir with no errors (as glob() wasn't working for me), I was able to pull 5 images for a carousel gallery, and get rid of duplicate images and sort out the empty values. One downside to using my method, is that the image count varies between 3 and 5 images in the gallery, probably due to the empty values being removed. Which didn't bother me too much as it works as needed. If someone can make my method better, I welcome you to do so.
Working example (the first carousel gallery at top of website): Eastfield Joinery
I'm quite new to PHP so I'm still learning the very basics, however I'm trying to create an image gallery.
After countless Google searches later, I found a PHP script that does what I want it to do, and after looking at the code and manipulating it slightly it was working perfectly with my site; except that the images were not in alphabetical order.
This is the code
$max_width = 100;
$max_height = 100;
$imagedir = 'gifs/animals/'; //Remember trailing slash
function getPictureType($ext) {
if ( preg_match('/jpg|jpeg/i', $ext) ) {
return 'jpg';
} else if ( preg_match('/png/i', $ext) ) {
return 'png';
} else if ( preg_match('/gif/i', $ext) ) {
return 'gif';
} else {
return '';
}
}
function getPictures() {
global $max_width, $max_height, $imagedir;
if ( $handle = opendir($imagedir) ) {
$lightbox = rand();
echo '<ul id="pictures">';
while ( ($file = readdir($handle)) !== false ) {
if ( !is_dir($file) ) {
$split = explode($imagedir, $file);
$ext = $split[count($split) - 1];
if ( ($type = getPictureType($ext)) == '' ) {
continue;
}
$name = substr($file, 0, -4);
$title = str_replace("_"," ",$name);
echo '<li><a href="'.$name.'">';
echo '<img src="thumbs/'.$file.'" class="pictures" alt="'.$file.'" />';
echo '</a>';
echo ''.$title.'';
echo '</li>';
}
}
echo '</ul>';
}
}
I've used the scandir() function which works in sorting them alphabetically, however I was left with an array. I then used the implode function to join the array together, however after that I was stuck with what to do.
Any help would be greatly appreciated!
Cheers.
You can use glob() to get the files from a directory, sorted alphabetically:
$files = glob('gifs/animals/*.{gif,jpg,png}', GLOB_BRACE);
To iterate over your files, use a foreach loop:
foreach($files as $file){
$title = str_replace("_"," ",$file);
echo '<li><a href="'.$name.'">';
echo '<img src="thumbs/'.basename($file).'" class="pictures" alt="'.basename($file).'" />';
echo '</a>';
echo ''.$title.'';
echo '</li>';
}
What's wrong with the arrays?
Also it would be better if you use pathinfo to obtain the filename and the extension.
$max_width = 100;
$max_height = 100;
$imagedir = 'gifs/animals/'; //Remember trailing slash
function getPictureType($ext) {
if ( preg_match('/jpg|jpeg/i', $ext) ) {
return 'jpg';
} else if ( preg_match('/png/i', $ext) ) {
return 'png';
} else if ( preg_match('/gif/i', $ext) ) {
return 'gif';
} else {
return '';
}
}
function getPictures() {
global $max_width, $max_height, $imagedir;
if ( $files = scandir($imagedir) ) {
$lightbox = rand();
echo '<ul id="pictures">';
foreach ($files as $file) {
$full_path = $imagedir.'/'.$file;
if ( !is_dir($file) ) {
$finfo = pathinfo($full_path);
$ext = $finfo['extension'];
if ( ($type = getPictureType($ext)) == '' ) {
continue;
}
$name = $finfo['filename'];
$title = str_replace("_"," ",$name);
echo '<li><a href="'.$name.'">';
echo '<img src="thumbs/'.$file.'" class="pictures" alt="'.$file.'" />';
echo '</a>';
echo ''.$title.'';
echo '</li>';
}
}
echo '</ul>';
}
}
I have this code set up for inserting any image that is uploaded to my "images" folder, right into a gallery I have.... My problem is that it kind of inserts them randomly.. I would like to set it up to insert the most recently uploaded picture to the end of the gallery "row", any suggestions? Thanks
<?php
$image_dir = 'uploads/images/';
$per_column = 10;
if ($handle = opendir($image_dir)) {
while (false !== ($file = readdir($handle)))
{
if ($file != '.' && $file != '..')
{
if(strstr($file,'.png'))
{
$files[] = $file;
}
if(strstr($file,'.jpg'))
{
$files[] = $file;
}
if(strstr($file,'.gif'))
{
$files[] = $file;
}
if(strstr($file,'.jpeg'))
{
$files[] = $file;
}
}
}
closedir($handle);
}
if(count($files))
{
foreach($files as $file)
{
$count++;
echo '<li><img src="',$image_dir,$file,'" width="20" height="20" title="',$file,'"/></li>';
if($count % $per_column == 0) { echo '<div class="clear"></div>'; }
}
}
else
{
echo 'no pictures yet...';
}
?>
<?php
$image_dir = 'uploads/images/';
$per_column = 10;
$validExt = array(
'png' => 'image/png',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpg',
'gif' => 'image/gif',
);
if ($handle = opendir($image_dir)) {
while (false !== ($file = readdir($handle)))
{
$ext = strtolower(substr($file, -3));
if (isset($validExt[$ext]))
{
$stats = stat($image_dir.$file);
$files[$stats['mtime']] = $file;
}
}
closedir($handle);
}
$count = 0;
krsort($files);
$cnt = count($files);
if($cnt)
{
foreach($files as $file)
{
$count++;
echo '<li><img src="' . $image_dir . $file . '" width="20" height="20" title="' . substr($file, 0, -4) . '"/></li>'.chr(10);
if($count % $per_column == 0) { echo '<div class="clear"></div>'; }
}
}
else
{
echo 'no pictures yet...';
}
I'm not sure what your file format is, but try setting the filename to the current time using time() when uploading.
When you list your images, try using glob() instead of readdir.
Your code might then go as follows (untested):
$image_dir = 'uploads/images/';
$per_column = 10;
$files = glob($image_dir . "*.jpg|png|gif|jpeg");
if(count($files))
{
foreach($files as $file)
{
$count++;
echo '<li><img src="',$image_dir,$file,'" width="20" height="20" title="',$file,'"/></li>';
if($count % $per_column == 0) { echo '<div class="clear"></div>'; }
}
}
else
{
echo 'no pictures yet...';
}
Coupling this with giving the uploaded file a filename of the current time, this should order your images by most recent.
The most important line here is $files = glob($image_dir . "*.jpg|png|gif|jpeg");. This will scan the directory for any file (*) ending in .jpg, .png, .gif or .jpeg. You don't need to worry about . and ..; the expression filters that out.