Skip to main content

Image generation

Generate custom images based on a prompt

Benefits

  • Provides bespoke image generation, enabling quick creation of images as needed.
  • Secures image generation using the Squid backend context to ensure exclusive access to resources.

How it works

To interact with the image generator, use the Squid Client SDK.

Note

The Squid AI image generator requires admin access. Therefore, its methods must be run in the Squid backend or another admin environment that has access to the application's API key.

Create an image generator

To create an image generator, use Squid AI's .image() method:

Backend code
const imageGenerator = this.squid.ai().image();

You can then use this image generation instance using the generate() method. generate() takes two parameters: the string prompt for the image to generate, and the AiGenerateImageOptions type that contains options for generating the image. This function returns a promise that resolves to a URL for the generated image.

The following example shows how to generate an image using generate():

Backend code
const options: AiGenerateImageOptions = {
modelName: 'dall-e-3',
quality: 'standard',
size: '1024x1024',
numberOfImagesToGenerate: 1,
};

const imageUrl = await imageGenerator.generate('a pirate ship', options);

More information on the AiGenerateImageOptions can be found in the reference documentation.

Securing AI image generation

Since the Squid AI image generator requires admin access, it should only be run in the Squid backend or another environment with admin privileges. If you are running the image generator from a client application connected to the Squid backend, you can use an Executable to trigger a backend function from the frontend. The following Executable function only generates an image if the user is authenticated:

Backend code
import { executable, SquidService } from '@squidcloud/backend';
import { AiGenerateImageOptions } from '@squidcloud/client';

@executable()
async generateImage(prompt: string): Promise<string | Error> {
if (!this.isAuthenticated()) {
return Error('Access denied');
}
const options: AiGenerateImageOptions = {
modelName: 'dall-e-3',
quality: 'standard',
size: '1024x1024',
numberOfImagesToGenerate: 1,
};

return await this.squid
.ai()
.image()
.generate(prompt, options);
}

Remove the background from an image

In addition to image generation, Squid also provides a background removal feature. This feature allows you to remove the background from an image, making it easier to isolate the subject of the image.

To use this feature, you can call the removeBackground() method on the image generator instance, passing in the file you want to process.

Backend code
await this.squid.ai().image().removeBackground(IMAGE_FILE_TO_PROCESS);

Next steps

Squid AI offers many AI features, including custom AI agents, long-running AI conversations, and Query with AI functionality. Build powerful AI workflows and applications using Squid AI.