|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <meta charset="UTF-8" /> |
| 5 | + <meta http-equiv="X-UA-Compatible" content="IE=edge" /> |
| 6 | + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| 7 | + <script src="https://cdn.tailwindcss.com"></script> |
| 8 | + <title>Image Gererator using OpenAI API</title> |
| 9 | + <style> |
| 10 | + .min-height { |
| 11 | + min-height: 100vh; |
| 12 | + } |
| 13 | + </style> |
| 14 | + </head> |
| 15 | + <body class="max-w-4xl mx-auto"> |
| 16 | + <div |
| 17 | + class="min-height flex flex-col justify-center px-4 py-12 sm:px-6 lg:px-8 -mt-20" |
| 18 | + > |
| 19 | + <div class="text-center"> |
| 20 | + <h1 class="text-4xl font-bold mb-10">Image Generator</h1> |
| 21 | + <p class="text-md lg:text-xl mb-10"> |
| 22 | + This is a simple image generator using |
| 23 | + <a href="https://openai.com/api/" class="font-semibold" |
| 24 | + >OpenAI API.</a |
| 25 | + > |
| 26 | + you can generate images by entering short description of the image or |
| 27 | + by entering a keyword. |
| 28 | + </p> |
| 29 | + </div> |
| 30 | + <div class="flex flex-row justify-center"> |
| 31 | + <input |
| 32 | + type="text" |
| 33 | + id="text" |
| 34 | + class="shadow-lg outline-none w-full rounded-l-lg px-4 py-4 text-gray-600 border-r-2 border-gray-200" |
| 35 | + placeholder="An Impressionist oil painting of sunflowers in a purple vase…" |
| 36 | + /> |
| 37 | + <button |
| 38 | + id="btn" |
| 39 | + class="font-bold py-4 px-8 rounded-r-lg shadow-lg" |
| 40 | + onclick="generateImage()" |
| 41 | + > |
| 42 | + Generate |
| 43 | + </button> |
| 44 | + </div> |
| 45 | + <div id="image" class="grid grid-cols-2 lg:grid-cols-3 gap-1 mt-4"></div> |
| 46 | + <footer |
| 47 | + class="text-center mt-10 border-t-2 border-gray-200 pt-4" |
| 48 | + id="checkAuthor" |
| 49 | + > |
| 50 | + <p class="text-sm"> |
| 51 | + Made with ❤️ by |
| 52 | + <a href="https://github.com/sauravhathi" class="font-semibold" |
| 53 | + >@sauravhathi</a |
| 54 | + > |
| 55 | + </p> |
| 56 | + </footer> |
| 57 | + </div> |
| 58 | + </body> |
| 59 | + <script> |
| 60 | + const api = "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"; |
| 61 | + const url = "https://api.openai.com/v1/images/generations"; |
| 62 | + const text = document.getElementById("text"); |
| 63 | + const image = document.getElementById("image"); |
| 64 | + const btn = document.getElementById("btn"); |
| 65 | + |
| 66 | + function generateImage() { |
| 67 | + if (text.value === "") { |
| 68 | + alert("Please enter a value"); |
| 69 | + } else { |
| 70 | + const data = { |
| 71 | + prompt: text.value, |
| 72 | + n: 3, |
| 73 | + size: "1024x1024", |
| 74 | + }; |
| 75 | + fetch(url, { |
| 76 | + method: "POST", |
| 77 | + headers: { |
| 78 | + "Content-Type": "application/json", |
| 79 | + Authorization: `Bearer ${api}`, |
| 80 | + }, |
| 81 | + body: JSON.stringify(data), |
| 82 | + }) |
| 83 | + .then((res) => res.json()) |
| 84 | + .then((data) => { |
| 85 | + console.log(data); |
| 86 | + const arraySize = data.data.length; |
| 87 | + for (let i = 0; i < arraySize; i++) { |
| 88 | + image.innerHTML += `<img src="${data.data[i].url}" alt="image" class="w-100 p-2">`; |
| 89 | + } |
| 90 | + }); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + const checkAuthor = document.getElementById("checkAuthor"); |
| 95 | + if (checkAuthor.children[0].children[0].textContent === "@sauravhathi") { |
| 96 | + } else { |
| 97 | + window.location.href = "https://github.com/sauravhathi"; |
| 98 | + } |
| 99 | + |
| 100 | + text.addEventListener("input", function () { |
| 101 | + if (text.value === "") { |
| 102 | + btn.classList.remove("bg-slate-900", "text-slate-50"); |
| 103 | + text.classList.add("border-r-2", "border-gray-200"); |
| 104 | + } else { |
| 105 | + text.classList.remove("border-r-2", "border-gray-200"); |
| 106 | + btn.classList.add("bg-slate-900", "text-slate-50"); |
| 107 | + } |
| 108 | + }); |
| 109 | + </script> |
| 110 | +</html> |
0 commit comments