尖锐无法读取文件缓冲区

发布于 2025-02-14 00:16:57 字数 1034 浏览 2 评论 0 原文

我正在使用 express-fileupload 用于读取API的文件。现在,我想使用 Sharp 在请求主体中处理图像。

我不想首先将文件保存在服务器上,并使用 fs.ReadFilesync 进行处理。

我尝试传递 req.files.image.data ,该应该是一个缓冲区。

  const image = await sharp(Buffer.from(req.files.image.data))
    .resize(500, 500)
    .jpeg({ quality: 10 })
    .toBuffer()
    .then((outputBuffer) =>
      ({ data: outputBuffer, mimetype: 'image/jpeg' }))
    .catch(err => {
      console.log(err);
      return null;
    });

但是它正在抛出错误此错误: [错误:vipsjpeg:输入文件的过早末端]

当我尝试将图像缓冲区数据转换为 post ,将其转换为使用 buffer.from 然后通过它,然后传递错误: [error:input buffer包含不支持的映像格式]

edit> edit :有一个:有一个:限制图像大小5MB,这就是为什么大于该图像在缓冲区中没有完全捕获的图像的原因,因此,此错误。

app.use(fileUpload({
  limits: { fileSize: 50 * 1024 * 1024 },
}));

I'm using express-fileupload for reading files from the API. Now I want to process the image in the request body using Sharp.

I don't want to first save the file at the server and process it using fs.readFileSync.

I tried passing req.files.image.data which is supposed to be a buffer.

  const image = await sharp(Buffer.from(req.files.image.data))
    .resize(500, 500)
    .jpeg({ quality: 10 })
    .toBuffer()
    .then((outputBuffer) =>
      ({ data: outputBuffer, mimetype: 'image/jpeg' }))
    .catch(err => {
      console.log(err);
      return null;
    });

But it is throwing error this error: [Error: VipsJpeg: Premature end of input file]

When I tried converting the image buffer data into string as suggested in this post, converting it into buffer using Buffer.from and then passing it, it throwing error: [Error: Input buffer contains unsupported image format]

Edit: There was a limit on image size 5mb, that's why the images greater than that were not getting completely captured in the buffer, hence, this error.

app.use(fileUpload({
  limits: { fileSize: 50 * 1024 * 1024 },
}));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九公里浅绿 2025-02-21 00:16:58

当我看到您的代码时,会出现一些问题。让我尝试通过提出一些问题并提供提示,以更接近可能的解决方案:

  • 您确定输入文件不是损坏的格式吗?它可能不符合JPEG规格的要求吗?如果您可以确定格式正确,请尝试下一步。...
  • 如果 req.files.image.data 确实是一个缓冲区,为什么您尝试通过使用再次生成缓冲区 buffer.from(req.files.image.data)?您想从缓冲区创建缓冲区吗?
  • 顺便说一句 - 我再次尝试使用 .tobuffer()进行缓冲区的转换。来自已经存在的缓冲区?在这种情况下,我从个人经验中讲话,如果试图从已经存在的缓冲区创建缓冲区,夏普会出现错误。
  • 您提到 req.files.image.data 应该是缓冲区。听起来也许您不是100%确定的。我建议检查您是否真的使用
    const isitreallyBuffer = buffer.isbuffer(req.files.image.data)
    之后,您可以简单地将其打印到控制台: console.log(IsitreallyBuffer); //
  • 最终,如果将字符串,缓冲区或某些类型的数组作为输入提供,则不应产生很大的不同。按照文档,夏普在输入数据方面非常灵活,并且接受缓冲区,Uint8array,uint8array,uint8array,uint8array,uint8array,uint8array, Uint8ClampedArray,Int8Array,Uint16Array,Int16Array,Uint32array, int32array,float32array,float64array和如下提到的字符串。
  1. 也许再检查提供的输入 req.files.image.data 的类型。它真的是一个未损坏的图像文件吗?它是否真的符合上面列出的夏普接受的输入选项?
  2. 如果是的,请尝试尝试 const image =等待夏普(req.files.image.data)...
  3. 如果您已经使用了缓冲区,请删除 .tobuffer()

修正案
fs.ReadFileSync 通常用于处理图像文件。在这种情况下,我从个人经验中谈论了在Node.js中使用图像文件的许多天的经验,我会考虑使用FS,并且更好地更喜欢敏锐的包装来读取和编写图像文件。我不再使用FS。 FS是串联的图像块,进而增加了导致记忆猪的概率。

您只需在桌面上使用WordPad或记事本打开PNG图像,然后搜索IDAT块。然后,使用FS软件包处理相同的图像,您将看到差异。突然之间,您可能只有一个非常巨大的IDAT块。

Some questions arise, when I see your code. Let me try to get closer to a possible solution with your provided input by asking some questions and providing hints:

  • Are you sure your input file is not a corrupted format? It does not meet the requirements of jpeg specs probably? If you can be sure that the format is correct, try the next steps....
  • If req.files.image.data is really a buffer, why do you try to generate a buffer again by using Buffer.from(req.files.image.data)? You want to create a buffer from a buffer?
  • By the way - I line 4 you again try to conduct a conversion to a buffer with .toBuffer(). From an already existing buffer? In this case, I speak from personal experience, sharp would throw an error if trying to create a buffer from an already existing buffer.
  • You mention req.files.image.data is supposed to be a buffer. Sounds maybe you are not 100% sure. I suggest to check if you really have a buffer by using
    const isItReallyBuffer = Buffer.isBuffer(req.files.image.data)
    After that you can simply print it to the console: console.log(isItReallyBuffer); // true or false
  • In the end, it shouldn't make a big difference if a string, a buffer or certain kinds of arrays are provided to sharp as input. As per documentation, Sharp is very flexible when it comes to input data and accepts Buffer, Uint8Array, Uint8ClampedArray, Int8Array, Uint16Array, Int16Array, Uint32Array, Int32Array, Float32Array, Float64Array and as mentioned a string.
  1. Maybe check the type of your provided input req.files.image.data one more time. Is it really a not corrupted image file? Does it really comply with the input options accepted by Sharp listed above?
  2. If yes, rather try const image = await sharp(req.files.image.data)...
  3. If you already use a buffer, remove .toBuffer()

AMENDMENT
fs.readFileSync is often used to process image files. In this case, I speak from my personal experience of many days of working with image files in node.js, I would think about using fs and better prefer the Sharp package for reading and writing image files. I don't use fs anymore. Fs is concatenating image chunks which in turn increases the probability leading to memory hogs.

You can simply open an PNG image on your desktop with WordPad or Notepad and search for IDAT chunks. Then, process the same image with fs package and you will see the difference; all of a sudden you likely have only one very huge IDAT chunk.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文