PHP code example of shareaichat / fal-ai-php-image-generator

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

    

shareaichat / fal-ai-php-image-generator example snippets




use ShareAIChat\FalAi\FalAiClient;
use ShareAIChat\FalAi\Models;

$client = new FalAiClient(getenv('FAL_KEY'));

$images = $client->textToImage(
    'a cyberpunk samurai cat in a neon-lit Tokyo alley, cinematic lighting, ultra detailed'
);

echo $images[0]->url; // https://fal.media/files/...

$images = $client->textToImage('an ancient temple hidden in a misty forest at dawn', [
    'model'      => Models::FLUX_SCHNELL,
    'image_size' => FalAiClient::SIZE_LANDSCAPE_16_9,
]);

echo $images[0]->url;

$images = $client->textToImage(
    'a magical library floating above the clouds, golden hour, fantasy concept art',
    [
        'model'                => Models::FLUX_DEV,
        'image_size'           => FalAiClient::SIZE_PORTRAIT_4_3,
        'num_inference_steps'  => 28,
        'guidance_scale'       => 3.5,
        'negative_prompt'      => 'blurry, low quality, watermark, text',
    ]
);

$images = $client->textToImage('a phoenix rising from volcanic ash, epic fantasy art', [
    'model'      => Models::FLUX_SCHNELL,
    'num_images' => 4,
]);

foreach ($images as $image) {
    echo "{$image->width}x{$image->height} — {$image->url}\n";
}

$images = $client->imageToImage(
    'Turn this portrait into a cyberpunk style with neon goggles',
    'https://example.com/portrait.jpg',
    ['model' => Models::FLUX_KONTEXT]
);

echo $images[0]->url;

$images = $client->imageToImage(
    'Combine the style of image 1 with the composition of image 2',
    [
        'https://example.com/ref1.jpg',
        'https://example.com/ref2.jpg',
    ],
    ['model' => Models::FLUX_KONTEXT_MULTI]
);

$prompts = [
    ['cyberpunk', 'a lone samurai walking through a rainy neon street'],
    ['fantasy',    'a dragon soaring over snowy mountain peaks at dawn'],
    ['anime',      'a cozy cafe interior with cherry blossoms in spring'],
];

foreach ($prompts as [$style, $scene]) {
    try {
        $images = $client->textToImage("$style: $scene, ultra detailed, 8k");
        echo "$style -> {$images[0]->url}\n";
    } catch (\ShareAIChat\FalAi\FalAiException $e) {
        error_log("Generation failed: {$e->getMessage()}");
    }
}

$images = $client->textToImage('a bioluminescent jellyfish in deep space');

$path = $images[0]->save(__DIR__ . '/output');
echo "Saved to: {$path}\n";

$data = $client->request('fal-ai/your-model-of-choice', [
    'prompt' => 'your prompt',
    // ...raw fal.ai parameters
]);

use ShareAIChat\FalAi\FalAiException;

try {
    $images = $client->textToImage($prompt);
} catch (FalAiException $e) {
    echo $e->getMessage();          // e.g. "body.num_inference_steps: Input should be less than or equal to 12"
    echo $e->getHttpStatus();       // e.g. 422
    print_r($e->getResponseBody()); // raw decoded response for logging
}