微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在来自客户端的输入图像上使用Jimp

如何解决在来自客户端的输入图像上使用Jimp

我正在尝试通过一个简单的输入文件从React中获取图像,并在Express服务器上使用Jimp调整其大小,然后对其进行其他处理。问题是我无法使用Jimp读取图像,因为它需要String或Buffer或ArrayBuffer,并且输入图像是File类型。我已经尝试将文件转换为缓冲区,但没有成功。我该如何使Jimp毫无问题地阅读它?

我将在此处编写与该问题相关的代码,中间肯定还有其他事情不会妨碍Jimp,因为在调用之前文件是完整的Jimp.read()

反应

const onInputFile = (e) => {
  let file = e.target.files[0];
  const formData = new FormData();
  formData.append("signature",signature.file,signature.file.fileName);
  formData.append("id",props.art.id);
  axios
    .post("http://localhost:9000/createpdf",formData,{
      headers: {
        "Content-Type": "multipart/form-data",authorization: props.userData.token,},})
    .then(res => {
      console.log(res);
    })
    .catch(err => {
      console.log(err);
    });
};

return(
  <div>
  <input
    type="file"
    id="inputFileID"
    onChange={onInputFile}
    name="inputFileName"
  />
  </div>
);

快递

app.post("/createpdf",async (req,res) => {
  // I'm using formidable module to get the FormData from the request
  // Just skipping that part,it works,I'll call the file "myImageFile"
  
  res.status(200).json({ status: "ok" });

  await Jimp.read(myImageFile) // Error in this line
    .then(resultImg => {
      resultImg.scale()
        .getBufferAsync(Jimp.MIME_JPEG)
        .then(finalImg => {
          // Upload finalImg to Cloud
        })
        .catch(err => {
          console.log(err);
        });
    })
    .catch(err => {
      console.log(err);
    });
});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。