PHP code example of spacecatninja / ai-image-editor

1. Go to this page and download the library: Download spacecatninja/ai-image-editor 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/ */

    

spacecatninja / ai-image-editor example snippets




use craft\helpers\App;

return [
    '*' => [
        'driver' => 'gemini',
        'driverConfig' => [
            'gemini' => [
                'apiKey' => App::env('GEMINI_API_KEY'),
            ],
            'openai' => [
                'apiKey' => App::env('OPENAI_API_KEY'),
            ],
        ],
    ],
];

'presets' => [
    ['label' => 'Remove background', 'prompt' => 'Remove the background, leaving a clean transparent or white backdrop.', 'precise' => true],
    ['label' => 'Enhance', 'prompt' => 'Improve the lighting, sharpness and color balance.', 'precise' => true],
    ['label' => 'Black & white', 'prompt' => 'Convert to a rich black and white photograph.'],
],

'driver' => 'flux',
'analysisDriver' => 'gemini',
'driverConfig' => [
    'flux' => ['apiKey' => App::env('FLUX_API_KEY')],
    'gemini' => [
        'apiKey' => App::env('GEMINI_API_KEY'),
        'analysisModel' => 'gemini-3.5-flash',
    ],
],

use spacecatninja\aiimageeditor\AiImageEditor;
use spacecatninja\aiimageeditor\events\RegisterEditDriversEvent;
use yii\base\Event;

Event::on(
    AiImageEditor::class,
    AiImageEditor::EVENT_REGISTER_EDIT_DRIVERS,
    static function(RegisterEditDriversEvent $event) {
        $event->drivers['my-driver'] = MyEditDriver::class;
    }
);

use spacecatninja\aiimageeditor\events\EditEvent;
use spacecatninja\aiimageeditor\events\SaveEvent;
use spacecatninja\aiimageeditor\services\FinalizeService;
use spacecatninja\aiimageeditor\services\SessionsService;
use yii\base\Event;

// Tweak or veto a prompt before it runs.
Event::on(
    SessionsService::class,
    SessionsService::EVENT_BEFORE_EDIT,
    static function(EditEvent $event) {
        $event->request->prompt .= ' Keep the brand colors intact.';
        // $event->isValid = false; // cancel the turn
    }
);

// Set a field on every AI-saved asset (e.g. alt text).
Event::on(
    FinalizeService::class,
    FinalizeService::EVENT_BEFORE_SAVE,
    static function(SaveEvent $event) {
        $event->asset->setFieldValue('altText', 'Edited with AI');
    }
);
bash
php craft ai-image-editor/test-connection