junaidiar/imagekit-adapter

此包的最新版本(1.0.0)没有可用的许可证信息。

ImageKit 的文件系统适配器。

1.0.0 2023-08-02 06:58 UTC

This package is not auto-updated.

Last update: 2024-09-26 10:54:51 UTC


README

TaffoVelikof 启发的 ImageKit 配置适配器。此包可用于 php 或 Laravel 项目。按照以下说明安装到您的项目中:

内容

⚙️ 安装

🛠️ 设置 imagekit.io

👩‍💻 PHP 原生使用

🚀 Laravel 中使用

👩‍💻 与 CKEditor 及 Laravel 一起使用

安装

您可以通过 composer 安装此包。

composer require junaidiar/imagekit-adapter

设置

首先,您需要在 ImageKit 上注册一个账户。然后您可以访问 https://imagekit.io/dashboard/developer/api-keys 获取您的公钥、私钥和 URL 终端。

PHP 原生使用

use ImageKit\ImageKit;
use League\Flysystem\Filesystem;
use JunaidiAR\ImageKitAdapter\ImageKitAdapter;

// Setup Client
$client = new ImageKit (
    'your_public_key',
    'your_private_key',
    'your_endpoint_url'
);

// Adapter
$adapter = new ImagekitAdapter($client);

// Filesystem
$fsys = new Filesystem($adapter);

// Check if file exists example
$file = $fsys->fileExists('default-image.jpg');

Laravel 中使用

您可以通过在 AppServiceProviderboot() 方法中扩展 Storage 来添加新的驱动程序。

public function boot()
{
    Storage::extend('imagekit', function ($app, $config) {
        $adapter = new ImagekitAdapter(
          new ImageKit(
              $config['key'],
              $config['secret'],
              $config['endpoint_url']
          ),
        );

        return new FilesystemAdapter(
            new Filesystem($adapter, $config),
            $adapter,
            $config
        );
    });
}

然后在 config/filesystems.php 中创建一个新的磁盘

'disks' => [

  ...

  'imagekit' => [
      'driver' => 'imagekit',
      'key' => env('IMAGEKIT_PUBLIC_KEY'),
      'secret' => env('IMAGEKIT_PRIVATE_KEY'),
      'endpoint_url' => env('IMAGEKIT_ENDPOINT_URL'),
      'throw' => false,
  ]
]

别忘了在 .env 中添加您的密钥

IMAGEKIT_PUBLIC_KEY=your-public-key
IMAGEKIT_PRIVATE_KEY=your-private-key
IMAGEKIT_ENDPOINT_URL=your-endpint-url

现在您可以使用 Laravel 的 Storage 门面了

$result = Storage::disk('imagekit')->put('index.txt', 'This is an index file.');
return response($result);

与 CKEditor 及 Laravel 一起使用

请从官方文档 https://ckeditor.npmjs.net.cn/docs/index.html 安装 ckeditor 5,并确保您已安装 Upload Adapter

您可以使用 ckeditor 5 下载我的入门包 下载此处

然后按照如下设置客户端

<script src="{{ asset('plugins/ckeditor5/build/ckeditor.js') }}"></script>
<script>
  ClassicEditor.create(document.querySelector("#editor"), {
    simpleUpload: {
      // The URL that the images are uploaded to.
      uploadUrl: "{{ route('ckeditor.upload') . '?_token=' . csrf_token() }}",
    },
  }).catch(error => {
    console.error(error);
  });
</script>

然后您可以创建如下方法控制器

public function upload(Request $request)
{
    if ($request->hasFile('upload')) {
        $request->validate([
            'upload' => 'image|mimes:jpeg,png,jpg|max:2048',
        ]);

        $file = $request->file('upload');

        $result = Storage::disk('imagekit')->put('ckeditor', $file);
        $url = env('IMAGEKIT_ENDPOINT_URL') . $result;
        return response()->json(['fileName' => $result, 'uploaded' => 1, 'url' => $url]);
    }
}

最终,如果您在 CKEditor 工具栏上点击插入图片,您将看到神奇的魔法。

编码愉快,谢谢 :)