Senne
Senne
FFilament
Created by Senne on 4/2/2024 in #❓┊help
Fill FileUpload with generated images
Im trying to fill a FileUpload field with an image I generate using OpenAI's Dall e I added the required FileUpload field to my schema and I added the action to generate the image. This action uses a form to ask the user for a prompt:
FileUpload::make('ai_image')
->image(),
Actions::make([
Action::make('aiImage')
->icon('heroicon-o-camera')
->form([
TextInput::make('prompt')
->label(__('input.prompt')),
])
->action(function (array $arguments, Set $set, array $data) {
$prompt = $data['prompt'];

OpenAIController::getImage($prompt);
})
]),
FileUpload::make('ai_image')
->image(),
Actions::make([
Action::make('aiImage')
->icon('heroicon-o-camera')
->form([
TextInput::make('prompt')
->label(__('input.prompt')),
])
->action(function (array $arguments, Set $set, array $data) {
$prompt = $data['prompt'];

OpenAIController::getImage($prompt);
})
]),
I already prepared the controller for generating the image and downloading it into storage:
public static function getImage(String $prompt)
{
$apiKey = env('OPENAI_KEY');
$client = OpenAI::client($apiKey);

$aiResponse = $client->images()->create([
'model' => 'dall-e-3',
'prompt' => $prompt,
'n' => 1,
'size' => '1024x1024',
'response_format' => 'url',
]);

$client = new Client();
$url = $aiResponse->data[0]->url;
$filename = uuid_create().'.jpg';

$imageResponse = $client->request('GET', $url);

Storage::disk('public')->put($filename, $imageResponse->getBody()->getContents());
}
public static function getImage(String $prompt)
{
$apiKey = env('OPENAI_KEY');
$client = OpenAI::client($apiKey);

$aiResponse = $client->images()->create([
'model' => 'dall-e-3',
'prompt' => $prompt,
'n' => 1,
'size' => '1024x1024',
'response_format' => 'url',
]);

$client = new Client();
$url = $aiResponse->data[0]->url;
$filename = uuid_create().'.jpg';

$imageResponse = $client->request('GET', $url);

Storage::disk('public')->put($filename, $imageResponse->getBody()->getContents());
}
I already tried to set the ai_image field using the Set method, but this didn't work. Is this possible in Filament? The ultimate goal is to generate the image and show it in the FileUpload preview, so the user can see it before saving.
5 replies