// code from goatstore // api: https://store.agi.bd const axios = require("axios"); const fs = require("fs-extra"); const path = require("path"); const { createCanvas, loadImage } = require("canvas"); module.exports = { config: { name: "4k", version: "1.1.0", author: "KABIR4X⚡", countDown: 10, role: 0, description: { en: "Enhance & upscale photo to high quality (clear & beautiful)" }, category: "image", guide: { en: "{pn} (reply to a photo)\n{pn} (send photo with command)" } }, onStart: async function ({ event, message }) { const { messageReply, attachments } = event; let imageUrl = null; // Reply to photo if (messageReply?.attachments?.[0]?.type === "photo") { imageUrl = messageReply.attachments[0].url; } // Send photo with command else if (attachments?.[0]?.type === "photo") { imageUrl = attachments[0].url; } if (!imageUrl) { return message.reply("⚠️ Please reply to a photo or send a photo with the command `4k`"); } try { const wait = await message.reply("✨ Enhancing photo to high quality...\nPlease wait..."); // Download original image const res = await axios.get(imageUrl, { responseType: "arraybuffer" }); const inputPath = path.join(__dirname, "tmp", `in_${Date.now()}.jpg`); const outputPath = path.join(__dirname, "tmp", `4k_${Date.now()}.jpg`); await fs.ensureDir(path.join(__dirname, "tmp")); await fs.writeFile(inputPath, Buffer.from(res.data)); const img = await loadImage(inputPath); const originalWidth = img.width; const originalHeight = img.height; // Scale to near 4K while keeping aspect ratio const maxSide = 2560; // good balance for Messenger let scale = 1; if (originalWidth > originalHeight) { scale = maxSide / originalWidth; } else { scale = maxSide / originalHeight; } // Don't downscale if already large if (scale < 1.2) scale = 1.8; const newWidth = Math.round(originalWidth * scale); const newHeight = Math.round(originalHeight * scale); const canvas = createCanvas(newWidth, newHeight); const ctx = canvas.getContext("2d"); // High quality settings ctx.imageSmoothingEnabled = true; ctx.imageSmoothingQuality = "high"; // Draw image ctx.drawImage(img, 0, 0, newWidth, newHeight); // Light sharpening (makes it look clearer) const imageData = ctx.getImageData(0, 0, newWidth, newHeight); const data = imageData.data; const sharpen = 0.35; // soft sharpen for (let i = 0; i < data.length; i += 4) { // Simple unsharp-ish effect on brightness const avg = (data[i] + data[i + 1] + data[i + 2]) / 3; data[i] = Math.min(255, data[i] + (data[i] - avg) * sharpen); data[i + 1] = Math.min(255, data[i + 1] + (data[i + 1] - avg) * sharpen); data[i + 2] = Math.min(255, data[i + 2] + (data[i + 2] - avg) * sharpen); } ctx.putImageData(imageData, 0, 0); // Save high quality const buffer = canvas.toBuffer("image/jpeg", { quality: 0.95 }); await fs.writeFile(outputPath, buffer); await message.reply({ body: `✅ Enhanced successfully!\n📐 ${newWidth} × ${newHeight}\n👑 Author: KABIR4X⚡`, attachment: fs.createReadStream(outputPath) }); // Cleanup await message.unsend(wait.messageID); fs.unlink(inputPath).catch(() => {}); fs.unlink(outputPath).catch(() => {}); } catch (err) { console.error(err); message.reply("❌ Failed to enhance the photo. Please try again with a clearer image."); } } };