Jump to content

Re-cache image with CURL


Nygganh
Go to solution Solved by 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!
Edited by Nygganh
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.