feat: backend route for removing backgrounds from images with imgly

This commit is contained in:
theoleuthardt 2025-02-22 14:29:36 +01:00
parent 91125204b5
commit e368155cdb
4 changed files with 1580 additions and 2 deletions

1533
backend/package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -14,13 +14,15 @@
"dependencies": { "dependencies": {
"@fastify/cors": "^10.0.2", "@fastify/cors": "^10.0.2",
"@fastify/multipart": "^9.0.3", "@fastify/multipart": "^9.0.3",
"@imgly/background-removal-node": "^1.4.5",
"dotenv": "^16.4.7", "dotenv": "^16.4.7",
"fastify": "^5.2.1", "fastify": "^5.2.1",
"fastify-cors": "^6.0.3", "fastify-cors": "^6.0.3",
"fastify-multipart": "^5.3.1", "fastify-multipart": "^5.3.1",
"libreoffice-convert": "^1.6.0", "libreoffice-convert": "^1.6.0",
"luxon": "^3.5.0", "luxon": "^3.5.0",
"qrcode": "^1.5.4" "qrcode": "^1.5.4",
"sharp": "^0.33.5"
}, },
"devDependencies": { "devDependencies": {
"@types/luxon": "^3.4.2", "@types/luxon": "^3.4.2",

View file

@ -8,6 +8,7 @@ import { regexTest } from "./src/routes/regextest.route";
import { tmzConvert } from "./src/routes/tmzconvert.route"; import { tmzConvert } from "./src/routes/tmzconvert.route";
import { generateQRCode } from "./src/routes/generateqrcode.route"; import { generateQRCode } from "./src/routes/generateqrcode.route";
import { wordCounter } from "./src/routes/wordcounter.route"; import { wordCounter } from "./src/routes/wordcounter.route";
import { removeBG } from "./src/routes/removebg.route";
const app = Fastify({ logger: true }); const app = Fastify({ logger: true });
@ -25,6 +26,7 @@ app.register(regexTest);
app.register(tmzConvert); app.register(tmzConvert);
app.register(generateQRCode); app.register(generateQRCode);
app.register(wordCounter); app.register(wordCounter);
app.register(removeBG);
const PORT = process.env.PORT || 4000; const PORT = process.env.PORT || 4000;
app.listen({ port: Number(PORT), host: "0.0.0.0" }, () => { app.listen({ port: Number(PORT), host: "0.0.0.0" }, () => {

View file

@ -0,0 +1,43 @@
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
import { Config, removeBackground } from "@imgly/background-removal-node";
import sharp from "sharp";
export async function removeBG(app: FastifyInstance) {
app.post(
"/api/remove-bg",
async (request: FastifyRequest, reply: FastifyReply) => {
try {
const parts = request.parts();
let fileBuffer: Buffer | null = null;
for await (const part of parts) {
if (part.type === "file") {
fileBuffer = await part.toBuffer();
}
}
if (!fileBuffer) {
return reply.status(400).send({ error: "No file uploaded!" });
}
console.log("Received file, buffer length:", fileBuffer.length);
const rightFileBuffer = await sharp(fileBuffer)
.toFormat("png")
.toBuffer();
console.log("Converted file:", rightFileBuffer);
const convertedBuffer = await removeBackground(rightFileBuffer);
reply
.header("Content-Type", "image/png")
.header("Content-Disposition", `attachment; filename="converted.png"`)
.status(200)
.send(convertedBuffer);
} catch (error) {
console.error("Convert error:", error);
reply.status(500).send({ error: "Error while converting!" });
}
},
);
}