使用Laravel Intervention库缓存动态图像无法正常工作

时间:2015-06-23 17:02:17

标签: php image .htaccess laravel caching

Webconsole with requests

我目前正在使用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文件

对其进行排序

1 个答案:

答案 0 :(得分:11)

您可以在浏览器端缓存它。只需将标题添加到响应中,如下所示,

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