[英]Sort pictures by date, newest first in PHP
I found a php script online that helps me automatically display images in an image folder, including thumbnails. 我在网上找到了一个php脚本,可以帮助我自动在图像文件夹中显示图像,包括缩略图。 It works well, but I want to sort my pictures with the newest pictures first in the webpage.
它运作良好,但我想将网页中最新的图片排在第一位。 Can someone help me do this?
有人可以帮我吗? Many thanks in advance!
提前谢谢了!
<?php
/* function: generates thumbnail */
function make_thumb($src,$dest,$desired_width) {
/* read the source image */
$source_image = imagecreatefromjpeg($src);
$width = imagesx($source_image);
$height = imagesy($source_image);
/* find the "desired height" of this thumbnail, relative to the desired width */
$desired_height = floor($height*($desired_width/$width));
/* create a new, "virtual" image */
$virtual_image = imagecreatetruecolor($desired_width,$desired_height);
/* copy source image at a resized size */
imagecopyresized($virtual_image,$source_image,0,0,0,0,$desired_width,$desired_height,$width,$height);
/* create the physical thumbnail image to its destination */
imagejpeg($virtual_image,$dest);
}
/* function: returns files from dir */
function get_files($images_dir,$exts = array('jpg')) {
$files = array();
if($handle = opendir($images_dir)) {
while(false !== ($file = readdir($handle))) {
$extension = strtolower(get_file_extension($file));
if($extension && in_array($extension,$exts)) {
$files[] = $file;
}
}
closedir($handle);
}
return $files;
}
/* function: returns a file's extension */
function get_file_extension($file_name) {
return substr(strrchr($file_name,'.'),1);
}
/** settings **/
$images_dir = 'August/';
$thumbs_dir = 'August Thumbnails/';
$thumbs_width = 200;
$images_per_row = 4;
/** generate photo gallery **/
$image_files = get_files($images_dir);
if(count($image_files)) {
$index = 0;
foreach($image_files as $index=>$file) {
$index++;
$thumbnail_image = $thumbs_dir.$file;
if(!file_exists($thumbnail_image)) {
$extension = get_file_extension($thumbnail_image);
if($extension) {
make_thumb($images_dir.$file,$thumbnail_image,$thumbs_width);
}
}
echo '<a href="',$images_dir.$file,'"><img src="',$thumbnail_image,'" /></a>';
if($index % $images_per_row == 0) { echo '<div class="clear"></div>'; }
}
echo '<div class="clear"></div>';
}
else {
echo '<p>Hang on! I am still growing.</p>';
}
?>
If you have any questions, or need me to clarify something just let me know. 如果您有任何疑问,或者需要我澄清一下,请告诉我。
Jerome 杰罗姆
Use filemtime() 使用filemtime()
function get_files($images_dir,$exts = array('jpg')) {
$files = array();
$times = array();
if($handle = opendir($images_dir)) {
while(false !== ($file = readdir($handle))) {
$extension = strtolower(get_file_extension($file));
if($extension && in_array($extension,$exts)) {
$files[] = $file;
$times[] = filemtime($images_dir . '/' . $file);
}
}
closedir($handle);
}
array_multisort($files, SORT_DESC, $times);
return $files;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.