PHP code example of ghijk / cloudflare-super-images

1. Go to this page and download the library: Download ghijk/cloudflare-super-images library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ghijk / cloudflare-super-images example snippets


use Ghijk\CloudflareSuperImages\Facades\CloudflareImages;
use Ghijk\CloudflareSuperImages\Enums\ImageFit;
use Ghijk\CloudflareSuperImages\Enums\ImageFormat;
use Ghijk\CloudflareSuperImages\Enums\ImageGravity;
use Ghijk\CloudflareSuperImages\Enums\ImagePreset;

// Responsive product card
CloudflareImages::url($imageId)
    ->width(600)
    ->fit(ImageFit::Cover)
    ->format(ImageFormat::Auto)
    ->toString();

// Background removal (format defaults to PNG to preserve transparency)
CloudflareImages::url($imageId)
    ->removeBackground()
    ->width(800)
    ->toString();

// Face-aware crop
CloudflareImages::url($imageId)
    ->preset(ImagePreset::Thumbnail)
    ->gravity(ImageGravity::Face)
    ->zoom(0.5)
    ->toString();

// A named variant configured in the dashboard
CloudflareImages::variant($imageId, 'hero');

use Ghijk\CloudflareSuperImages\Facades\CloudflareImages;

public function share(Request $request): array
{
    return [
        ...parent::share($request),
        'cloudflareImages' => [
            'accountHash' => CloudflareImages::accountHash(),
        ],
    ];
}

use Ghijk\CloudflareSuperImages\Facades\CloudflareImages;

$upload = CloudflareImages::requestDirectUpload([
    'product_id' => $product->id,
]);

// $upload->id, $upload->uploadUrl, $upload->accountHash
return response()->json($upload->toArray());

// Polls with exponential backoff, throws CouldNotVerifyUpload if it never readies
CloudflareImages::assertUploaded($upload->id);

$details = CloudflareImages::getImageDetails($imageId);

if ($details?->isReady()) {
    // ...
}

// Best-effort cleanup, returns a boolean rather than throwing
CloudflareImages::delete($imageId);
bash
php artisan vendor:publish --tag="cloudflare-super-images-js"