PHP code example of ianrodrigues / laravel-fal-ai

1. Go to this page and download the library: Download ianrodrigues/laravel-fal-ai 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/ */

    

ianrodrigues / laravel-fal-ai example snippets


'providers' => [

    // ...

    'fal' => [
        'driver' => 'fal',
        'key' => env('FAL_KEY'),
    ],

],

use Laravel\Ai\Files\Image as ImageFile;
use Laravel\Ai\Image;

$response = Image::of('change the sky to a sunset')
    ->attachments([ImageFile::fromUrl('https://example.com/photo.jpg')])
    ->square()
    ->quality('high')
    ->generate(provider: 'fal', model: 'nano-banana-2/edit');

file_put_contents('out.png', base64_decode($response->images->first()->image));

use IanRodrigues\FalAi\Facades\Fal;
use Laravel\Ai\Files\Image as ImageFile;

$response = Fal::image('change the sky to a sunset')
    ->attachments([ImageFile::fromUrl('https://example.com/photo.jpg')])
    ->seed(42)
    ->numImages(2)
    ->aspectRatio('16:9')
    ->safetyTolerance(4)
    ->outputFormat('webp')
    ->generate(model: 'nano-banana-2/edit');

use Laravel\Ai\Files\Image as ImageFile;
use Laravel\Ai\Image;

$response = Image::of('remove background')
    ->attachments([ImageFile::fromPath('/home/laravel/character.png')])
    ->generate(provider: 'fal', model: 'birefnet');

file_put_contents('cutout.png', base64_decode($response->images->first()->image));

namespace App\AiModels;

use IanRodrigues\FalAi\Image\ModelHandler;
use Illuminate\Support\Collection;
use Laravel\Ai\Responses\Data\GeneratedImage;
use Laravel\Ai\Responses\Data\Meta;
use Laravel\Ai\Responses\Data\Usage;
use Laravel\Ai\Responses\ImageResponse;

class FluxPro implements ModelHandler
{
    public function supports(string $model): bool
    {
        return $model === 'flux-pro' || $model === 'fal-ai/flux-pro';
    }

    public function endpoint(string $model): string
    {
        return 'fal-ai/flux-pro';
    }

    public function buildPayload(string $prompt, array $imageUrls, ?string $size, ?string $quality, array $requestOptions, array $providerConfig): array
    {
        return array_filter([
            'prompt' => $prompt,
            'image_size' => $size,
            ...$requestOptions,
        ]);
    }

    public function parseResponse(array $json, string $model): ImageResponse
    {
        $images = Collection::make($json['images'] ?? [])
            ->map(fn (array $image) => new GeneratedImage(
                $image['_b64'] ?? $image['url'] ?? '',
                $image['content_type'] ?? 'image/png',
            ));

        return new ImageResponse($images, new Usage, new Meta('fal', $model));
    }

    public function 

'image' => [
    'models' => [
        \IanRodrigues\FalAi\Image\NanoBananaTwoEdit::class,
        \App\AiModels\FluxPro::class,
    ],
],

use Laravel\Ai\Events\GeneratingImage;
use Laravel\Ai\Events\ImageGenerated;

Event::fake([GeneratingImage::class, ImageGenerated::class]);

// ... perform a generation ...

Event::assertDispatched(ImageGenerated::class);
bash
php artisan vendor:publish --tag=fal-ai-config