Created
June 21, 2023 12:59
-
-
Save jeremybenaim/7148a4dea3c4bb267ff1a53f9cf7fe33 to your computer and use it in GitHub Desktop.
Using PhotoRoom with Dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:convert'; | |
import 'dart:html'; | |
void main() { | |
final headers = { | |
'Content-Type': 'application/json', | |
'x-api-key': 'YOUR_API_KEY', // Replace with your own API key | |
}; | |
final jsonData = { | |
'imageUrl': 'https://cdn.shopify.com/s/files/1/0540/1726/1765/files/OS-Trays-DarkGreen.jpg?v=1677609582&width=720', | |
'prompt': 'photo of an object disposed on a bathroom counter, in front of luxurious bathroom. award-winning photography, high contrast, dramatic lights. award-winning photography, high contrast, dramatic lights', | |
'negativePrompt': 'oversaturated, ugly, 3d, render, cartoon, grain, low-res, kitsch' | |
}; | |
HttpRequest.request( | |
'https://beta-sdk.photoroom.com/v1/instant-backgrounds', | |
method: 'POST', | |
requestHeaders: headers, | |
sendData: json.encode(jsonData), | |
responseType: 'blob').then((HttpRequest request) { | |
if (request.status == 200) { | |
final blob = request.response as Blob; | |
final imageUrl = Url.createObjectUrl(blob); | |
final imageElement = ImageElement(src: imageUrl); | |
document.body!.append(imageElement); | |
} else { | |
print('API request failed. Status code: ${request.status}'); | |
} | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
html { | |
margin: 1rem; | |
} | |
img { | |
max-width: 100% | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment