2015-06-23 60 views
4

Webconsole with requests緩存動態圖像與Laravel干預庫工作不

我使用Laravel 5的那一刻,並採取干預圖像融合的優勢:http://image.intervention.io/

我使用它動態地使影像有一個像網址這樣的:

http://example.org/media/1600x340/2/image_name.jpg

所以所有的工作,但即時通知這些圖像不會緩存。其他做,但不是我的動態。

這是我的htaccess

<FilesMatch "\.(ico|pdf|jpg|jpeg|png|gif|html|htm|xml|txt|xsl)$"> 
    Header set Cache-Control "max-age=31536050" 
</FilesMatch> 

<IfModule mod_expires.c> 
ExpiresActive On 
ExpiresByType image/jpg "access plus 1 year" 
ExpiresByType image/jpeg "access plus 1 year" 
ExpiresByType image/gif "access plus 1 year" 
ExpiresByType image/png "access plus 1 year" 
ExpiresByType text/css "access plus 1 month" 
ExpiresByType application/pdf "access plus 1 month" 
ExpiresByType text/x-javascript "access plus 1 month" 
ExpiresByType application/x-shockwave-flash "access plus 1 month" 
ExpiresByType image/x-icon "access plus 1 year" 
ExpiresDefault "access plus 2 days" 
</IfModule> 

如果我通過FTP實際連接,並期待在高速緩存圖像,它們存儲這樣的:

/存儲/框架/緩存/ 55/BA/0245f7453543100cf0aaa4709eca82f4

如果每次都必須重新加載,緩存又有什麼意義呢?我懷疑問題是瀏覽器不知道這些文件是什麼(它們沒有擴展名),所以它不會緩存它們。

下面是一些例子laravel代碼:

$cache_image = Image::cache(function ($image) use ($size, $name) { 
    return $image->make(url('/uploads/' . $name))->resize($size[0], $size[1], function ($constraint) { 
     $constraint->aspectRatio(); 
     $constraint->upsize(); 
    }); 
}, $cache_length)->sharpen(5); 

...

return Response::make($cache_image, 200, [ 'Content-Type' => 'image' ]); 

頭在Chrome看起來是這樣的:

GET /media/1600x340/2/img_0033-copy.jpg HTTP/1.1 
Host: mywebsite.com.au 
Connection: keep-alive 
Accept: image/webp,*/*;q=0.8 
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36 FirePHP/4Chrome 
Referer: http://mywebsite.com.au/functions 
Accept-Encoding: gzip, deflate, sdch 
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6 
Cookie: __cfduid=d4fee20f7753d88962b94c880cec57f981435071761; _gat=1; _ga=GA1.3.1903752380.1435076392; XSRF-TOKEN=eyJpdiI6IkFpMHJDTDZtTTVoczVaSkxaZ1hjNmc9PSIsInZhbHVlIjoiVlVRSk1ibCtCWHpZNW5SQkl0NTA2bVN2Tmh4c0N6aW5TWGZ1Njk1WG1Gb05XXC91dW5DYjdLNWpGXC9kSWFsYXZ6bDZMQ0NmZE15UlVvMFV3OEN1bWJPZz09IiwibWFjIjoiZGI1OTYwM2IwZTNhYWFlM2I5NGExNTkzNTlkN2YxNGI4Y2IzNjI5ODU5Y2RjNTRkODYxMWIxNDJiMDM3NGI5YyJ9; laravel_session=eyJpdiI6Ikc5a3ZibmZraG9aZElnYWFKUG5CWHc9PSIsInZhbHVlIjoiblRWcDRxQW9ORGl0RFdubDJ3NTdXR3lNdUcyWmtnSkEyXC9sZFI3VWQ2NkVBdUxJNWVpZU5ubEZ2RG1HbVF6SHdYNFhob21UOEVjOVZxMklKdndxQ3dBPT0iLCJtYWMiOiIwMThlMGZkMTdlNWM5MDQ4OWYxNzIwMzE2NzhhMzEzMjg1MGM4NjI2NzRkMmJhMWEzNmUzN2MyNjJhYzdjNjQ1In0%3D 
X-FirePHP-Version: 0.0.6 
X-Wf-Max-Combined-Size: 261120 

任何想法?希望我可以用htaccess文件來解決這個問題

回答

8

你可以在瀏覽器端緩存它。只需在下面添加標題即可,

return Response::make($cache_image, 200, [ 'Content-Type' => 'image' ]) 
     ->setMaxAge(604800) //seconds 
     ->setPublic(); 
+1

完美工作!非常感謝! – cardi777