php - How to cache script image -
i want implement script generates thumbnail on requested file (image). here simple list of actions understand working of script:
- request server
url/preset/filename.jpg
- check if file thumbed
- if not, generate new 1 , return file. if exists return thumbed file.
this works perfect, problem images not cached browser. i'm not familiar caching. thought returning 304 not modified
trigger browser image cache hangs in pending state.
does know how cache these images?
this show part of script:
private function _show_image($file_path, $filename, $new = false) { $this->load->helper('file'); if($new) { header($_server['server_protocol'] . ' 200 ok'); } else { //header('http/1.1 304 not modified'); header($_server['server_protocol'] . ' 304 not modified'); } header('cache-control: public'); // needed i.e. header('content-type: ' . get_mime_by_extension($file_path)); header('content-transfer-encoding: binary'); header('content-length:' . filesize($file_path)); //header('content-disposition: attachment; filename=' . $filename); header('content-disposition: inline; filename=' . $filename); readfile($file_path); exit(); }
update
i updated code first checks if $_server['http_if_modified_since']
set, if check stuff. thing browser not sending header ever..
here send , received headers when accessing file.
request
accept:*/* accept-encoding:identity;q=1, *;q=0 accept-language:nl-nl,nl;q=0.8,en-us;q=0.6,en;q=0.4 connection:keep-alive cookie:phpsessid=heiog0hdasg181l45cfbm7u353; __jwpusr=264f6c61-3752-4682-a61b-e02b822c7815 host:domain.local range:bytes=0- referer:http://domain.local/test user-agent:mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, gecko) chrome/27.0.1453.94 safari/537.36
response
cache-control:public connection:keep-alive content-description:file transfer content-disposition:inline content-length:24103476 content-transfer-encoding:binary content-type:video/mp4 date:thu, 30 may 2013 14:07:02 gmt expires:0 keep-alive:timeout=5, max=98 last-modified:wed, 29 may 2013 09:03:30 gmt server:apache/2.2.22 (win32) php/5.3.13 x-frame-options:sameorigin x-powered-by:php/5.3.13 x-xss-protection:1; mode=block
you may send 304 "not modified" if client sent if-modified-since header such as:
if-modified-since: sat, 29 oct 1994 19:43:31 gmt
or if-none-match:
if-none-match: "737060cd8c284d8af7ad3082f209582d"
you can't know if image still in browser's cache or if has been cleaned out. correct action return image when create thumbnail. if thumbnail exists, check if-modified-since header (this browser indicating has file in cache) , return 304 not modified if file has not changed. otherwise return file since won't in browser's cache.
the if-none-match system uses etags , you're better off avoiding if don't need deal them.
Comments
Post a Comment