AI Image Generator
AI Image Generator
function generateImage() {
const prompt = document.getElementById("prompt").value;
const result = document.getElementById("result");
if (!prompt) {
alert("Please image idea likho");
return;
}
result.innerHTML = "Image generate ho rahi hai... ⏳";
fetch("generate.php", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ prompt: prompt })
})
.then(res => res.json())
.then(data => {
if (data.image) {
result.innerHTML = `

`;
} else {
result.innerHTML = "Error aayi 😓";
}
})
.catch(err => {
result.innerHTML = "Server error ❌";
});
}
";
$input = json_decode(file_get_contents("php://input"), true);
$prompt = $input["prompt"];
$ch = curl_init("https://api.openai.com/v1/images/generations");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Content-Type: application/json",
"Authorization: Bearer $apiKey"
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
"prompt" => $prompt,
"n" => 1,
"size" => "512x512"
]));
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response, true);
echo json_encode([
"image" => $data["data"][0]["url"]
]);
No comments:
Post a Comment