How does the AI SDK handle image generation in Laravel 13?

In Laravel 13, the AI SDK uses the LaravelAiImage class to handle image generation, providing a unified, fluent API that abstracts away provider-specific details. This allows you to generate, transform, and store images using a single clean syntax, regardless of whether you are using OpenAI, Gemini, or xAI.

Basic Image Generation

You can generate an image by specifying a text prompt and chaining configuration methods. Once generated, the image can be stored directly using Laravel is filesystem integration.

use LaravelAiImage;

$image = Image::of( A donut sitting on the kitchen counter )
   ->landscape()
   ->quality( high )
   ->generate();

// Store the image on your default disk
$path = $image->store();

You can also use storePublicly(), storeAs(), or storePubliclyAs() to specify the disk or filename .

Key Configuration Options

The SDK provides several chainable methods to control the output:

Aspect Ratio: Use ->square(), ->portrait(), or ->landscape() .
Quality: Use ->quality( high ), ->quality( medium ), or ->quality( low ) .
Timeout: Set the HTTP timeout in seconds using ->timeout(120) .

Transforming Images (Image to Image)

You can use reference images to transform existing photos. The SDK supports attaching images from a URL, a local path, or a storage disk .

use LaravelAiImage;
use LaravelAiFilesImage as ImageFile;

$image = Image::of( Update this photo to be in the style of an impressionist painting. )
   ->attachments([
       ImageFile::fromStorage( photo.jpg ),
       // ImageFile::fromUrl( https://example.com/photo.jpg ),
       // ImageFile::fromPath( /home/laravel/photo.jpg ),
   ])
   ->landscape()
   ->generate();

Queueing Generation

Since image generation can be time-consuming, you can queue the task using the ->queue() method to prevent blocking the HTTP request. It accepts a callback to handle the result once completed .

use LaravelAiImage;
use LaravelAiResponsesImageResponse;

Image::of( A donut sitting on the kitchen counter )
   ->portrait()
   ->queue()
   ->then(function (ImageResponse $image) {
       $path = $image->store();
       // Handle the stored image...
   });

6 Views