Skip to content

AI Alt Text ​

Media Library Pro can write alt text for you. It adds a Write it for me action to the inspector and can describe new images on the queue. Generation is off until you pick a generator.

Set up the Anthropic generator ​

Install the SDK:

bash
composer require anthropic-ai/sdk

Add your key to .env:

bash
ANTHROPIC_API_KEY=your-key

Point the config at the generator:

php
'alt_text' => [
    'generator' => \Hoceineel\FilamentMediaLibrary\AltText\AnthropicAltTextGenerator::class,
    'auto_generate' => false,
    'anthropic' => [
        'api_key' => env('ANTHROPIC_API_KEY'),
        'model' => env('MEDIA_LIBRARY_ALT_TEXT_MODEL', 'claude-opus-5-5'),
    ],
],

The generator is available when the SDK is installed and an API key is set. Until then the inspector action stays hidden. Change the model with MEDIA_LIBRARY_ALT_TEXT_MODEL.

In the inspector ​

With a generator available, the alt text field in the inspector shows Write it for me. It appears for images only.

Press it and the generated text fills the field. Nothing is saved until you save the inspector, so you can edit the text first. If the provider returns nothing, you see a warning and the field keeps its current text.

The request waits for the provider's answer. Turn the action off for a panel with ->disableFeatures(Feature::AltText).

Automatic alt text ​

php
'auto_generate' => true,

With this on, every new image that has no alt text is described by a queued job. Keep a queue worker running. The job:

  • Skips files that already have alt text, so text you wrote is never replaced.
  • Retries once if it fails.
  • Saves the text without firing model events.
  • Writes in the application locale of the queue worker, since the job does not know which language the uploader used.

Files that arrive through URL import and the import command go through the same path, so they are described too.

How the Anthropic generator works ​

  • It only handles images. Other files return nothing.
  • JPEG, PNG, GIF and WebP files up to 4 MB are sent as they are. Larger files, or other image types, are sent as their preview conversion if one exists. If neither applies, there is no alt text.
  • It asks for one sentence under 125 characters, in the language of the current locale, with no "Image of" opening.
  • Text is trimmed and capped at 250 characters. A refusal from the model returns nothing.

Write your own generator ​

Implement AltTextGenerator. It has two methods:

MethodPurpose
isAvailable(): boolReturn false to hide the inspector action and skip generation.
generate(MediaItem $item, ?string $locale = null): ?stringReturn the alt text, or null for nothing.
php
namespace App\Support;

use Hoceineel\FilamentMediaLibrary\AltText\AltTextGenerator;
use Hoceineel\FilamentMediaLibrary\Models\MediaItem;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;

class CaptionServiceAltTextGenerator implements AltTextGenerator
{
    public function isAvailable(): bool
    {
        return filled(config('services.captioner.url'));
    }

    public function generate(MediaItem $item, ?string $locale = null): ?string
    {
        $media = $item->file();

        if (! $media || ! $item->isImage()) {
            return null;
        }

        $response = Http::withToken(config('services.captioner.key'))
            ->timeout(30)
            ->attach('image', Storage::disk($media->disk)->get($media->getPathRelativeToRoot()), $media->file_name)
            ->post(config('services.captioner.url'), ['language' => $locale ?? app()->getLocale()]);

        return $response->successful() ? $response->json('caption') : null;
    }
}

Register it:

php
'alt_text' => [
    'generator' => App\Support\CaptionServiceAltTextGenerator::class,
    'auto_generate' => true,
],

The container builds your class, so constructor injection works. The default is NullAltTextGenerator, which is never available and always returns null.

Privacy ​

WARNING

To describe an image, the generator sends the image itself to the provider you configure. With the Anthropic generator, that is Anthropic's API. Check the provider's data terms before you turn this on.

  • auto_generate describes every new image, including files uploaded as Only me. The generator does not check visibility. If private images must not leave your server, leave auto_generate off, or write a generator that skips them with $item->isPrivate().
  • With the default NullAltTextGenerator, nothing is sent anywhere.
  • Review generated text before publishing. It describes what the model sees and does not know your context.

Commercial licence. One licence per production project. Terms · Privacy · Refunds