Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/phpaisdk/google/llms.txt

Use this file to discover all available pages before exploring further.

Gemini’s multimodal models can produce images directly from a text prompt. Image generation in aisdk/google is a separate code path from text generation: you use Google::image() to create an image model instance (not Google::model()), and the SDK sends the request to the native Gemini endpoint /models/{modelId}:generateContent rather than the unified /interactions endpoint used for text. The response is parsed for inlineData parts containing base64-encoded image bytes.

Supported Image Models

The following models are registered in the provider catalog and support the ImageGeneration capability:
Model IDStatus
gemini-3.1-flash-lite-imageStable
gemini-3.1-flash-imageStable
gemini-3-pro-imageStable
gemini-2.5-flash-imageLegacy
Prefer gemini-3.1-flash-image for a balance of quality and cost. Use gemini-3-pro-image when image fidelity is the top priority. gemini-3.1-flash-lite-image is the fastest and most cost-effective option for high-volume, lower-resolution use cases.

Basic Example

Create an image model with Google::image(), attach it to Generate::image(), add a prompt, and call ->run(). The returned ImageResponse exposes a single output object you can save directly to disk.
use AiSdk\Generate;
use AiSdk\Google;

Google::create(['apiKey' => env('GOOGLE_GENERATIVE_AI_API_KEY')]);

$result = Generate::image()
    ->model(Google::image('gemini-3.1-flash-image'))
    ->prompt('A clean app icon for a PHP AI SDK — minimalist, dark background, neon-green snake.')
    ->aspectRatio('1:1')
    ->run();

$result->output->save(__DIR__ . '/icon.png');

Aspect Ratio

Control the shape of the generated image with ->aspectRatio(). The value is forwarded verbatim as generation_config.image_config.aspect_ratio in the Gemini request body.
// Square — suitable for icons and avatars
->aspectRatio('1:1')

// Landscape — suitable for hero images and banners
->aspectRatio('16:9')

// Portrait — suitable for mobile screens and stories
->aspectRatio('9:16')

// Classic photo landscape
->aspectRatio('4:3')

// Classic photo portrait
->aspectRatio('3:4')
A full example with a landscape aspect ratio:
$result = Generate::image()
    ->model(Google::image('gemini-3.1-flash-image'))
    ->prompt('A panoramic view of a futuristic PHP conference hall.')
    ->aspectRatio('16:9')
    ->run();

$result->output->save(__DIR__ . '/banner.png');

Image Size

Use ->size() to specify the output resolution as a {width}x{height} string. The SDK maps the longest edge to one of Gemini’s named size tiers:
Longest edgeGemini image_size value
≤ 1024 px1K
> 1024 px2K
// Longest edge is 1024 — maps to '1K'
->size('1024x1024')

// Longest edge is 2048 — maps to '2K'
->size('2048x2048')

// Asymmetric — longest edge (1920) maps to '2K'
->size('1920x1080')
The mapping is performed by GoogleImageRequestBuilder::imageSize():
private static function imageSize(string $size): string
{
    [$width, $height] = array_map('intval', explode('x', $size, 2));
    $longest = max($width, $height);

    return $longest > 1024 ? '2K' : '1K';
}

Accessing the Result

->run() returns an ImageResponse. The primary image is available through ->output:
$result = Generate::image()
    ->model(Google::image('gemini-3.1-flash-image'))
    ->prompt('A glowing PHP elephant logo on a dark background.')
    ->run();

// Raw base64-encoded PNG bytes
echo $result->output->base64;

// MIME type — typically 'image/png'
echo $result->output->mimeType;

// Save to a file path on disk
$result->output->save('/var/www/html/public/generated.png');

Token usage

Even though the output is an image, Gemini still reports token counts for the prompt and the generated content:
echo $result->usage->inputTokens;  // Tokens consumed by your prompt
echo $result->usage->outputTokens; // Tokens attributed to the image output

Advanced Parameters via Raw Passthrough

For Gemini request fields that have no portable SDK equivalent, use ->providerOptions('google', ['raw' => [...]]). The raw array is merged into the request body with array_replace_recursive, so it can extend or override any field including generation_config:
$result = Generate::image()
    ->model(Google::image('gemini-3-pro-image'))
    ->prompt('A watercolour illustration of a mountain lake at sunrise.')
    ->aspectRatio('4:3')
    ->providerOptions('google', [
        'raw' => [
            'generation_config' => [
                'image_config' => [
                    'output_options' => [
                        'mime_type' => 'image/jpeg',
                        'compression_quality' => 85,
                    ],
                ],
            ],
        ],
    ])
    ->run();

$result->output->save(__DIR__ . '/lake.jpg');
Two portable options are not supported by the Google image provider and will throw an InvalidArgumentException if used:
  • ->count(n) — Multiple images per request are not currently exposed by Gemini’s API. You must issue separate requests to generate more than one image.
  • ->seed(n) — Deterministic seeding is not supported by the Gemini image generation API.
These restrictions are enforced in GoogleImageRequestBuilder::build() before the HTTP request is dispatched.

Build docs developers (and LLMs) love