Problem
Using PHP to download a remote image using the same URL before and after the image is changed, downloads the same photo, even though the source is different. Somewhere the image is being cached.
The issue is not browser caching as I am looking at the photo directly through windows explorer after copying via FTP.
Example
1:00pm: Download Photo URL A -> Downloads Photo A
1:30pm: Photo A changed to Photo B, but photo URL remains the same
2:00pm: Download Photo URL A Again -> Downloads Photo A (But Should be Photo B)
My download script
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;
}
}
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:
Note that
time()is not exactly unique, it effectively limits caching to 1 second…