justdvlp/imagecache

Intervention Image 类的缓存扩展

dev-master 2024-04-18 09:01 UTC

This package is auto-updated.

Last update: 2024-09-18 09:46:28 UTC


README

此包已被废弃,将不再维护。

Intervention Image Cache (已废弃)

Intervention Image Cache 扩展了 Intervention Image Class 包,使其能够实现图像缓存功能。

该库使用 Illuminate/Cache 包,并可以轻松集成到 Laravel 框架 中。根据您的 Laravel 缓存配置,您可以选择使用文件系统、数据库、Memcached 或 Redis 作为临时缓冲存储。

原理很简单。每次调用 Intervention Image 类的方法都会被缓存接口捕获和检查。如果这个特定的操作序列已经执行过,数据将直接从缓存加载,而不是进行资源密集型的图像操作。

安装

您可以使用 Composer 快速轻松地安装此包。

通过 Composer 需求此包

$ composer require intervention/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'
),

用法

最好通过 Intervention Image 类的静态方法 Image::cache 调用 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 许可证 的许可。