1

问题

使用 PHP 在图像更改前后使用相同的 URL 下载远程图像,即使来源不同,也会下载相同的照片。某处图像被缓存。

问题不在于浏览器缓存,因为我在通过 FTP 复制后直接通过 Windows 资源管理器查看照片。

例子

下午 1:00:下载照片 URL A -> 下载照片 A

下午 1:30:照片 A 更改为照片 B,但照片 URL 保持不变

2:00 pm:再次下载照片 URL A -> 下载照片 A(但应该是照片 B)

我的下载脚本

function resampimagejpg( $forcedwidth, $forcedheight, $sourcefile, $destfile )
{

$fw = $forcedwidth;
$fh = $forcedheight;
$is = getimagesize( $sourcefile );
if( $is[0] >= $is[1] )
{
    $orientation = 0;
}
else
{
    $orientation = 1;
    $fw = $forcedheight;
    $fh = $forcedwidth;
}
if ( $is[0] > $fw || $is[1] > $fh )
{
    if( ( $is[0] - $fw ) >= ( $is[1] - $fh ) )
    {
        $iw = $fw;
        $ih = ( $fw / $is[0] ) * $is[1];
    }
    else
    {
        $ih = $fh;
        $iw = ( $ih / $is[1] ) * $is[0];
    }
    $t = 1;
}
else
{
    $iw = $is[0];
    $ih = $is[1];
    $t = 2;
}
if ( $t == 1 )
{
    $img_src = imagecreatefromjpeg( $sourcefile );
    $img_dst = imagecreatetruecolor( $iw, $ih );
    imagecopyresampled( $img_dst, $img_src, 0, 0, 0, 0, $iw, $ih, $is[0], $is[1] );
    if( !imagejpeg( $img_dst, $destfile, 95 ) )
    {
        exit( );
    }
    return true;
}
else if ( $t == 2 )
{
    copy( $sourcefile, $destfile );
    return true;
}
}
4

1 回答 1

1

Apache can cache images, that depends on the setup. The same applies to the browser.

The easiest way to bypass caching for a certain asset, is to add a (reasonably...) unique query string at the end, for example a timestamp:

// load image always, assuming output to html
<img src="/path/to/image.jpg?<?php echo time(); ?>">

Note that time() is not exactly unique, it effectively limits caching to 1 second...

于 2012-09-26T15:06:13.953 回答