taffovelikoff / imagekit-adapter
Flysystem适配器用于ImageKit。
Requires
- php: ^8.0
- imagekit/imagekit: ^2.0
- league/flysystem: ^2.0.4 || ^3.0.0
Requires (Dev)
- phpspec/prophecy-phpunit: ^2.0.1
- phpunit/phpunit: ^9.5.4
This package is auto-updated.
Last update: 2024-09-23 18:22:56 UTC
README
这是一个Flysystem适配器,用于ImageKit。这个包之前仅适用于Laravel,但现在可以在任何PHP项目中使用!如果你在Laravel应用程序中使用了此包的旧版本,请阅读“在Laravel中使用”部分。
内容
安装
您可以通过composer安装此包
composer require taffovelikoff/imagekit-adapter
配置
首先,您需要注册ImageKit账户。然后,您可以访问https://imagekit.io/dashboard#developers以获取公钥、私钥和URL端点。
使用
use ImageKit\ImageKit; use League\Flysystem\Filesystem; use TaffoVelikoff\ImageKitAdapter\ImageKitAdapter; // Client $client = new ImageKit ( 'your_public_key', 'your_private_key', 'your_endpoint_url' // Should look something like this https://ik.imagekit.io/qvkc... ); // Adapter $adapter = new ImagekitAdapter($client); // Filesystem $fsys = new Filesystem($adapter); // Check if file exists example $file = $fsys->fileExists('default-image.jpg');
如果您需要在文件更新/删除后清除缓存,可以将"purge_cache"添加到适配器的$options数组中。
$adapter = new ImagekitAdapter($client, $options = [ 'purge_cache_update' => [ 'enabled' => true, 'endpoint_url' => 'your_endpoint_url' ] ]);
这将创建一个清除缓存请求。您可以在此处了解更多信息:https://docs.imagekit.io/features/cache-purging
在Laravel中使用
您可以在AppServiceProvider的boot()方法中扩展Storage来创建新的驱动器。
public function boot() { Storage::extend('imagekit', function ($app, $config) { $adapter = new ImagekitAdapter( new ImageKit( $config['public_key'], $config['private_key'], $config['endpoint_url'] ), $options = [ // Optional 'purge_cache_update' => [ 'enabled' => true, 'endpoint_url' => 'your_endpoint_url' ] ] ); return new FilesystemAdapter( new Filesystem($adapter, $config), $adapter, $config ); }); }
然后在config/filesystems.php中创建一个新的磁盘
'imagekit' => [ 'driver' => 'imagekit', 'public_key' => env('IMAGEKIT_PUBLIC_KEY'), 'private_key' => env('IMAGEKIT_PRIVATE_KEY'), 'endpoint_url' => env('IMAGEKIT_ENDPOINT_URL') ],
别忘了在.env中添加您的密钥
IMAGEKIT_PUBLIC_KEY = your-public-key IMAGEKIT_PRIVATE_KEY = your-private-key IMAGEKIT_ENDPOINT_URL = your-endpint-url
现在您可以使用Laravel的Storage门面了
Storage::disk('imagekit')->put('test.txt', 'This is a test file.'); return response(Storage::disk('imagekit')->get('test.txt'));
如果您已经在Laravel应用程序中使用了较旧的taffovelikoff/imagekit-adapter
版本,您很可能发布了配置文件config/imagekit.php。您可以在那里设置一些选项
return [ 'purge_cache_update' => true, 'extend_storage' => true, ];
设置extend_storage => true会自动扩展Storage门面并创建'imagekit'驱动器。如果您使用了该选项,您需要像上面的示例一样手动在AppServiceProvider中添加新的驱动器。
如果将purge_cache_update设置设置为true,则在删除/更新文件时将执行缓存清除请求。为了保持此功能,现在您需要做的只是添加purge_cache_update参数到ImageKitAdapter选项中,当扩展存储时。
贡献
欢迎提交拉取请求。请随时提出任何问题或反馈作为讨论点。