admin管理员组文章数量:1247288
I tried to use Sharp image processing in my create react app. But it sends an error
This is my code:
import sharp from 'sharp';
const buffer = await sharp(req.file.buffer)
.resize({ width: 250, height: 250 })
.extend({
top: 10,
bottom: 10,
left: 10,
right: 10,
background: '#d7d8de'
})
.png()
Edit: I’m using Node.js version 12.18.0
I tried to use Sharp image processing in my create react app. But it sends an error
This is my code:
import sharp from 'sharp';
const buffer = await sharp(req.file.buffer)
.resize({ width: 250, height: 250 })
.extend({
top: 10,
bottom: 10,
left: 10,
right: 10,
background: '#d7d8de'
})
.png()
Edit: I’m using Node.js version 12.18.0
Share Improve this question edited Apr 4, 2023 at 9:06 ChimdumebiNebolisa 3132 silver badges11 bronze badges asked Nov 2, 2021 at 16:12 RupamRupam 1151 gold badge2 silver badges10 bronze badges 02 Answers
Reset to default 9No.
Sharp uses the node.js runtime as well as binary executable code that is installed on the host system. All this means that Sharp does not work in a web browser.
I see that somebody already provided a good answer, but more alternatives can't hurt
For the simple task of resizing an image that I had to do in a service worker for a Chrome V3 extension, after many tries with canvases I found that image-js easily did the trick.
Here is a snippet for what I did:
import { Image } from 'image-js';
export const downsizeImage = async (
imageBase64: string
): Promise<string> => {
const blob = await fetch(imageBase64).then(r => r.blob());
const buffer = await blob.arrayBuffer();
const image = await Image.load(buffer);
const resizedImage = image.resize({ width: 200, height: 200 });
const imgBuffer = resizedImage.toBuffer();
// convert to base64
return Buffer.from(imgBuffer)
.toString('base64');
}
本文标签: javascriptCan I use Sharp image processing library in React jsStack Overflow
版权声明:本文标题:javascript - Can I use Sharp image processing library in React js? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740280761a2253781.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论