Comments (4)
I cannot reproduce this locally using Bun 1.0.35 via the oven/bun
(Debian 11) Docker container.
If you're still having problems, please provide a complete code sample and image that allows someone else to reproduce.
from sharp.
I cannot reproduce this locally using Bun 1.0.35 via the
oven/bun
(Debian 11) Docker container.If you're still having problems, please provide a complete code sample and image that allows someone else to reproduce.
This is the entire code
Just my small service to remove the background image and then trim it using sharp
import Fastify from "fastify";
import { removeBackground } from "@imgly/background-removal-node";
const fastify = Fastify({
// logger: true
});
import { Buffer } from "buffer";
import cors from "@fastify/cors";
import axios from "axios";
import { writeFileSync } from "fs";
import { join } from "path";
import { randomBytes } from "crypto";
import imageType from "image-type";
import { fileURLToPath } from "url";
import { dirname } from "path";
import sharp from "sharp";
import fs from "fs";
const __dirname = dirname(fileURLToPath(import.meta.url));
await fastify.register(cors, {
origin: "*",
});
const downloadImage = async (url: string) => {
const response = await axios.get(url, { responseType: "arraybuffer" });
const buffer = Buffer.from(response.data, "binary");
// Get the image type from the buffer
const type = await imageType(buffer);
console.log("type", type);
// If the image type couldn't be detected, throw an error
if (!type) {
throw new Error("Could not detect image type");
}
// Generate a random filename with the correct extension
const filename = randomBytes(16).toString("hex") + "." + type.ext;
// Full path of the image
const imagePath = join(__dirname, "backgroundImages", filename);
// Save the image
writeFileSync(imagePath, buffer);
// Return the full path of the image
return imagePath;
};
// Declare a route
fastify.post("/remove-background", async function handler(request, reply) {
console.log("search");
const body = request.body as any;
const imageUrl = body.imageUrl;
const imageId = body.imageId;
console.log("body", body);
if (!imageUrl && !imageId) {
console.log("Invalid Request", "if (!imageUrl || !imageId) {");
return reply.code(400).send("Invalid Request");
}
if (!imageUrl.startsWith("http")) {
console.log("Invalid Request", "if (!imageUrl.startsWith('http')) {");
return reply.code(400).send("Invalid Request");
}
let result: Blob | undefined;
if (imageUrl) {
try {
//download the image, and save it to a file in backgroundImages folder
const data = await downloadImage(imageUrl);
console.log("removeBackground(data, {");
result = await removeBackground(data, {
debug: true,
output: { format: "image/png", quality: 1 },
});
//remove the image file
fs.unlinkSync(data);
} catch (e) {
console.log("err await removeBackground(imageUrl);", e);
return reply.code(400).send("Invalid Request");
}
}
console.log("theResultDone");
if (!result) {
return reply.code(400).send("Invalid Request");
}
const buffer = await result.arrayBuffer();
let theBase64 = Buffer.from(buffer).toString("base64"); // you can ignore this, it's just the part of the code before I implement sharp
// console.log("base64Image", theBase64); // This will log the base64 representation of the image
//convert result to base64 png
// theBase64 = `data:image/png;base64,${theBase64}`;
// console.log("theBase64", theBase64);
console.log("sharpingsharpingsharping");
const theSharp = await sharp(buffer).trim().toBuffer();
console.log("theSharp done");
theBase64 = theSharp.toString("base64");
theBase64 = `data:image/png;base64,${theBase64}`;
const data = {
base64: theBase64,
};
// console.log("data", data);
return reply.code(200).send(data);
});
// Run the server!
try {
await fastify.listen({ port: 5832 });
console.log(`server listening on ${fastify.server.address().port}`);
} catch (err) {
fastify.log.error("ulalafast", err);
process.exit(1);
}
from sharp.
Please can you provide a more minimal code sample that allows someone else to reproduce. For example, please remove all networking code, and please remove the other dependencies such as @imgly/background-removal-node
.
Perhaps create a complete, minimal, standalone repo that makes it as simple as possible for someone else to reproduce.
from sharp.
I've just spotted that @imgly/background-removal-node
depends on an old version of sharp and will not work on Bun.
Support for Bun was added in sharp v0.33.0.
from sharp.
Related Issues (20)
- Image stream hangs in Bun v1.1.3 HOT 2
- use last version, i get empty slider HOT 4
- "extend" May Fail for Animated TIFF HOT 1
- Adjust quality based on output file size HOT 2
- Error "text: invalid value (typically too big) for the size of the input" HOT 10
- Images arent closed after transforms HOT 1
- Issue setting a color of an element in an SVG HOT 5
- Cannot use custom fonts "Fontconfig error: Cannot load default config file: No such file: (null)" HOT 2
- Remove white pixels to have a transparent background HOT 2
- rotate().extract() resulting in bad extract area for iOS images rotated by metadata HOT 2
- Question: Why does the result of image.raw().extractChannel('red').toBuffer.length != image.width * image.height? HOT 3
- How to change OwnerName by withExifMerge? HOT 3
- Cross-compilation broken on [email protected] and 10.5.0 HOT 7
- Problems creating sharp Lambda layer package with custom libvips HOT 6
- What is the recommended hardware to run highly available production setup HOT 3
- Sharp keeps input file locked HOT 1
- .trim() removes 1 too many pixels HOT 5
- .trim().toBuffer() does not produce info response Object offset properties HOT 2
- Include trimOffsetRight & trimOffsetBottom HOT 2
- Add settings for symmetric .trim() HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sharp.