fast72 commited on
Commit
f75a524
·
verified ·
1 Parent(s): 77840c8

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +14 -8
server.js CHANGED
@@ -1,19 +1,25 @@
1
  const express = require("express");
2
  const multer = require("multer");
3
- const { imageToAscii } = require("ascii-image-converter");
4
 
5
  const app = express();
6
  const upload = multer({ storage: multer.memoryStorage() });
7
 
8
- app.post("/convert", upload.single("image"), async (req, res) => {
9
  if (!req.file) return res.status(400).send("No image uploaded");
10
 
11
- try {
12
- const ascii = await imageToAscii(req.file.buffer, { colored: false });
13
- res.type("text/plain").send(ascii);
14
- } catch (err) {
15
- res.status(500).send("Error processing image");
16
- }
 
 
 
 
 
 
17
  });
18
 
19
  app.listen(7860, () => console.log("Server running on port 7860"));
 
1
  const express = require("express");
2
  const multer = require("multer");
3
+ const { exec } = require("child_process");
4
 
5
  const app = express();
6
  const upload = multer({ storage: multer.memoryStorage() });
7
 
8
+ app.post("/convert", upload.single("image"), (req, res) => {
9
  if (!req.file) return res.status(400).send("No image uploaded");
10
 
11
+ const width = req.query.width || 50;
12
+ const command = `jp2a --width=${width} -`;
13
+
14
+ const child = exec(command, { encoding: "utf8" });
15
+
16
+ child.stdin.write(req.file.buffer);
17
+ child.stdin.end();
18
+
19
+ let ascii = "";
20
+ child.stdout.on("data", (data) => (ascii += data));
21
+ child.stdout.on("end", () => res.type("text/plain").send(ascii));
22
+ child.stderr.on("data", () => res.status(500).send("Error processing image"));
23
  });
24
 
25
  app.listen(7860, () => console.log("Server running on port 7860"));