按日期排序图片,最新的PHP格式

时间:2014-08-18 19:54:44

标签: php

我在网上发现了一个php脚本,它可以帮助我自动在图像文件夹中显示图像,包括缩略图。它效果很好,但我想先在网页上用最新的图片对照片进行排序。有人可以帮我这么做吗?非常感谢提前!

<?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>';
}

?>

如果您有任何疑问,或需要我澄清一些事情,请告诉我。

杰罗姆

1 个答案:

答案 0 :(得分:0)

使用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;
}