Cloudflare Images
Provides access to Cloudflare Images API for Laravel projects
Table of contents
Installation
To get the latest version of Laravel CloudflareImages
, simply require the project using Composer:
$ composer require dedmytro/laravel-cloudflare-images
Or manually update require
block of composer.json
and run composer update
.
{
"require": {
"dedmytro/laravel-cloudflare-images": "^0.1"
}
}
Configuration
Add two environment variables to your .env file:
CLOUDFLARE_IMAGES_ACCOUNT='your-account-id'
CLOUDFLARE_IMAGES_KEY='your-api-key'
or publish config and set up vars there
return [
'account'=> env('CLOUDFLARE_IMAGES_ACCOUNT'),
'key'=> env('CLOUDFLARE_IMAGES_KEY'),
];
Using
Upload
Call upload()
method and pass file as local file path or UploadedFile
instance. As a result of upload you'll get DetailsResponse
instance with uploaded image details, so you can save it locally.
use DeDmytro\CloudflareImages\Facades\CloudflareImages;
use DeDmytro\CloudflareImages\Http\Responses\DetailsResponse;
use DeDmytro\CloudflareImages\Http\Entities\Image;
/* @var $file \Illuminate\Http\UploadedFile|string */
/* @var $response DetailsResponse*/
$response = CloudflareImages::api()->upload($file)
/* @var $image Image*/
$image = $response->result
$image->id;
$image->filename;
$image->variants->thumbnail;
$image->variants->original;
List
To list existing images you should use list()
method which also has pagination and accept $page
and $perPage
arguments.
use DeDmytro\CloudflareImages\Facades\CloudflareImages;
/* @var $response ListResponse*/
$response = CloudflareImages::api()->list()
//OR
$response = CloudflareImages::api()->list($page, $perPage)
foreach($response->result as $image){
$image->id;
$image->filename;
$image->variants->thumbnail;
$image->variants->original;
}
Details
To get existing image details you should use get($id)
method where $id
is image identifier you received when you list or upload the image.
use DeDmytro\CloudflareImages\Facades\CloudflareImages;
$response = CloudflareImages::api()->get($id)
$image = $response->result;
$image->id;
$image->filename;
$image->variants->thumbnail;
$image->variants->original;
Delete
To delete existing image you should use delete($id)
method where $id
is image identifier you received when you list or upload the image.
use DeDmytro\CloudflareImages\Facades\CloudflareImages;
$response = CloudflareImages::api()->delete($id)
$response->success