Spaces:
Paused
Paused
Update index.js
Browse files
index.js
CHANGED
|
@@ -1 +1,143 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require('express');
|
| 2 |
+
const { createCanvas, loadImage } = require('canvas');
|
| 3 |
+
const app = express();
|
| 4 |
+
const port = 3000;
|
| 5 |
+
|
| 6 |
+
app.use(express.json());
|
| 7 |
+
app.use(express.urlencoded({ extended: true }));
|
| 8 |
+
|
| 9 |
+
/**
|
| 10 |
+
* Fungsi untuk menghasilkan gambar status kustom
|
| 11 |
+
* @param {string} profileImage - URL gambar profil
|
| 12 |
+
* @param {string} mainImage - URL gambar utama
|
| 13 |
+
* @param {string} caption - Teks caption
|
| 14 |
+
* @param {number} views - Jumlah tayangan
|
| 15 |
+
* @returns {Promise<Buffer>} - Buffer gambar dalam format PNG
|
| 16 |
+
*/
|
| 17 |
+
async function createCustomSWGenerator({ profileImage, mainImage, caption = "Custom Caption", views = 4 }) {
|
| 18 |
+
const canvasWidth = 1080;
|
| 19 |
+
const canvasHeight = 1920;
|
| 20 |
+
const canvas = createCanvas(canvasWidth, canvasHeight);
|
| 21 |
+
const ctx = canvas.getContext("2d");
|
| 22 |
+
|
| 23 |
+
// Background hitam
|
| 24 |
+
ctx.fillStyle = "#000000";
|
| 25 |
+
ctx.fillRect(0, 0, canvasWidth, canvasHeight);
|
| 26 |
+
|
| 27 |
+
// Tambahkan gambar utama
|
| 28 |
+
if (mainImage) {
|
| 29 |
+
try {
|
| 30 |
+
const mainImg = await loadImage(mainImage);
|
| 31 |
+
ctx.drawImage(mainImg, 0, 200, canvasWidth, 1080);
|
| 32 |
+
} catch (error) {
|
| 33 |
+
console.error("Gagal memuat gambar utama:", error.message);
|
| 34 |
+
}
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
// Header background
|
| 38 |
+
ctx.fillStyle = "rgba(0, 0, 0, 0.7)";
|
| 39 |
+
ctx.fillRect(0, 0, canvasWidth, 180);
|
| 40 |
+
|
| 41 |
+
// Gambar profil dengan border putih
|
| 42 |
+
if (profileImage) {
|
| 43 |
+
try {
|
| 44 |
+
const profileImg = await loadImage(profileImage);
|
| 45 |
+
ctx.save();
|
| 46 |
+
ctx.beginPath();
|
| 47 |
+
ctx.arc(90, 90, 60, 0, Math.PI * 2);
|
| 48 |
+
ctx.closePath();
|
| 49 |
+
ctx.clip();
|
| 50 |
+
|
| 51 |
+
// Gambar profil
|
| 52 |
+
ctx.drawImage(profileImg, 30, 30, 120, 120);
|
| 53 |
+
|
| 54 |
+
// Garis putih di luar profil
|
| 55 |
+
ctx.beginPath();
|
| 56 |
+
ctx.arc(90, 90, 62, 0, Math.PI * 2); // Radius sedikit lebih besar
|
| 57 |
+
ctx.strokeStyle = "white";
|
| 58 |
+
ctx.lineWidth = 10; // Stroke putih tipis
|
| 59 |
+
ctx.stroke();
|
| 60 |
+
ctx.closePath();
|
| 61 |
+
ctx.restore();
|
| 62 |
+
} catch (error) {
|
| 63 |
+
console.error("Gagal memuat gambar profil:", error.message);
|
| 64 |
+
}
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
// Teks header
|
| 68 |
+
ctx.fillStyle = "#FFFFFF";
|
| 69 |
+
ctx.font = "50px 'Roboto Sans', Arial"; // Gunakan Roboto Sans jika tersedia
|
| 70 |
+
ctx.fillText("Status saya", 180, 80);
|
| 71 |
+
|
| 72 |
+
ctx.fillStyle = "#CCCCCC";
|
| 73 |
+
ctx.font = "30px 'Roboto Sans', Arial"; // Gunakan Roboto Sans
|
| 74 |
+
ctx.fillText("1 menit yang lalu", 180, 130);
|
| 75 |
+
|
| 76 |
+
// Caption teks
|
| 77 |
+
ctx.fillStyle = "#FFFFFF";
|
| 78 |
+
ctx.font = "40px 'Roboto Sans', Arial";
|
| 79 |
+
ctx.textAlign = "center";
|
| 80 |
+
ctx.fillText(caption, canvasWidth / 2, 1400);
|
| 81 |
+
|
| 82 |
+
// Footer background
|
| 83 |
+
ctx.fillStyle = "rgba(0, 0, 0, 0.7)";
|
| 84 |
+
ctx.fillRect(0, 1620, canvasWidth, 300);
|
| 85 |
+
|
| 86 |
+
// Ikon dan teks footer
|
| 87 |
+
try {
|
| 88 |
+
const viewIcon = await loadImage('https://example.com/eye.png'); // Ganti dengan URL ikon tayangan
|
| 89 |
+
const shareIcon = await loadImage('https://example.com/share.png'); // Ganti dengan URL ikon bagikan
|
| 90 |
+
const promoteIcon = await loadImage('https://example.com/promotion.png'); // Ganti dengan URL ikon promosikan
|
| 91 |
+
|
| 92 |
+
// "Tayangan"
|
| 93 |
+
ctx.drawImage(viewIcon, 80, 1660, 70, 70); // Ikon tayangan
|
| 94 |
+
ctx.fillStyle = "#FFFFFF";
|
| 95 |
+
ctx.font = "30px 'Roboto Sans', Arial";
|
| 96 |
+
ctx.textAlign = "center";
|
| 97 |
+
ctx.fillText(`${views} Tayangan`, 170, 1780);
|
| 98 |
+
|
| 99 |
+
// "Promosikan"
|
| 100 |
+
ctx.drawImage(promoteIcon, 460, 1660, 70, 70); // Ikon promosikan
|
| 101 |
+
ctx.fillText("Promosikan", 550, 1780);
|
| 102 |
+
|
| 103 |
+
// "Bagikan"
|
| 104 |
+
ctx.drawImage(shareIcon, 840, 1660, 70, 70); // Ikon bagikan
|
| 105 |
+
ctx.fillText("Bagikan", 920, 1780);
|
| 106 |
+
} catch (error) {
|
| 107 |
+
console.error("Gagal memuat ikon:", error.message);
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
// Kembalikan gambar sebagai Buffer PNG
|
| 111 |
+
return canvas.toBuffer('image/png');
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
/**
|
| 115 |
+
* Endpoint untuk membuat gambar status
|
| 116 |
+
* @route POST /generate-status
|
| 117 |
+
* @param {string} profileImage - URL gambar profil
|
| 118 |
+
* @param {string} mainImage - URL gambar utama
|
| 119 |
+
* @param {string} caption - Teks caption
|
| 120 |
+
* @param {number} views - Jumlah tayangan
|
| 121 |
+
* @returns {Buffer} - Gambar dalam format PNG
|
| 122 |
+
*/
|
| 123 |
+
app.post('/generate-status', async (req, res) => {
|
| 124 |
+
const { profileImage, mainImage, caption, views } = req.body;
|
| 125 |
+
|
| 126 |
+
if (!profileImage || !mainImage) {
|
| 127 |
+
return res.status(400).json({ error: "Gambar profil dan gambar utama harus disediakan." });
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
try {
|
| 131 |
+
const imageBuffer = await createCustomSWGenerator({ profileImage, mainImage, caption, views });
|
| 132 |
+
res.setHeader('Content-Type', 'image/png');
|
| 133 |
+
res.send(imageBuffer);
|
| 134 |
+
} catch (error) {
|
| 135 |
+
console.error(error);
|
| 136 |
+
res.status(500).json({ error: "Gagal membuat gambar." });
|
| 137 |
+
}
|
| 138 |
+
});
|
| 139 |
+
|
| 140 |
+
// Mulai server Express
|
| 141 |
+
app.listen(port, () => {
|
| 142 |
+
console.log(`Server berjalan di http://localhost:${port}`);
|
| 143 |
+
});
|