-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
119 lines (99 loc) · 3.97 KB
/
Copy pathapp.js
File metadata and controls
119 lines (99 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
const express = require("express");
const multer = require("multer");
const path = require("path");
const { removeBackground } = require("@imgly/background-removal-node");
const { reduceImage } = require("./imageReducer");
const app = express();
const port = 3000;
const storage = multer.memoryStorage();
const upload = multer({ storage: storage });
app.use(express.static(path.join(__dirname, "public")));
app.get("/ads.txt", (_, res) => {
res.sendFile(path.join(__dirname, "ads.txt"));
});
app.get("/", (_, res) => {
res.sendFile(path.join(__dirname, "index.html"));
});
app.post("/upload", upload.array("images"), async (req, res) => {
try {
const quality = parseInt(req.body.quality);
const maxSizeValue = parseInt(req.body.maxSizeValue);
const maxSizeUnit = req.body.maxSizeUnit; // KB or MB
const isRemoveBackground = req.body.isRemoveBackground === "on";
let maxSize;
if (maxSizeUnit === "MB") {
maxSize = maxSizeValue * 1024 * 1024; // Convert MB to bytes
} else {
maxSize = maxSizeValue * 1024; // Keep it in KB
}
const maxWidth = parseInt(req.body.maxWidth);
const maxHeight = parseInt(req.body.maxHeight);
const addWhitePaddingIfNeeded = async (file) => {
const imageBuffer = file.buffer;
const reductionOptions = { quality, maxSize, maxWidth, maxHeight, isInstagramFeedFormat: req.body.isInstagramFeedFormat === "on" };
return reduceImage(imageBuffer, reductionOptions);
};
const removeBackgroundIfNeeded = async (file) => {
try {
const resultBlob = await removeBackground(file.buffer);
const resultBuffer = Buffer.from(await resultBlob.arrayBuffer());
return resultBuffer;
} catch (error) {
console.error("Error removing background:", error);
return file.buffer;
}
};
let reducedImages = req.files;
if (isRemoveBackground) {
reducedImages = await Promise.all(reducedImages.map(removeBackgroundIfNeeded));
}
reducedImages = await Promise.all(reducedImages.map(addWhitePaddingIfNeeded));
// Prepare an array to store data URIs
const dataURIs = [];
// Convert each image buffer to a data URI and push to the array
reducedImages.forEach((buffer, _) => {
const mimeType = "image/jpeg";
const base64Image = buffer.toString("base64");
const dataURI = `data:${mimeType};base64,${base64Image}`;
dataURIs.push(dataURI);
});
// Send HTML response with embedded data URIs
res.send(`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bulk Image Size Reducer</title>
<!-- Include Tailwind CSS -->
<link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
</head>
<body class="font-sans text-center bg-gray-100 py-10">
<h1 class="text-4xl mb-6">Bulk Image Size Reducer</h1>
<div class="mb-8">
${dataURIs
.map(
(dataURI, index) => `
<div class="mb-4">
<span class="text-lg font-semibold">Image ${index + 1}:</span>
<a href="${dataURI}" download="reduced_image_${index + 1}.jpg" class="ml-2 inline-block px-3 py-1 bg-blue-500 text-white font-semibold rounded-full hover:bg-blue-600 focus:outline-none focus:ring focus:ring-blue-200 focus:ring-opacity-50">Download</a>
</div>`
)
.join("")}
</div>
<button onclick="resetForm()" class="bg-red-500 text-white font-semibold px-4 py-2 rounded-full hover:bg-red-600 focus:outline-none focus:ring focus:ring-red-200 focus:ring-opacity-50">Reset Form</button>
<script>
function resetForm() {
location.href = "/";
}
</script>
</body>
</html>
`);
} catch (error) {
res.status(500).send(`<h2>Error reducing images: ${error.message}</h2>`);
}
});
app.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`);
});