将base64转换为打字稿/Angular中的功能性JPEG

发布于 2025-02-06 15:26:00 字数 601 浏览 2 评论 0原文

我正在浏览器中的静态图像上运行cropperjs(从jpeg格式中从nodejs服务器检索),它返回了基本64中不同图像中的预览。我试图以原始的JPEG格式获取数据并将修改后的图像保存回服务器。 我已经尝试了一些不同的事情,但这是最新的:

saveCroppedImage(){  

     var split = this.imageDestination.split(','); // parsing out data:image/png;base64,
     var croppedImage = split[1];                  // assigning the base64 to a variable
     var blob = new Blob([croppedImage],{type: 'image/jpeg'}); //changing the base64->Blob
     var file = new File([blob],'cropped.jpeg');  //theoretically changing the blob->jpeg
     this.newCroppedImage = file;
}

然后,我将文件上传到服务器,并损坏。

I am running cropperjs on a static image in the browser(retrieved from a nodejs server in jpeg format), it returns a preview in a different image that is in base64. Im trying to take that data and save the modified image back to the server in the original jpeg format.
I've tried a few different things, but this is the latest:

saveCroppedImage(){  

     var split = this.imageDestination.split(','); // parsing out data:image/png;base64,
     var croppedImage = split[1];                  // assigning the base64 to a variable
     var blob = new Blob([croppedImage],{type: 'image/jpeg'}); //changing the base64->Blob
     var file = new File([blob],'cropped.jpeg');  //theoretically changing the blob->jpeg
     this.newCroppedImage = file;
}

I then upload the file to the server and it is corrupted.

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

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

发布评论

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

评论(1

無心 2025-02-13 15:26:00

好的,所以我在Interweb的一些帮助下弄清楚了:

这是我必须在文件创建中包含的信息,并且确实必须保持PNG才能工作。

public file = (theBlob: Blob): File =>
    {return new File([theBlob],'croppedImage.png',{lastModified:new 
    Date().getTime(), type:'image/png'}) 
 }

savecroppedimage(){

    var split = this.imageDestination.split(',');
    console.log(this.imageDestination)
    var croppedImage = split[1]
    this.blob = blobUtil.base64StringToBlob(croppedImage);
    this.newCroppedImage =this.file(this.blob);
    this.onFileChange();

}

Ok, so I figured it out with some help of the interweb:

This is the info I had to include with the file creation, and it did have to stay a png to work.

public file = (theBlob: Blob): File =>
    {return new File([theBlob],'croppedImage.png',{lastModified:new 
    Date().getTime(), type:'image/png'}) 
 }

saveCroppedImage() {

    var split = this.imageDestination.split(',');
    console.log(this.imageDestination)
    var croppedImage = split[1]
    this.blob = blobUtil.base64StringToBlob(croppedImage);
    this.newCroppedImage =this.file(this.blob);
    this.onFileChange();

}

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