Jump to content

Re-cache image with CURL


Nygganh

Recommended Posts

Hi,

 

I have a image upload script and a image viewer. The image viewer works something like this:

http://www.website.com/image/id_of_image/width/height

 

That will cache and render an image with headers looking something like this:

header('Content-Type: ' . $content_type);
header('Content-Length: ' . strlen($image_data));
header('Cache-Control: max-age=' . Date::HOUR . ', public, must-revalidate');
header('Expires: ' . gmdate('D, d M Y H:i:s', (isset($_GET['clear_cache']) ? NOW - Date::YEAR : NOW + Date::HOUR)) . ' GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $this->last_modified_time) . ' GMT');
header('ETag: ' . $etag);
 
Everything's working great, expect for when I'm uploading a new image over an existing one (with same filename), I automatically refresh the page after the upload but the image doesn't get refreshed until I manually refresh the page.
 
I have tried to make a CURL request to the image right after the image is uploaded, but that doesn't seem to trigger the cache to get renewed.
It looks something like this:
curl_setopt($ch, CURLOPT_FORBID_REUSE, TRUE);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_exec($ch);
 
The CURL request is made correctly and I can see the request in my access log.
In my CURL request, I pass ?clear_cache=1. This trick, to change the Expires-header on the CURL request only seems to work in Chrome, but that's because Chrome doesn't give me the "If-None-Match" and is not cached correctly. But maybe that's on the right track?
 
What is a good approach here? I don't want to add a timestamp to my image URL:s since that would trigger the images to never cache.
 
Thanks!
Link to comment
https://forums.phpfreaks.com/topic/284037-re-cache-image-with-curl/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.