按日期排序图片,最新的PHP格式 [英] Sort pictures by date, newest first in PHP
本文介绍了按日期排序图片,最新的PHP格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,我在网上发现了一个php脚本,可以帮助我自动在图像文件夹中显示图像,包括缩略图。它效果很好,但我想先在网页上用最新的图片对照片进行排序。有人可以帮我这么做吗?非常感谢提前!
Hi guys, I found a php script online that helps me automatically display images in an image folder, including thumbnails. 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>';
}
?>
如果您有任何疑问,或需要我澄清一些事情,请告诉我。
Jerome
If you have any questions, or need me to clarify something just let me know.
Jerome
推荐答案
src,
dest,
desired_width){
/ * 阅读源图像* /
desired_width) { /* read the source image */
这篇关于按日期排序图片,最新的PHP格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文