|
| 1 | +# Image Generation |
| 2 | + |
| 3 | +Generate stunning images from text prompts using AI-powered models. Prism provides a clean, consistent API for image generation across different providers, starting with comprehensive OpenAI support. |
| 4 | + |
| 5 | +## Getting Started |
| 6 | + |
| 7 | +Creating images with Prism is as simple as describing what you want: |
| 8 | + |
| 9 | +```php |
| 10 | +use Prism\Prism\Prism; |
| 11 | + |
| 12 | +$response = Prism::image() |
| 13 | + ->using('openai', 'dall-e-3') |
| 14 | + ->withPrompt('A cute baby sea otter floating on its back in calm blue water') |
| 15 | + ->generate(); |
| 16 | + |
| 17 | +$image = $response->firstImage(); |
| 18 | +echo $image->url; // https://oaidalleapiprodscus.blob.core.windows.net/... |
| 19 | +``` |
| 20 | + |
| 21 | +## Provider Support |
| 22 | + |
| 23 | +Currently, Prism supports image generation through: |
| 24 | + |
| 25 | +- **OpenAI**: DALL-E 2, DALL-E 3, and GPT-Image-1 models |
| 26 | + |
| 27 | +Additional providers will be added in future releases as the ecosystem evolves. |
| 28 | + |
| 29 | +## Basic Usage |
| 30 | + |
| 31 | +### Simple Generation |
| 32 | + |
| 33 | +The most straightforward way to generate an image: |
| 34 | + |
| 35 | +```php |
| 36 | +$response = Prism::image() |
| 37 | + ->using('openai', 'dall-e-3') |
| 38 | + ->withPrompt('A serene mountain landscape at sunset') |
| 39 | + ->generate(); |
| 40 | + |
| 41 | +// Access the generated image |
| 42 | +$image = $response->firstImage(); |
| 43 | +if ($image->hasUrl()) { |
| 44 | + echo "Image URL: " . $image->url; |
| 45 | +} |
| 46 | +if ($image->hasBase64()) { |
| 47 | + echo "Base64 Image Data: " . $image->base64; |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +### Working with Responses |
| 52 | + |
| 53 | +The response object provides helpful methods for accessing generated content: |
| 54 | + |
| 55 | +```php |
| 56 | +$response = Prism::image() |
| 57 | + ->using('openai', 'dall-e-2') |
| 58 | + ->withPrompt('Abstract geometric patterns in vibrant colors') |
| 59 | + ->generate(); |
| 60 | + |
| 61 | +// Check if images were generated |
| 62 | +if ($response->hasImages()) { |
| 63 | + echo "Generated {$response->imageCount()} image(s)"; |
| 64 | + |
| 65 | + // Access all images |
| 66 | + foreach ($response->images as $image) { |
| 67 | + if ($image->hasUrl()) { |
| 68 | + echo "Image: {$image->url}\n"; |
| 69 | + } |
| 70 | + |
| 71 | + if ($image->hasBase64()) { |
| 72 | + echo "Base64 Image: " . substr($image->base64, 0, 50) . "...\n"; |
| 73 | + } |
| 74 | + |
| 75 | + if ($image->hasRevisedPrompt()) { |
| 76 | + echo "Revised prompt: {$image->revisedPrompt}\n"; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // Or just get the first one |
| 81 | + $firstImage = $response->firstImage(); |
| 82 | +} |
| 83 | + |
| 84 | +// Check usage information |
| 85 | +echo "Prompt tokens: {$response->usage->promptTokens}"; |
| 86 | +echo "Model used: {$response->meta->model}"; |
| 87 | +``` |
| 88 | + |
| 89 | +## Provider-Specific Options |
| 90 | + |
| 91 | +While Prism provides a consistent API, you can access provider-specific features using the `withProviderOptions()` method. |
| 92 | + |
| 93 | +### OpenAI Options |
| 94 | + |
| 95 | +OpenAI offers various customization options depending on the model: |
| 96 | + |
| 97 | +#### DALL-E 3 Options |
| 98 | + |
| 99 | +```php |
| 100 | +$response = Prism::image() |
| 101 | + ->using('openai', 'dall-e-3') |
| 102 | + ->withPrompt('A beautiful sunset over mountains') |
| 103 | + ->withProviderOptions([ |
| 104 | + 'size' => '1792x1024', // 1024x1024, 1024x1792, 1792x1024 |
| 105 | + 'quality' => 'hd', // standard, hd |
| 106 | + 'style' => 'vivid', // vivid, natural |
| 107 | + 'response_format' => 'url', // url, b64_json |
| 108 | + ]) |
| 109 | + ->generate(); |
| 110 | +``` |
| 111 | + |
| 112 | +#### GPT-Image-1 (Base64 Only) |
| 113 | + |
| 114 | +The GPT-Image-1 model always returns base64-encoded images, regardless of the `response_format` setting: |
| 115 | + |
| 116 | +```php |
| 117 | +$response = Prism::image() |
| 118 | + ->using('openai', 'gpt-image-1') |
| 119 | + ->withPrompt('A cute baby sea otter floating on its back') |
| 120 | + ->withProviderOptions([ |
| 121 | + 'size' => '1024x1024', // 1024x1024, 1536x1024, 1024x1536, auto |
| 122 | + 'quality' => 'high', // auto, high, medium, low |
| 123 | + 'background' => 'transparent', // transparent, opaque, auto |
| 124 | + 'output_format' => 'png', // png, jpeg, webp |
| 125 | + 'output_compression' => 90, // 0-100 (for jpeg/webp) |
| 126 | + ]) |
| 127 | + ->generate(); |
| 128 | + |
| 129 | +$image = $response->firstImage(); |
| 130 | +if ($image->hasBase64()) { |
| 131 | + // Save the base64 image to a file |
| 132 | + file_put_contents('generated-image.png', base64_decode($image->base64)); |
| 133 | + echo "Base64 image saved to generated-image.png"; |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +#### Base64 vs URL Responses |
| 138 | + |
| 139 | +Different models return images in different formats: |
| 140 | + |
| 141 | +- **GPT-Image-1**: Always returns base64-encoded images in the `base64` property |
| 142 | +- **DALL-E 2 & 3**: Return URLs by default, but can return base64 when `response_format` is set to `'b64_json'` |
| 143 | + |
| 144 | +```php |
| 145 | +// Request base64 format from DALL-E 3 |
| 146 | +$response = Prism::image() |
| 147 | + ->using('openai', 'dall-e-3') |
| 148 | + ->withPrompt('Abstract art') |
| 149 | + ->withProviderOptions([ |
| 150 | + 'response_format' => 'b64_json', |
| 151 | + ]) |
| 152 | + ->generate(); |
| 153 | + |
| 154 | +$image = $response->firstImage(); |
| 155 | +if ($image->hasBase64()) { |
| 156 | + echo "Received base64 image data"; |
| 157 | +} |
| 158 | +``` |
| 159 | + |
| 160 | +## Testing |
| 161 | + |
| 162 | +Prism provides convenient fakes for testing image generation: |
| 163 | + |
| 164 | +```php |
| 165 | +use Prism\Prism\Prism; |
| 166 | +use Prism\Prism\Testing\PrismFake; |
| 167 | + |
| 168 | +test('can generate images', function () { |
| 169 | + $fake = PrismFake::create()->image(); |
| 170 | + Prism::fake($fake); |
| 171 | + |
| 172 | + $response = Prism::image() |
| 173 | + ->using('openai', 'dall-e-3') |
| 174 | + ->withPrompt('Test image') |
| 175 | + ->generate(); |
| 176 | + |
| 177 | + expect($response->hasImages())->toBeTrue(); |
| 178 | + expect($response->firstImage()->url)->toContain('fake-image-url'); |
| 179 | +}); |
| 180 | +``` |
| 181 | + |
| 182 | +Need help with a specific provider or use case? Check the [provider documentation](/providers/openai) for detailed configuration options and examples. |
0 commit comments