Smarty 3 添加最后修改到图像

Smarty 3 add last modified to image

我正在使用 Smarty,我也在使用浏览器缓存。每当修改图像(使用鸟舍)时,我想在 'scr' 属性中显示它,以便浏览器在修改图像时重新加载图像。我知道这可以在 PHP 中使用 filemtime() 完成,因此它添加了一个 Unix 时间戳,显示文件上次更改的时间。但是当我尝试实现它时,我无法让它工作。

我目前有这个:

<img id="PrvImage" class="js_lightbox" data-id="42" data-image="https://example.com/content/uploads/photos/2017/01/c2d1c2547b0c3feec3f86af7a6484832.jpg" data-context="album" src="https://example.com/content/uploads/photos/2017/01/c2d1c2547b0c3feec3f86af7a6484832.jpg" alt="alt">

我要的是这个:

<img id="PrvImage" class="js_lightbox" data-id="42" data-image="https://example.com/content/uploads/photos/2017/01/c2d1c2547b0c3feec3f86af7a6484832.jpg" data-context="album" src="https://example.com/content/uploads/photos/2017/01/c2d1c2547b0c3feec3f86af7a6484832.jpg?=1483848592" alt="alt">

.tpl 文件中生成 html 的原始代码如下所示:

<img id="PrvImage" {if $profile['user_picture_id']} class="js_lightbox" data-id="{$profile['user_picture_id']}" data-image="{$profile['user_picture']}" data-context="album" {/if} src="{$profile['user_picture']}" alt="{$profile['user_fullname']}">

{$profile['user_picture']} 是文件的路径。所以我将其添加到与 .tpl 文件相关的 PHP 文件中:

$filemtime = filemtime($profile['user_picture']);

// assign variables
$smarty->assign('filemtime', $filemtime);

然后我将 .tpl 修改为:

<img id="PrvImage" {if $profile['user_picture_id']} class="js_lightbox" data-id="{$profile['user_picture_id']}" data-image="{$profile['user_picture']}" data-context="album" {/if} src="{$profile['user_picture']}?={$filemtime}" alt="{$profile['user_fullname']}">

生成:

<img id="PrvImage" class="js_lightbox" data-id="42" data-image="https://example.com/content/uploads/photos/2017/01/c2d1c2547b0c3feec3f86af7a6484832.jpg" data-context="album" src="https://example.com/content/uploads/photos/2017/01/c2d1c2547b0c3feec3f86af7a6484832.jpg?=" alt="alt">

调试后出现以下错误:

filemtime():第 164

行 /var/www/vhosts/qreets.nl/httpdocs/profile.php 中 https://example.com/content/uploads/photos/2017/01/c2d1c2547b0c3feec3f86af7a6484832.jpg 的统计失败

我的问题:

我做错了什么以及如何让它显示上次修改时间?

我知道 Unix 时间戳在 2038 年会有问题所以有没有更好的替代方案具有相同的效果?

好吧,就像我的其他问题一样,人们会查看它们..."and not a single suggestion or attemt to help was made that day"。很棒...

我设法写了一个 javascript 函数来打破缓存。适用于每张图片。如果您不想将时间戳添加到某些图像(例如 google 静态地图),您可以添加一个 id 以便将它们排除在外。我希望它对某人有所帮助。

<script type="text/javascript">  

    function replaceSrc()
    {

        var images = document.getElementsByTagName('img');

        for(var i = 0; i < images.length; i++)
        {
            var dt = new Date();
            var img = images[i];

            if(img.src.length >= 0 & img.id != 'idImageNoTimestamp')
            {
                img.src = img.src + "?" + dt.getTime();
                //javascript:alert(document.lastModified);
            }
        }
    }
    replaceSrc();
      </script>