Sort Image with latest modified on top - php

I am using the following code from Timthumb to built an thumb gallery on my page from a directory.
However, I would like to have my image sort so that the latest image is on top.
I've try to implement some codes I found on this website, but with no luck.
Can anyone help? You can ignore all the stuff with regex.
<?php
$images = scandir($dirname);
$ignore = Array(".", "..", ".DS_Store", "index.php");
foreach($images as $curimg){
if(!in_array($curimg, $ignore)) {
echo "<li><img src=\"img.php?src=$dirname$curimg&w=500&zc=1&q=50\" alt='' style=\"width:100%;\" /></li>\n ";
}
}
?>

Related

How to list all images in a directory and include them with php

I try to list all images that have been uploaded to the website and embed them with a foreach loop, all I see is just a "not working" thumbnail, like a placeholder for the images, while the image url is already correct.
the code:
$scan = scandir('images');
foreach ($scan as $image){
echo "<img src=\"$image\"> ";
}
I am working with windows xampp environment, just for testing purposes, but i can't test it anywhere else yet, so i can't say if it's a windows thing or not :(
Thank you in advance!
If the images were in a folder called images then add that to the src attribute
$scan = scandir('images');
foreach ($scan as $image){
// remember to remove the `.` and `..` folders
if ( $image != '.' && $image != '..'){
echo '<img src="images/$image">';
}
}

PHP: Pull all images from a specified directory using relative image path

I am trying to create a gallery where images are automatically pulled from a images directory.
When I foreach() each image it returns however it pulls the entire root path as the image src
/home/dev/public_html/assets/images/
this causes the images not to show and shows dead linked images on screen. I need the relative path like this:
/assets/images/imagename.jpg
How would I set a base path/URL?
$dirname = "/home/dev/public_html/assets/images/";
$images = glob($dirname."*.jpg");
foreach($images as $image) {
echo '<img src="'.$image.'" /><br />';
}
I tried setting $dirname to this
$dirname = "/assets/images/";
But I get no result and nothing pulls through at all.
The file i am creating this in, is located outside the public_html folder, i believe this could be casing the issue but the file cannot be relocated.
I would edit the loop in order to remove unnecessary string from path:
foreach($images as $image) {
$src = str_replace('/home/dev/public_html' ,'', $image) ;
echo '<img src="'.$src.'" /><br />';
}
You could may make use of scandir() http://php.net/manual/en/function.scandir.php
Somethink like:
Edit: Changed the code a bit, so scandir only take files, otherwise the first two placed in the array are folders for the parent folders.
$dir = "assets/images/";
$images = scandir($dir);
foreach($images as $image) {
if($image != '.' && $image != '..') {
echo ' <img src="assets/images/'.$image.'"><br>';
}
}
Hope this helped. If not, let me know, so I can change my answer. But I think it should be fine, as far as I understood what you're trying.

how to get image caption using php

I'm using PHP to put all the images in a given folder into a slideshow.
This works fine in the following code:
<?php
//get all image files with a .jpg extension.
$images = glob("" . $directory . "*.jpg");
$imgs = array();
// create array
foreach($images as $image){ $imgs[] = "$image"; }
/****** Display Images ******/
foreach ($imgs as $img) {
//I would like to get the image title here, to put in the echo below
echo "<div><img src='$img' border='0' width=\"100%\" height=\"100%\"/ /></div>";
}
?>
This all goes quite easy, but since I would now also like to add the Titles of the pictures as captions, I need to extract/get this information from the image.
I'm thinking I can get it with something along the lines of the function exif_read_data , but I'm not quite sure how to get the title and not all the meta data . . .
With a little help from the smart people using stackoverflow, this is the final and functional result, as seen in my answer below as well and again, made with bits and pieces from several answers.
<?php
/*
This script will print the image files in a given folder, along with their image description (title in windows file explorer).
*/
// Set the directory where the files reside
$directory = $GLOBALS['directory'];
//get all image files with a .jpg extension.
$imagenpath = glob("" . $directory . "*.jpg");
$imgs = array();
// create array
foreach($imagenpath as $image){ $imgs[] = "$image"; }
// Print each image file with the ImageDescription (the title/caption) in the ALT field
foreach ($imgs as $img) {
$exif_data = exif_read_data($img, 'IFD0');
$exif_description = "";
if (!empty($exif_data['ImageDescription']))
$exif_description = $exif_data['ImageDescription'];
echo "<div><img src='$img' alt='$exif_description' border='0' width=\"100%\" height=\"100%\"/ /></div>";
}
?>
thats everything that can be done, because most pictures are not supported with this function, though, you are doing it wrong because the error that you are getting is because the file is not found, the error you get when the file is found and is not supported is this:
Warning: exif_read_data(file.png): File not supported in
/path/to/file/file.php on line x
and its because you single quoted the variable in
$exif_data = exif_read_data($img, 'IFD0');
the code could be less characters, this is my solution:
<?php
/*
This script will print the image files in a given folder, along with their image description (title in windows file explorer).
*/
// Set the directory where the files reside
$directory = $GLOBALS['directory'];
//get all image files with a .jpg extension.
$imgs = glob($directory . "*.jpg");
// Print each image file with the ImageDescription (the title/caption) in the ALT field
foreach ($imgs as $img) {
$exif_data = exif_read_data($img, 'IFD0');
if (!empty($exif_data['ImageDescription']))
$exif_description = $exif_data['ImageDescription'];
echo "<div><img src=\"$img\" alt=\"$exif_description\" border=\"0\" width=\"100%\" height=\"100%\"/ /></div>";
}
?>
Try this :-
foreach (glob("*.jpg") as $filename) {
$name = basename($filename, '.jpg');
echo "<div><img src='$filename' title='$name' alt='$name' border='0' width=\"100%\" height=\"100%\"/ /></div>";
}
To solve the problem, I basically used the Windows file browser, selected the image file and entered the text in the document comments section.
Then I printed the $exif_data as suggested above by #RamRaider and found out this text ends up in the ImageDescription of the file.
Once I nailed down the errors in my script, along with help from #Abdallah Samman, there wasn't much to it anymore!
Thank you everyone...
The following works beautifully:
<?php
/*
This script will print the image files in a given folder, along with their image description.
*/
// Set the directory where the files reside
$directory = $GLOBALS['directory'];
//get all image files with a .jpg extension.
$imagenpath = glob("" . $directory . "*.jpg");
$imgs = array();
// create array
foreach($imagenpath as $image){ $imgs[] = "$image"; }
// Print each image file with the ImageDescription (the caption) in the ALT field
foreach ($imgs as $img) {
$exif_data = exif_read_data($img, 'IFD0');
$exif_description = "";
if (!empty($exif_data['ImageDescription']))
$exif_description = $exif_data['ImageDescription'];
echo "<div><img src='$img' alt='$exif_description' border='0' width=\"100%\" height=\"100%\"/ /></div>";
}
?>

populate an HTML <ul> with images from a folder automatically

I wanted to ask how can I populate an HTML list with Images located in a folder automatically?
lets say I have 4 pages with 4 different lists.
lets say the lists are as follows: Cars, Motorcycles, Airplanes, Ships
each list entry has a src as a thumbnail and a href as a link to full size image.
my folder contain images with specific titles:
C=Cars
M=Motorcycles
A=Airplanes
S=Ships
then I have a serial number
and the ending is either
T=Thumbnail
F=Full Image
so a complete file name would be C20T.jpg or C20F.jpg.
I am looking for a script that will update the 4 lists according to their subject and populate them automatically with related file names.
from my search here I have found this piece of code but I believe it needs some tweaking and I am not sure how to do this :(
$dir = '/images';
$files = scandir($dir);
$images = array();
foreach($files as $file) {
if(fnmatch('*.jpg',$file)) {
$images[] = $file;
}
}
foreach($images as $image) {
echo '<img src="images/'.$image.'" />';
}
I hope someone could help me with this!
Thank you for your time reading this.
$type = 'c';
$imagesDir = 'images/';
$images = glob($imagesDir . $type . '*[TYPE].{jpg,jpeg,png,gif}', GLOB_BRACE);
This should get all your full images.
Making a ul is then pretty easy...
<ul>
<?php foreach($images as $image): ?>
<li><img src="<?php echo str_replace('[TYPE]', 'T', $image); ?>" alt="" /></li>
<?php endforeach; ?>
</ul>

How to display file names from a folder

I'm using this script to display all the images in a folder, but I can't figure out how to get each image's file name to display underneath it. Any suggestions?
<?php
$dirname = "images";
$images = scandir($dirname);
$ignore = Array(".", "..", "otherfiletoignore");
foreach($images as $curimg){
if (!in_array($curimg, $ignore)) {
echo "<img src='images/$curimg' /><br />\n";
}
}
?>
echo "<img src='images/$curimg' /><br />$curimg<br />\n";
I think nickf's suggestion is the simplest thing you can do achieve what you want without any css or complex structure..

Categories