joshcadman01 / imagecache
Intervention Image 类的缓存扩展
Requires
- php: ~8.2
- illuminate/cache: ^5.5|~6|~7|~8|~9|~10|~11
- illuminate/filesystem: ^5.5|~6|~7|~8|~9|~10|~11
- intervention/image: ~3.5
- nesbot/carbon: ^2.39
- opis/closure: ^3.5
Requires (Dev)
- phpunit/phpunit: ^8.0
This package is auto-updated.
Last update: 2024-09-02 16:38:23 UTC
README
这是一个由 已废弃 包维护的库,用于缓存图像。
Invervention Image Cache
Intervention Image Cache 扩展了 Intervention Image 类 包,使其能够具备图像缓存功能。
该库使用 Illuminate/Cache 包,并可以轻松集成到 Laravel 框架 中。根据您的 Laravel 缓存配置,您可以选择在临时缓冲存储中之间选择文件系统、数据库、Memcached 或 Redis。
原理很简单。对 Intervention Image 类的每次方法调用都被缓存接口捕获并检查。如果这个特定的操作序列已经执行过,数据将直接从缓存中加载,而不是执行资源密集型的图像操作。
安装
您可以使用 Composer 快速轻松地安装此包。
通过 Composer 需求此包
$ composer require joshcadman01/imagecache
现在您可以要求 vendor/autoload.php
文件以 PSR-4 自动加载库。
Laravel 集成
Image Cache 类支持 Laravel 集成。在 Laravel 中使用库的最佳实践是添加 Intervention Image 类的 ServiceProvider 和 Facade。
打开您的 Laravel 配置文件 config/app.php
并添加以下行。
在 $providers
数组中添加此包的服务提供者。
'providers' => array(
[...]
'Intervention\Image\ImageServiceProvider'
),
将此包的 facade 添加到 $aliases
数组中。
'aliases' => array(
[...]
'Image' => 'Intervention\Image\Facades\Image'
),
使用方法
Image Cache 最佳是通过 Intervention Image 类的静态方法 Image::cache
调用。
要创建缓存图像,请使用静态方法 Image::cache
并通过闭包传递图像操作。此方法将自动检测是否存在特定操作的缓存文件。
// run the operations on the image or read a file // for the particular operations from cache $img = Image::cache(function($image) { return $image->make('public/foo.jpg')->resize(300, 200)->greyscale(); });
可选地,为缓存文件指定以分钟为单位的生存时间作为第二个参数。将布尔值 true 作为可选的第三个参数传递,以返回 Intervention Image 对象而不是图像流。
// determine a lifetime and return as object instead of string $img = Image::cache(function($image) { return $image->make('public/foo.jpg')->resize(300, 200)->greyscale(); }, 10, true);
服务器配置
如果您在 Nginx 上启用了静态资源缓存,请将您的缓存目录 ({route} 在配置中) 添加到静态资源处理程序的排除列表中。
# where "cache" is {route}
location ~* ^\/(?!cache).*\.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc|webp|woff|woff2)$ {
expires max;
access_log off;
add_header Cache-Control "public";
}
许可
Intervention Imagecache 类采用 MIT 许可证。