/** * Fake e-KTP Canvas Generator Engine * Author: Vamz Spectre (VamxAPI) * License: MIT * * Fitur: * - 1:1 Pixel-accurate generator kartu e-KTP Indonesia (720 x 463). * - Customization lengkap: NIK, Nama, Provinsi, Kota, TTL, Jenis Kelamin, Gol Darah, * Alamat, RT/RW, Kelurahan/Desa, Kecamatan, Agama, Status Perkawinan, Pekerjaan, * Kewarganegaraan, Masa Berlaku, dan Tanggal Diterbitkan. * - Auto-download template KTP dan font resmi (ArrialKTP, OcrKTP, SignKTP) langsung dari Vamx CDN. * - Pasfoto otomatis di-crop dan diposisikan proporsional pada bingkai foto KTP. * - Tanda tangan digital otomatis disesuaikan dengan nama pemilik KTP. * * Cara Penggunaan CLI: * 1. Download kode snippet: * curl -sL "https://code.vamxapi.my.id/snippet/raw/fakektp" -o fakektp.js * * 2. Jalankan langsung via terminal: * node fakektp.js "https://picsum.photos/400/600" "BUDI SANTOSO" "3171010101900001" * node fakektp.js "https://picsum.photos/400/600" "nama|provinsi|kota|nik|ttl|jk|goldar|alamat|rt/rw|kelurahan|kecamatan|agama|status|pekerjaan|wn|masa|terbuat" */ const fs = require("fs"); const path = require("path"); const https = require("https"); const { Canvas, loadImage, FontLibrary } = require("skia-canvas"); // Direktori cache aset (template & fonts) const CACHE_DIR = path.join(process.cwd(), ".cache_ktp"); // CDN URL Aset const ASSETS_CDN = { template: "https://cdn.vamxapi.my.id/m/m_f1f482ad2035.png", arrial: "https://cdn.vamxapi.my.id/m/m_887d795cbeb8.ttf", ocr: "https://cdn.vamxapi.my.id/m/m_f5704a3d097f.ttf", sign: "https://cdn.vamxapi.my.id/m/m_b4b0b5f87c73.ttf", }; /** * Helper untuk mengunduh buffer atau file dari URL */ function downloadFile(url, destPath) { return new Promise((resolve, reject) => { if (fs.existsSync(destPath) && fs.statSync(destPath).size > 0) { return resolve(destPath); } const dir = path.dirname(destPath); if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true }); const file = fs.createWriteStream(destPath); const get = (targetUrl) => { https .get(targetUrl, (res) => { if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) { return get(res.headers.location); } if (res.statusCode !== 200) { file.close(); fs.unlinkSync(destPath); return reject(new Error(`Failed to download asset from ${targetUrl}: HTTP ${res.statusCode}`)); } res.pipe(file); file.on("finish", () => { file.close(() => resolve(destPath)); }); }) .on("error", (err) => { file.close(); if (fs.existsSync(destPath)) fs.unlinkSync(destPath); reject(err); }); }; get(url); }); } function fetchBuffer(url) { return new Promise((resolve, reject) => { const get = (targetUrl) => { https .get(targetUrl, (res) => { if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) { return get(res.headers.location); } if (res.statusCode !== 200) { return reject(new Error(`HTTP error ${res.statusCode} fetching ${targetUrl}`)); } const chunks = []; res.on("data", (c) => chunks.push(c)); res.on("end", () => resolve(Buffer.concat(chunks))); }) .on("error", reject); }; get(url); }); } let fontsLoaded = false; async function ensureKtpAssets() { if (!fs.existsSync(CACHE_DIR)) { fs.mkdirSync(CACHE_DIR, { recursive: true }); } const localAssetDir = path.join(process.cwd(), "assets/ktp"); const getPath = (filename, cdnUrl) => { const local = path.join(localAssetDir, filename); if (fs.existsSync(local) && fs.statSync(local).size > 0) return local; return path.join(CACHE_DIR, filename); }; const templatePath = getPath("template.png", ASSETS_CDN.template); const arrialPath = getPath("arrial.ttf", ASSETS_CDN.arrial); const ocrPath = getPath("ocr.ttf", ASSETS_CDN.ocr); const signPath = getPath("sign.ttf", ASSETS_CDN.sign); await Promise.all([ fs.existsSync(templatePath) ? Promise.resolve() : downloadFile(ASSETS_CDN.template, templatePath), fs.existsSync(arrialPath) ? Promise.resolve() : downloadFile(ASSETS_CDN.arrial, arrialPath), fs.existsSync(ocrPath) ? Promise.resolve() : downloadFile(ASSETS_CDN.ocr, ocrPath), fs.existsSync(signPath) ? Promise.resolve() : downloadFile(ASSETS_CDN.sign, signPath), ]); if (!fontsLoaded) { try { FontLibrary.use("ArrialKTP", arrialPath); FontLibrary.use("OcrKTP", ocrPath); FontLibrary.use("SignKTP", signPath); fontsLoaded = true; } catch {} } return { templatePath }; } /** * Parser string dengan separator "|" */ function parsePipeParams(text) { const v = text.split("|").map((s) => s.trim()); return { nama: v[0] || undefined, prov: v[1] || undefined, kota: v[2] || undefined, nik: v[3] || undefined, ttl: v[4] || undefined, jk: v[5] || undefined, goldar: v[6] || undefined, alamat: v[7] || undefined, rtrw: v[8] || undefined, keldesa: v[9] || undefined, kec: v[10] || undefined, agama: v[11] || undefined, status: v[12] || undefined, kerja: v[13] || undefined, wn: v[14] || undefined, masa: v[15] || undefined, terbuat: v[16] || undefined, }; } /** * Main Fake e-KTP Generator * @param {Object} options Data KTP * @param {string|Buffer} photoInput URL gambar, Buffer, atau path file gambar * @returns {Promise} PNG Buffer */ async function generateFakeKtp(options = {}, photoInput) { const { templatePath } = await ensureKtpAssets(); // Load photo let photoImg = null; if (Buffer.isBuffer(photoInput)) { photoImg = await loadImage(photoInput); } else if (typeof photoInput === "string" && photoInput.startsWith("http")) { const pBuf = await fetchBuffer(photoInput); photoImg = await loadImage(pBuf); } else if (typeof photoInput === "string" && fs.existsSync(photoInput)) { photoImg = await loadImage(photoInput); } else { // Default dummy photo const dummySvg = ` `; photoImg = await loadImage(Buffer.from(dummySvg)); } const templateImg = await loadImage(templatePath); const canvas = new Canvas(720, 463); const ctx = canvas.getContext("2d"); // Gambar template background e-KTP ctx.drawImage(templateImg, 0, 0, 720, 463); // Penempatan & crop pasfoto const PHOTO_X = 520, PHOTO_Y = 80, PHOTO_W = 200, PHOTO_H = 280; const frameRatio = PHOTO_W / PHOTO_H; const imgRatio = photoImg.width / photoImg.height; let sx, sy, sw, sh; if (imgRatio > frameRatio) { sh = photoImg.height; sw = sh * frameRatio; sx = (photoImg.width - sw) / 2; sy = 0; } else { sw = photoImg.width; sh = sw / frameRatio; sx = 0; sy = (photoImg.height - sh) / 2; } const scale = Math.min(PHOTO_W / sw, PHOTO_H / sh) * 0.78; const dw = sw * scale; const dh = sh * scale; const dx = PHOTO_X + (PHOTO_W - dw) / 2 - 15; const dy = PHOTO_Y + (PHOTO_H - dh) / 2; ctx.drawImage(photoImg, sx, sy, sw, sh, dx, dy, dw, dh); // Helper render teks const L = (x, y, text, font, size) => { ctx.font = `${size}px ${font}`; ctx.textAlign = "left"; ctx.textBaseline = "top"; ctx.fillStyle = "#000000"; ctx.fillText(text || "", x, y); }; const C = (x, y, text, font, size) => { ctx.font = `${size}px ${font}`; ctx.textAlign = "center"; ctx.textBaseline = "middle"; ctx.fillStyle = "#000000"; ctx.fillText(text || "", x, y); }; const up = (s) => (s || "").toUpperCase(); const prov = options.prov || "DKI JAKARTA"; const kota = options.kota || "JAKARTA PUSAT"; const nik = options.nik || "3171010101900001"; const nama = options.nama || "BUDI SANTOSO"; const ttl = options.ttl || "JAKARTA, 01-01-2000"; const jk = options.jk || "LAKI-LAKI"; const goldar = options.goldar || "O"; const alamat = options.alamat || "JL. MERDEKA NO. 45"; const rtrw = options.rtrw || "001/002"; const keldesa = options.keldesa || "GAMBIR"; const kec = options.kec || "GAMBIR"; const agama = options.agama || "ISLAM"; const status = options.status || "BELUM KAWIN"; const kerja = options.kerja || "PELAJAR/MAHASISWA"; const wn = options.wn || "WNI"; const masa = options.masa || "SEUMUR HIDUP"; const terbuat = options.terbuat || "01-01-2024"; // Header Provinsi & Kota C(380, 45, "PROVINSI " + up(prov), "ArrialKTP", 25); C(380, 70, "KOTA " + up(kota), "ArrialKTP", 25); // NIK dengan font OCR ctx.font = "32px OcrKTP"; ctx.textAlign = "left"; ctx.textBaseline = "top"; ctx.fillStyle = "#000000"; ctx.fillText(nik, 170, 105); // Data Biodata Pribadi L(190, 145, up(nama), "ArrialKTP", 16); L(190, 168, up(ttl), "ArrialKTP", 16); L(190, 191, up(jk), "ArrialKTP", 16); L(463, 190, up(goldar), "ArrialKTP", 16); L(190, 212, up(alamat), "ArrialKTP", 16); L(190, 234, up(rtrw), "ArrialKTP", 16); L(190, 257, up(keldesa), "ArrialKTP", 16); L(190, 279, up(kec), "ArrialKTP", 16); L(190, 300, up(agama), "ArrialKTP", 16); L(190, 323, up(status), "ArrialKTP", 16); L(190, 346, up(kerja), "ArrialKTP", 16); L(190, 369, up(wn), "ArrialKTP", 16); L(190, 390, up(masa), "ArrialKTP", 16); // Penerbitan Kota & Tanggal L(553, 345, "KOTA " + up(kota), "ArrialKTP", 12); L(570, 365, terbuat, "ArrialKTP", 12); // Tanda Tangan ctx.font = "40px SignKTP"; const firstName = nama.split(" ")[0] || nama; ctx.fillText(firstName, 540, 395); return await canvas.toBuffer("image/png"); } module.exports = { generateFakeKtp, parsePipeParams, }; // ========================================== // CLI RUNNER (WAJIB KONSISTEN SESUAI RULE) // ========================================== if (require.main === module) { const arg1 = process.argv[2] || ""; const arg2 = process.argv[3] || ""; const arg3 = process.argv[4] || ""; let photo = "https://picsum.photos/400/600"; let ktpData = {}; if (arg1.startsWith("http") || fs.existsSync(arg1)) { photo = arg1; if (arg2.includes("|")) { ktpData = parsePipeParams(arg2); } else if (arg2) { ktpData.nama = arg2; if (arg3) ktpData.nik = arg3; } } else if (arg1.includes("|")) { ktpData = parsePipeParams(arg1); if (arg2.startsWith("http") || fs.existsSync(arg2)) { photo = arg2; } } else if (arg1) { ktpData.nama = arg1; if (arg2 && !arg2.startsWith("http")) ktpData.nik = arg2; if (arg3 && (arg3.startsWith("http") || fs.existsSync(arg3))) photo = arg3; } const outputPath = path.join(process.cwd(), "fake_ktp.png"); generateFakeKtp(ktpData, photo) .then((buf) => { fs.writeFileSync(outputPath, buf); const res = { status: true, creator: "Vamz Spectre", result: { file: outputPath, nama: ktpData.nama || "BUDI SANTOSO", nik: ktpData.nik || "3171010101900001", size: `${(buf.length / 1024).toFixed(2)} KB`, width: 720, height: 463, }, }; console.log(JSON.stringify(res, null, 2)); }) .catch((err) => { const errRes = { status: false, creator: "Vamz Spectre", message: err.message || "Gagal membuat gambar e-KTP.", }; console.error(JSON.stringify(errRes, null, 2)); process.exit(1); }); }