Skip to content

Image Sources ​

The media picker opens on the site's own library. An image source adds a tab beside it, so an editor can search somewhere else (a stock photo service, your asset manager, an image generator) and use a result without leaving the editor. With no sources registered the picker looks exactly as before and no tab bar is shown.

Register a source ​

php
use FilamentCraft\FilamentCraftPlugin;

FilamentCraftPlugin::make()->imageSources([
    App\Media\StockPhotoSource::class,
]);

Pass an instance or a class name; class names are resolved from the container, so a source can take a client or an API key in its constructor.

The contract ​

A source implements FilamentCraft\Media\Contracts\ImageSource:

MethodPurpose
key()A short, unique key such as stock. Stored on every image picked from the source.
label()The tab title.
icon()A Heroicon name for the tab.
placeholder()The search field placeholder.
available(Site $site)Return false to hide the tab, for example when the API key is missing.
search(Site $site, string $query, int $page = 1)Return an ImageResultPage of ImageResult thumbnails and whether more pages exist.
pick(Site $site, string $id)Return a PickedImage for the chosen result.

Either search() or pick() may throw FilamentCraft\Media\Exceptions\ImageSourceUnavailable. Its message is shown to the editor, so write it for them ("The service is over its hourly limit."). Any other exception is reported and the editor sees a generic "This source isn't available right now" message.

Each ImageResult has an id, a thumbUrl, optional alt text, size and an optional ImageCredit. A credit with an author is shown under the thumbnail as "Photo by Author on Source", linking to the author and the source. Only http and https links are kept.

What a pick returns ​

PickedImage has three kinds, so a source decides where the file lives:

KindUse it when
PickedImage::external($url, $credit, $alt)The image stays on the provider's servers. The URL must be https://. The library keeps a row that points at it, and the credit stays on the row.
PickedImage::stored($path, $alt, $name)Your code already put the file on the site's uploads disk. The library registers it.
PickedImage::contents($binary, $filename, $alt)You have the bytes (a download, a generated image). FilamentCraft validates and stores them as an ordinary upload.

Picking the same result twice reuses the library row instead of adding another.

Stock photos (Unsplash) ​

FilamentCraft ships one ready-made source, a free stock photo search backed by the Unsplash API. It appears as a Stock photos tab once you give it an access key.

php
use FilamentCraft\FilamentCraftPlugin;
use FilamentCraft\Media\Sources\UnsplashImageSource;

FilamentCraftPlugin::make()->imageSources([UnsplashImageSource::class]);
dotenv
FILAMENTCRAFT_UNSPLASH_ACCESS_KEY=your-access-key
FILAMENTCRAFT_UNSPLASH_APP_NAME=your-app-name
  1. Create an app at the Unsplash developers site and copy its Access Key (not the secret key).
  2. Set FILAMENTCRAFT_UNSPLASH_ACCESS_KEY. Without it the tab stays hidden. The key is only used on your server and is never rendered into the editor.
  3. Set FILAMENTCRAFT_UNSPLASH_APP_NAME to your own application's name. It becomes the utm_source on every credit link, as Unsplash's guidelines require.

A new Unsplash app runs in demo mode with 50 requests an hour. To go live, apply for production approval in the Unsplash dashboard. Unsplash asks for screenshots showing the attribution, so keep the credit line on (it is on by default) and take your screenshots of the picker and a published page.

Behind the scenes:

  • Hotlinking. A picked photo is not downloaded. The image is served from Unsplash's CDN, as their API terms ask, and the library keeps a row that points at it.
  • Attribution. Each photo carries "Photo by Author on Unsplash", with links to the photographer and to Unsplash carrying your utm_source. The credit is stored on the library row and shown under the thumbnail.
  • Download tracking. Picking a photo queues a small job that tells Unsplash the photo was used, as their guidelines require. Run a queue worker, or the ping waits until one runs.
  • Caching. Identical searches (same words, same page) are cached for 15 minutes to save your hourly quota.

Credit line on the page ​

Pages that use a picked photo with a credit show a small "Photo by Author on Unsplash" line. Turn it off per panel or per site with showImageCredits(), but keep it on while you are asking Unsplash for production approval:

php
FilamentCraftPlugin::make()->showImageCredits(false);
FilamentCraftPlugin::make()->showImageCredits(fn (Site $site): bool => $site->slug !== 'internal');

Pages are rendered outside any panel, so the setting comes from the first panel that registers the plugin. A host with no panel plugin sets 'image_credits' => false in config/filamentcraft.php instead.

Use it for mood images and backgrounds, never for product photos: a stock image of a loaf says nothing about the loaf you sell.

Add an AI image generator ​

An image generator is a source whose pick() generates the image. The host pays for it with its own credits; FilamentCraft only calls your code.

php
use FilamentCraft\Media\Contracts\ImageSource;
use FilamentCraft\Media\Sources\ImageResult;
use FilamentCraft\Media\Sources\ImageResultPage;
use FilamentCraft\Media\Sources\PickedImage;
use FilamentCraft\Models\Site;

final class MoodImageSource implements ImageSource
{
    public function __construct(private readonly ImageGenerator $generator) {}

    public function key(): string
    {
        return 'mood';
    }

    public function label(): string
    {
        return 'Generate';
    }

    public function icon(): string
    {
        return 'heroicon-o-sparkles';
    }

    public function placeholder(): string
    {
        return 'Describe a mood image';
    }

    public function available(Site $site): bool
    {
        return $this->generator->hasCredits($site);
    }

    public function search(Site $site, string $query, int $page = 1): ImageResultPage
    {
        return new ImageResultPage([
            new ImageResult(id: base64_encode($query), thumbUrl: $this->generator->previewUrl($query)),
        ]);
    }

    public function pick(Site $site, string $id): PickedImage
    {
        $image = $this->generator->generate($site, base64_decode($id));

        return PickedImage::contents($image->binary, 'mood.png', alt: null);
    }
}

Keep it to mood and background images. Generated product photos or faces can mislead shoppers, and a generated image has no author to credit.

Cost ​

Nothing is requested while an editor types. A search runs when they submit the form (Enter or the Search button), and Load more fetches one more page. A source is only called for the site that opened the picker, and only if its available() returns true for that site.