haggag/imagecache

Intervention Image Class的缓存扩展

1.0.0 2024-04-14 20:44 UTC

This package is auto-updated.

Last update: 2024-09-14 21:39:25 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数组中添加此包的service 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许可授权。