Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

feat: remove background from thumbnail

Brad Barbin653d5996f935
diff --git a/packages/database/supabase/functions/thumbnail/index.ts b/packages/database/supabase/functions/thumbnail/index.ts
index 2262156b77..0aab89219a 100644
--- a/packages/database/supabase/functions/thumbnail/index.ts
+++ b/packages/database/supabase/functions/thumbnail/index.ts
@@ -1,8 +1,23 @@
import { serve } from "https://deno.land/[email protected]/http/server.ts";
import puppeteer from "https://deno.land/x/[email protected]/mod.ts";
import { z } from "https://deno.land/x/[email protected]/mod.ts";
+import { Buffer } from "node:buffer";
import { corsHeaders } from "../lib/headers.ts";
+import {
+ ImageMagick,
+ MagickColor,
+ initializeImageMagick,
+} from "npm:@imagemagick/[email protected]";
+
+const wasmBytes = await Deno.readFile(
+ new URL(
+ "magick.wasm",
+ import.meta.resolve("npm:@imagemagick/[email protected]")
+ )
+);
+await initializeImageMagick(wasmBytes);
+
const payloadSchema = z.object({
url: z.string(),
});
@@ -39,11 +54,25 @@ serve(async (req: Request) => {
const screenshot = await page.screenshot({
encoding: "binary",
- clip: { x: 0, y: 0, width: 400, height: 400 },
+ clip: { x: 0, y: 0, width: 1000, height: 1000 },
});
await browser.close();
- return new Response(screenshot, {
+ const screenshotArray = new Uint8Array(
+ typeof screenshot === "string"
+ ? Buffer.from(screenshot, "utf-8")
+ : screenshot
+ );
+
+ const result = await ImageMagick.read(screenshotArray, (img) => {
+ img.transparent(new MagickColor("white"));
+ img.trim(); // Remove any excess transparent space
+ img.sharpen(0, 1.0); // Subtle sharpening for better details
+ img.resize(300, 300);
+ return img.write((data) => data);
+ });
+
+ return new Response(result, {
headers: { ...corsHeaders, "Content-Type": "image/png" },
status: 200,
});