使用Laravel Intervention库无法缓存动态图像 [英] caching dynamic images with Laravel Intervention library not working

查看:68
本文介绍了使用Laravel Intervention库无法缓存动态图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Laravel 5,并利用干预图像集成: http://image.intervention .io/

I am using Laravel 5 at the moment and taking advantage of the intervention image integration: http://image.intervention.io/

我正在动态使用它,因此图像具有这样的URL:

I am using it dynamically so images have a URL like this:

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

所有工作正常,但请注意,这些图像将不会缓存.其他的,但我不是动态的.

So all working, but im noticing these images will not cache. Other do, but not my dynamic ones.

这是我的htaccess

This is my 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连接并查看缓存的图像,则它们的存储方式如下:

If I actually connect via FTP and look at the cached images, they are stored like this:

/storage/framework/cache/55/ba/0245f7453543100cf0aaa4709eca82f4

/storage/framework/cache/55/ba/0245f7453543100cf0aaa4709eca82f4

如果每次都要重新加载缓存,缓存有什么意义?我怀疑问题在于浏览器不知道这些文件是什么(它们没有扩展名),因此不缓存它们.

What is the point in caching if it has to reload it every time? I suspect the problem is that the browser doesn't know what these files are (they have no extension) so it doesn't cache them.

这是一些laravel示例代码:

Here is some example laravel code:

$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标头中的内容如下:

Header in chrome looks like this:

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文件来解决这个问题

Any ideas? Hopefully I can sort this out with the htaccess file

推荐答案

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

you can cache it at browser side. simply add header to response as below,

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

这篇关于使用Laravel Intervention库无法缓存动态图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆