Prism provides a simple API for generating images using AI models from supported providers like OpenAI’s DALL-E. The Prism::image() method allows you to create images from text descriptions.
Check the provider documentation to see which providers support image generation and what models are available.
Specify which provider and model to use for image generation:
use Prism\Prism\Enums\Provider;$response = Prism::image() ->using(Provider::OpenAI, 'dall-e-3') ->withPrompt('A futuristic city with flying cars') ->generate();
Provide system-level instructions to guide image generation:
$response = Prism::image() ->using('openai', 'dall-e-3') ->withSystemPrompt('Generate images in a watercolor painting style') ->withPrompt('A forest scene with wildlife') ->generate();
Some providers support using reference images for image editing or variation:
use Prism\Prism\ValueObjects\Media\Image;$response = Prism::image() ->using('openai', 'dall-e-2') ->withPrompt('Add a sunset to this landscape', [ Image::fromLocalPath('/path/to/original.png') ]) ->generate();
Reference images are filtered automatically - only Image media types are included in the request.
Track API usage for your image generation requests:
$response = Prism::image() ->using('openai', 'dall-e-3') ->withPrompt('Digital art of a robot') ->generate();echo "Images generated: {$response->usage->imagesGenerated}";
Use detailed, descriptive prompts for better results:
// Good$response = Prism::image() ->withPrompt('A photorealistic portrait of a wise elderly man with a white beard, wearing a blue robe, standing in a medieval library filled with ancient books, warm lighting from candles') ->generate();// Less effective$response = Prism::image() ->withPrompt('Old man in library') ->generate();
Always verify images were generated:
$response = Prism::image() ->using('openai', 'dall-e-3') ->withPrompt('Mountain vista') ->generate();if (!$response->hasImages()) { // Handle no images case throw new Exception('No images were generated');}$image = $response->firstImage();
Research provider-specific options for optimal results: