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.

GoogleImageModel is the concrete class that handles image generation using Gemini’s native content-generation endpoint. It implements ImageModelInterface from the PHP AI SDK core and is the object returned by Google::image(). Unlike GoogleTextModel, image generation calls are routed to the model-specific /models/{modelId}:generateContent endpoint rather than the unified /interactions endpoint, and the response is parsed specifically for inline image data.

Namespace

AiSdk\Google\Models\GoogleImageModel

Constructor

public function __construct(
    string $modelId,
    GoogleOptions $options,
    ?ModelRegistry $registry = null,
)
modelId
string
required
The Gemini image model identifier, for example 'gemini-2.0-flash-preview-image-generation'. This value is interpolated into the endpoint URL as /models/{modelId}:generateContent.
options
GoogleOptions
required
A configured GoogleOptions instance carrying the API key, base URL, and SDK-level settings.
registry
ModelRegistry|null
Optional ModelRegistry for overriding built-in capability definitions. Consulted before the ModelCatalog in both capabilities() and capability().

Methods

provider(): string

Returns the provider name string.
public function provider(): string
Returns 'google'.

modelId(): string

Returns the model identifier passed to the constructor.
public function modelId(): string

capabilities(): array

Resolves the capability list for this model. Resolution order:
  1. If a ModelRegistry is set and has a definition for this provider + model, use it.
  2. Otherwise, load the built-in ModelCatalog from resources/models.json.
/** @return array<int, Capability> */
public function capabilities(): array

capability(Capability $capability): CapabilitySupport

Returns the support status for a single capability using the same registry → catalog resolution order as capabilities().
public function capability(Capability $capability): CapabilitySupport
capability
Capability
required
The Capability enum case to query.

generate(ImageRequest $request): ImageResponse

Performs an image generation request against the Gemini native content endpoint.
public function generate(ImageRequest $request): ImageResponse
request
ImageRequest
required
An ImageRequest carrying the text prompt, aspect ratio, size, and any provider-specific options under the 'google' key. Note the constraints documented in the Limitations section below.
Returns an ImageResponse containing the generated images as ImageData objects, token usage, the raw response array, and providerMetadata['google']. Request / response flow:
  1. GoogleImageRequestBuilder::build('google', $request) validates the request and converts it into a Gemini generateContent body.
  2. The body is POST-ed to {baseUrl}/models/{modelId}:generateContent with auth headers.
  3. The raw JSON response is passed to GoogleImageResponseParser::parse($payload, 'google').
  4. The parser iterates candidates[].content.parts[] and extracts any inlineData entries as ImageData objects.
  5. An ImageResponse is returned.
Example:
use AiSdk\Google\Google;
use AiSdk\Requests\ImageRequest;

$model = Google::image('gemini-2.0-flash-preview-image-generation');

$response = $model->generate(
    ImageRequest::make('A photorealistic mountain landscape at golden hour')
        ->withAspectRatio('16:9')
);

// Save the first generated image
$response->output->save('/tmp/landscape.png');

Limitations

count() must be 1. Google’s unified image API does not expose a portable multi-image option through the SDK. Passing any value other than 1 via ImageRequest::withCount() throws an InvalidArgumentException. If you need multiple images, run separate generate() calls. You may still pass provider-specific multi-image options via providerOptions('google')['raw'] when Google exposes that capability.seed() is not supported. Calling ImageRequest::withSeed() with any non-null value throws an InvalidArgumentException. Seed-based deterministic generation is not available through this provider.

ImageResponse Structure

generate() returns an ImageResponse object. The primary contents are:
images
array<ImageData>
An ordered list of ImageData objects, one per generated image. For Google, this list will contain exactly one item unless multiple candidates are returned by the API.
images[0]->base64
string
The raw base64-encoded binary content of the image. This is the data field from the Gemini inlineData response part — it does not include a data URI prefix.
images[0]->mimeType
string
The MIME type of the image, for example 'image/png' or 'image/jpeg'. Sourced from the mimeType or mime_type field inside the Gemini inlineData part. Defaults to 'image/png' if the field is absent.
output
ImageData
A convenience shortcut for images[0]. Provides the same base64 and mimeType properties plus a .save(string $path) helper method that writes the decoded image bytes directly to a file path.
usage
Usage
Token usage for the request, populated from usageMetadata / usage_metadata in the response. Includes inputTokens and outputTokens.
rawResponse
array
The full, unmodified JSON-decoded response array from the Gemini API.
providerMetadata
array
An array keyed by provider name ('google'). The nested array contains: model, promptFeedback / prompt_feedback, usageMetadata / usage_metadata — whatever fields are present in the raw response.
Saving an image:
$response = $model->generate(ImageRequest::make('A cozy library interior'));

// Using the output shortcut
$response->output->save('/var/www/public/images/library.png');

// Or accessing image data directly
$base64  = $response->images[0]->base64;
$mime    = $response->images[0]->mimeType;
file_put_contents('/tmp/library.png', base64_decode($base64));

Build docs developers (and LLMs) love