generateImagePngFromText method

Future<Uint8List> generateImagePngFromText({
  1. required String apiKey,
  2. required String engineId,
  3. required TextToImageRequestParams params,
  4. String? organization,
  5. String? clientId,
  6. String? clientVersion,
})

Generates an image from the specified prompt and returns the set of bytes in PNG format.

Implementation

Future<Uint8List> generateImagePngFromText({
  required String apiKey,
  required String engineId,
  required TextToImageRequestParams params,
  String? organization,
  String? clientId,
  String? clientVersion,
}) async {
  Map<String, String> headers = {
    "Accept": "image/png",
    "Authorization": apiKey,
    'Content-Type': 'application/json',
    "Access-Control-Allow-Origin": "*", // Required for CORS support to work
  };

  if (organization != null) {
    headers.putIfAbsent("Organization", () => organization);
  }

  if (clientId != null) {
    headers.putIfAbsent("Stability-Client-ID", () => clientId);
  }

  if (clientVersion != null) {
    headers.putIfAbsent("Stability-Client-Version", () => clientVersion);
  }

  Uri endpoint;
  if (secure) {
    endpoint = Uri.https(baseUrl, "/v1/generation/$engineId/text-to-image");
  } else {
    endpoint = Uri.http(baseUrl, "/v1/generation/$engineId/text-to-image");
  }

  var response = await http.post(endpoint,
      headers: headers, body: jsonEncode(params.toJson()));

  if (response.statusCode == 200 || response.statusCode == 201) {
    return response.bodyBytes;
  } else {
    var error = ServerError.fromJson(jsonDecode(response.body));
    throw GenerationException(
        statusCode: response.statusCode, message: error.message);
  }
}