简体   繁体   English

在文件夹中显示图库中的最新图像

[英]displaying most recent images in a gallery from folder

I am saving images that are created by a user with the canvas tag into a folder on my site: 我将用户使用canvas标签创建的图像保存到我网站上的文件夹中:

<?php 
$data = $_POST['img']; 
$data = str_replace('data:image/png;base64,','',$data);
$data = str_replace(' ', '+', $data);
$img = base64_decode($data);
$path = 'images/' . uniqid() . '.png';
if(file_put_contents($path, $img)){
print $path;
}else{
header("HTTP/1.1 500 Internal Server Error");
}
?>

and then displaying the images in a gallery: 然后在图库中显示图像:

<?php // display source code
$folder_path = 'images/';
$files = glob($folder_path . "*.{JPG,jpg,gif,png,bmp}", GLOB_BRACE);
foreach($files as $file){
echo '<img src="'.$file.'" />';    
}
?>

I would like the most recent images displayed at the top and if possible only to show a handful of the images at one time and have a 'show more' function that will make visible the hidden elements when clicked 我希望最近显示的图像显示在顶部,如果可能的话,一次只能显示少量图像,并具有“显示更多”功能,当单击该功能时,隐藏的元素将可见

您可以将图像的名称以及时间戳保存在数据库(例如mysql)中,并以top (number of records to be shown)order by (timestamp column) desc查询最近的图像。

You'll want to sort the images in to an array first, by date, and then display them from the array like so: 您需要先按日期将图像分类到一个数组中,然后从数组中显示它们,如下所示:

$folder_path = 'images/';

$files = glob($folder_path . "*.{JPG,jpg,gif,png,bmp}", GLOB_BRACE);

//Images array
$images = array();

foreach($files as $key => $file) {
    //Get the modified time of the file
    $filetime = filemtime($file);

    //Add the info to an array
    $images[$key]['filepath'] = $file;
    $images[$key]['filetime'] = $filetime;
}

//Sort the array
usort($images, function($a, $b){
    return $b['filetime'] - $a['filetime'];
});

//Now you can display the images
foreach($images as $image) {
    echo '<img src="' . $image["filepath"] . '" />';
}

If you decide that you want to display them in order of oldest images first, then simply swap the $b and $a around in the usort() function, like so: 如果您决定先按最旧的图像显示它们,则只需在usort()函数中交换$ b和$ a,如下所示:

usort($images, function($a, $b){
    return $a['filetime'] - $b['filetime'];
});

See more information on usort here: http://php.net/manual/en/function.usort.php 在此处查看有关usort的更多信息: http : //php.net/manual/en/function.usort.php

Regarding the "Show More" button, you want to be looking at javascript / jquery and working out how to do that, it's a very broad question to be answered here I'm afraid. 关于“显示更多”按钮,您想查看javascript / jquery并确定如何执行该操作,这是一个非常广泛的问题,恐怕需要在这里回答。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM