我正在使用Nodejs Sharp将png图像转码/调整为jpg.有没有办法用白色(或其他浅色)代替透明而不是黑色?我找到了一个较旧的库的解决方案,但夏普似乎是最快和最好的.
.background不起作用
.then(data => Sharp(data.Body).resize(SIZES[resize_type].width, SIZES[resize_type].height).max().withoutEnlargement().background("white").toFormat('jpeg') .toBuffer())
Run Code Online (Sandbox Code Playgroud) 运行时sudo npm i sharp --save出现以下错误:
> sharp@0.21.3 install /home/server/node_modules/sharp
> (node install/libvips && node install/dll-copy && prebuild-install) || (node-gyp rebuild && node install/dll-copy)
info sharp Using cached /home/ronny/.npm/_libvips/libvips-8.7.0-linux-x64.tar.gz
ERR! sharp EACCES: permission denied, mkdir '/home/server/node_modules/sharp/vendor'
info sharp Attempting to build from source via node-gyp but this may fail due to the above error
info sharp Please see https://sharp.pixelplumbing.com/page/install for required dependencies
gyp ERR! configure error
gyp ERR! stack Error: EACCES: permission denied, mkdir '/home/server/node_modules/sharp/build'
gyp ERR! System Linux …Run Code Online (Sandbox Code Playgroud) 我从外部获取缓冲区数据到我的程序,我想处理缓冲区数据并将其作为缓冲区发送。所以我不想将缓冲区转换为图像。我怎样才能做到这一点?
我尝试这种方式,但它不起作用。
const process = await sharp(incoming_buffer_data).grayscale();
Run Code Online (Sandbox Code Playgroud)
fs.writeFileSync('test.jpg', process);// 我正在使用它进行测试。总是我得到一个错误的图像格式
我目前正在尝试使用 Axios 下载图像,然后调整结果大小并通过 GraphQL 解析器中的 Node 将其保存在本地。
这是我正在使用的代码块:
axios.get(url)
.then((response) => {
const { set, collector_number } = response.data;
const sourceUrl = response.data.image_uris.border_crop;
const filename = `${set}/${collector_number}.png`;
axios.get(sourceUrl, { responseType: 'arraybuffer' })
.then((res) => {
console.log(`Resizing Image!`)
sharp(res)
.resize(226, 321)
.toFile(`../cardimg/${filename}`)
.then(() => {
console.log(`Image downloaded and resized!`)
})
.catch((err) => {
console.log(`Couldn't process: ${err}`);
})
})
})
Run Code Online (Sandbox Code Playgroud)
当我执行代码(通过 GraphQL Mutation)时,它会抛出一个错误,指出:Input file is missing。
不确定这是 Axios 的误用,还是我对 Sharp 的操作有问题。
有什么建议么?我最初担心我需要弄乱来自 HTTP 请求的响应的格式,但从我收集到的信息来看,我做得正确。
提前致谢!
我已经使用 console.log 来确保它确实抓取了图像并且 URL 是正确的,因此已经经过测试,因此 …
我需要使用 Sharp 的帮助,我希望在上传时调整图像大小,但我似乎无法做到这一点。
router.post("/", upload.single("image"), async (req, res) => {
const { filename: image } = req.file;
await sharp(req.file.path)
.resize(300, 200)
.jpeg({ quality: 50 })
.toFile(path.resolve(req.file.destination, "resized", image));
fs.unlinkSync(req.file.path);
res.send("sent");
});
Run Code Online (Sandbox Code Playgroud) 我正在尝试迁移到 S3,现在我通过 URL 向其他主机提供图像,我可以下载它,在使用时更改它的大小(以节省大小并更快地提供它们),然后将其上传到 S3在我的机器上保存副本吗?
我在 lambda 节点函数中使用锐节点 ( https://www.npmjs.com/package/sharp ) 来转换、裁剪和白色背景图像
我可以容忍各种输入格式,但图像输出格式应该始终是jpg。
问题是,在某些情况下,sharp 不会将图像转换为 jpg,当它发生时总是使用 png 图像,它并不总是发生,另一个问题是 Sharp 不会抛出异常,即错误是“无声的”。
不幸的是我没有原始图像,因为我没有保存此信息,下面是代码:
对于作物:
image.extract(offset)
.resize(width, height)
.toFormat('jpeg')
.jpeg({
quality: 100,
chromaSubsampling: '4:4:4'
})
.toBuffer();
Run Code Online (Sandbox Code Playgroud)
放置白色背景:
image.flatten(true)
.resize(width, height)
.background(backgroundColor.white)
.embed()
.toFormat('jpeg')
.jpeg({
quality: 100,
chromaSubsampling: '4:4:4'
})
.toBuffer();
Run Code Online (Sandbox Code Playgroud)
其他案例:
image.flatten(true)
.resize(width, height)
.background(backgroundColor.white)
.toFormat('jpeg')
.jpeg({
quality: 100,
chromaSubsampling: '4:4:4'
})
.toBuffer();
Run Code Online (Sandbox Code Playgroud)
我搜索了这个问题的答案,但仍然找不到,有人经历过这个吗?
sharp ×7
node.js ×6
amazon-s3 ×1
axios ×1
image ×1
javascript ×1
jpeg ×1
multer ×1
node-fetch ×1
npm ×1
transparency ×1