Installation
Process
Run the following:
composer require thoughtco/statamic-cloudflare-images
php artisan vendor:publish --tag=cfimage-config #this publishes config/cfimage.php
php artisan vendor:publish --tag=cfimage-worker #this publishes workers/cfimage/
Create an R2 bucket and point a disk at it
In the Cloudflare dashboard, go to R2 → Create bucket and give it a name (e.g.
my-app-assets).Create an API token scoped to R2: R2 → Manage API tokens → Create API token, with read/write access to that bucket. This gives you an Access Key ID and Secret Access Key — save both, the secret is only shown once.
Your Cloudflare Account ID is shown in the right-hand sidebar of almost every page in the dashboard — you'll need it for the R2 endpoint below and again later for the Worker.
Add an R2 disk in
config/filesystems.php, using Laravel's S3 driver (R2 is S3-compatible):
'r2' => [
'driver' => 's3',
'key' => env('R2_ACCESS_KEY_ID'),
'secret' => env('R2_SECRET_ACCESS_KEY'),
'region' => 'auto',
'bucket' => env('R2_BUCKET'),
'endpoint' => env('R2_ENDPOINT'), // https://<account_id>.r2.cloudflarestorage.com
'use_path_style_endpoint' => true,
],
Point a Statamic asset container (or your Spatie Media disk, etc.) at this
r2disk. This is the diskconfig('cfimage.disks')will reference in step 2 — the addon reads existing assets from it, it doesn't migrate anything for you.
Configure the package
Open config/cfimage.php and set, at minimum:
disks— every disk{{ cfimage }}may serve from, mapped to an arbitrary R2 binding name the Worker will use, e.g.['assets' => 'ASSETS']. Each key must be a disk that exists inconfig/filesystems.phpand has abucketvalue set (ther2disk from step 1, for example).presets— named shortcuts in the same shape as Statamic's Glide presets, e.g.'thumbnail' => ['w' => 300, 'h' => 300, 'q' => 80, 'fm' => 'webp']. You can literally copy/paste them from the assets config file.
See the comments in that file for the rest of the options.
Set up the worker
From your app root, the published Worker source lives in workers/cfimage/.
Install dependencies (this installs
wranglerlocally — every command below runs it vianpx, no global install needed):
cd workers/cfimage
npm install
Authenticate wrangler so it's allowed to deploy to your account:
npx wrangler login
This opens a browser to authorize against your Cloudflare account. For CI or other non-interactive environments, set a
CLOUDFLARE_API_TOKENenvironment variable instead (dashboard → My Profile → API Tokens → Create Token, the "Edit Cloudflare Workers" template covers it) — wrangler picks this up automatically and skips the browser flow.Point your chosen hostname at Cloudflare. Decide on the hostname
{{ cfimage }}URLs will use (e.g.images.example.com) and add a DNS record for it in the Cloudflare dashboard, proxied(orange cloud on). The record's target doesn't matter — the Worker's route intercepts requests to this hostname before they'd reach any actual origin. Set this hostname asCFIMAGE_URLin your app's.env(https://images.example.com).Fill in the Worker deploy settings in
config/cfimage.php'sworkerarray:zone_name— your domain as it appears in Cloudflare (e.g.example.com), set viaCFIMAGE_ZONE_NAME.account_id— only needed if the account authenticated in step 2 has access to more than one Cloudflare account; otherwise wrangler infers it and this can stay blank. It's the Account ID from step 1.3, set viaCFIMAGE_ACCOUNT_ID.bucket_namevalues are generated automatically fromconfig('cfimage.disks')andconfig/filesystems.php— nothing to fill in by hand here, but the R2 bucket(s) themselves must already exist (step 1).
Generate a signing secret — any random string works, e.g.:
openssl rand -hex 32
Set this value in both places, exactly matching, or every URL will 404(run from workers/cfimage/) and as CFIMAGE_SIGNING_KEY in your app's .env):
npx wrangler secret put CFIMAGE_SIGNING_KEY
Generate and deploy the Worker.
From your app root:
php artisan cfimage:publish --deploy
This regenerates wrangler.toml and the preset table from config/cfimage.php, then runs wrangler deploy. (Run without --deploy to just regenerate the files without deploying.)