Log in

View Full Version : Display latest image in directory



pellsonline
03-19-2014, 12:15 AM
I have images in a directory that are uploaded from smart phones. When uploaded the file name is appended with date & time stamp in Epoch format
(e.g. image-1392938655.jpg).

I want to display, on a specific page, only the latest image in that directory.

Can anyone give me the code I need?

Thank You

Beverleyh
03-19-2014, 06:42 AM
Google is your friend https://www.google.com/search?q=php%20display%20latest%20image%20in%20directory

pellsonline
03-20-2014, 12:10 AM
I followed link you suggested and found the code to do what I wanted.

This is what I am using:
<?php
$dir = 'fileDir/';
$base_url = '../demo/uploads/';
$newest_mtime = 0;
$show_file = 'BROKEN';
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if (($file != '.') && ($file != '..')) {
$mtime = filemtime("$dir/$file");
if ($mtime > $newest_mtime) {
$newest_mtime = $mtime;
$show_file = "$base_url/$file";
}
}
}
}
print '<img src="' .$show_file. '" alt="Image Title Here">';
?>


This works fine but displays image in original size.
I am placing the image into an existing web page and wish to resize it to 250px wide while maintaining height ratio.

Searched Google but can't find code to insert into the above.

Any suggestions?
Thanks

Beverleyh
03-20-2014, 06:25 AM
You can scale the image in HTML - just use the width attribute in the img tag (without a height attribute) http://www.w3schools.com/tags/att_img_width.asp

However, as this will still force the user to download the full size image, you might want to also look at resizing with something like ImageMagick https://www.google.com/search?q=resize%20image%20imagemagick or GD Library https://www.google.com/search?q=resize%20image%20gd%20php