从图像中获取日期

Get date from images

我正在构建一个用于上传图片的应用程序,但当我需要获取上传图片的日期时遇到问题。图片将随机上传,因此日期必须与上传日期相同。 我正在使用 electron 开发应用程序,为了上传图片我正在使用 multer。

这是我的图片库代码,我可以在其中将图片上传到图片库。如果有人知道应该怎么做,我将不胜感激。

// Image gallery
    app.get('/gallery', (req, res) => {
    let images = getImagesFromDir(path.join(__dirname, 'public/uploads'));
    let time = new Date(images.lastModified); //here I'm trying to get date
    res.render('gallery', { title: 'Galerija slik', images: images, time: time })
});

// dirPath: target image directory
function getImagesFromDir(dirPath) {

    // All iamges holder, defalut value is empty
    let allImages = [];

    // Iterator over the directory
    let files = fs.readdirSync(dirPath);

    // Iterator over the files and push jpg and png images to allImages array.
    for (file of files) {
        let fileLocation = path.join(dirPath, file);
        var stat = fs.statSync(fileLocation);
        if (stat && stat.isDirectory()) {
            getImagesFromDir(fileLocation); // process sub directories
        } else if (stat && stat.isFile() && ['.jpg', '.png'].indexOf(path.extname(fileLocation)) != -1) {
            allImages.push('uploads/'+file); // push all .jpf and .png files to all images 
        }
    }

    // return all images in array formate
    return allImages;
}

这是我的 HTML 代码

      <div id="content">

        <h2 class="text-center"><%= title %></h2>

    <div id="filters">
        <a href="#" id="newer" class="selected">Newer</a>
        <a href="#" id="older">Older</a>
    </div>
    <div id="posts">

        <% for(let image of images) {%>
            <article class="post">
                <figure>
                    <a href="<%= image %>" data-fancybox="1"><img src="<%= image %>" /></a>
                </figure>
                <time><%= time %></time>
        </article>
          <% } %>

    </div>

我假设你想获取每张图片的最后修改日期。

// Image gallery
    app.get('/gallery', (req, res) => {
    let images = getImagesFromDir(path.join(__dirname, 'public/uploads'));
    res.render('gallery', { title: 'Galerija slik', images: images })
});

// dirPath: target image directory
function getImagesFromDir(dirPath) {

    // All iamges holder, defalut value is empty
    let allImages = [];

    // Iterator over the directory
    let files = fs.readdirSync(dirPath);

    // Iterator over the files and push jpg and png images to allImages array.
    for (file of files) {
        let fileLocation = path.join(dirPath, file);
        var stat = fs.statSync(fileLocation);
        if (stat && stat.isDirectory()) {
            getImagesFromDir(fileLocation); // process sub directories
        } else if (stat && stat.isFile() && ['.jpg', '.png'].indexOf(path.extname(fileLocation)) != -1) {
            allImages.push({path: 'uploads/'+file, lastModifiedDate: stat.mtime}); // push all .jpf and .png files to all images 
        }
    }

    // return all images in array formate
    return allImages;
}

并且 html 应该像下面这样更新。

<div id="posts"> <% for(let image of images) {%> <article class="post"> <figure> <a href="<%= image.path %>" data-fancybox="1"><img src="<%= image.path %>" /></a> </figure> <time><%= image.lastModifiedDate %></time> </article> <% } %> </div>