benbjurstrom / glint
A Cloudflare Images library for Laravel
v0.1.4
2023-03-29 20:36 UTC
Requires
- php: ^8.1
- ext-gd: *
- benbjurstrom/cloudflare-images-php: ^0.5.0
- illuminate/contracts: ^9.0|^10.0
- kornrunner/blurhash: ^1.2
- sammyjo20/saloon-laravel: ^2.0
- spatie/laravel-package-tools: ^1.14.0
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^7.9
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^8.0
- pestphp/pest: ^2.0
- pestphp/pest-plugin-arch: ^2.0
- pestphp/pest-plugin-laravel: ^2.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
README
️⚠️ WARNING: This package is still in development and not ready for production use. ⚠️
For example, currently there's no option to use a custom model in place of the package's Image and ImageType models. I'll add that functionality once my implementation is stable.
Installation
Install the package via composer:
composer require benbjurstrom/glint
Add cloudflare credentials to your services config file:
// config/services.php 'cloudflare' => [ 'account_hash' => env('CLOUDFLARE_IMAGES_ACCOUNT_HASH'), 'account_id' => env('CLOUDFLARE_IMAGES_ACCOUNT_ID'), 'api_token' => env('CLOUDFLARE_IMAGES_API_TOKEN'), 'signing_key' => env('CLOUDFLARE_IMAGES_SIGNING_KEY'), ],
Then publish and run the migrations with:
php artisan vendor:publish --tag="glint-migrations"
php artisan migrate
Usage
Client upload
Since there's so many ways to handle authentication, authorization, and response formatting this is left to the user to implement. But an example controller might look something like this:
public function store(Request $request) { Gate::authorize('uploadImages', [ $request->user() ]); $data = $request->validate([ 'type_id' => 'required|uuid|exists:image_types,id', 'model_id' => 'required|uuid', 'model_type' => 'required', ]); $modelName = Relation::getMorphedModel($data['model_type']) ?? $data['model_type']; $model = (new $modelName)->findOrFail($data['model_id']); throw_unless($model instanceof HasImagesInterface, \Exception::class, 'Model does not implement HasImagesInterface' ); $type = ImageType::findOrFail($data['type_id']); $image = $model->addImageFromDraft($type); return response()->json($image); }
Credits
License
The MIT License (MIT). Please see License File for more information.