简体   繁体   English

在使用ngx-img-cropper的Angular中,如何获取裁剪的jpg或png文件,而不是base64图像

[英]In Angular using ngx-img-cropper, how do I get the cropped jpg or png file, not a base64 image

I'm using Angular (not AngularJS) and the ngx-img-cropper library. 我正在使用Angular(不是AngularJS)和ngx-img-cropper库。 Once the image is cropped, is there a way to get the cropped jpg or png file? 裁剪图像后,有没有办法获得裁剪的jpg或png文件? this.data.image inside of fileChangeListener($event) is a base64 image string 'data:image/png;base64,iVBORw0KGgo...'. fileChangeListener($ event)中的this.data.image是一个base64图像字符串'data:image / png; base64,iVBORw0KGgo ...'。 this.cropper.image[0] is an HTML Element object and this.cropper.image[1] is a base 64 image string. this.cropper.image [0]是一个HTML Element对象,this.cropper.image [1]是一个base 64图像字符串。

This is the ngx-img-cropper website https://github.com/web-dave/ngx-img-cropper . 这是ngx-img-cropper网站https://github.com/web-dave/ngx-img-cropper I'm trying to upload an image inside of fileChangeListener() about 3/4 of the way down the page. 我正在尝试在fileChangeListener()内部上传大约3/4的页面。

This is what worked for me. 这对我有用。 Installed file-saver using 'npm install file-saver --save'. 使用'npm install file-saver --save'安装文件保护程序。

import { saveAs } from 'file-saver';

fileChangeListener($event) {
  const image: any = new Image();
  const file: File = $event.target.files[0];

  const myReader: FileReader = new FileReader();
  const that = this;
  myReader.onloadend = function (loadEvent: any) {
    image.src = loadEvent.target.result;
    that.cropper.setImage(image);
  };
  myReader.readAsDataURL(file);
  const base64: string = Object.values(that.cropper.image)[1];
  const blob = this.dataURItoBlob(base64);
  saveAs(blob, 'croppedFilezz.png');
}

dataURItoBlob(dataURI) {
  const binary = atob(dataURI.split(',')[1]);
  const array = [];
  for (let i = 0; i < binary.length; i++) {
    array.push(binary.charCodeAt(i));
  }
  return new Blob([new Uint8Array(array)], {
    type: 'image/png'
  });
}

Where I got stuck: in the html don't forget #cropper inside of Credit to answer from @vava720 Convert Data URI to File then append to FormData 我被卡住的地方:在html中不要忘记来自@vava720内的#cropper来回答@vava720 将数据URI转换为File然后附加到FormData

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular ngx-img-cropper:如何获取上传图像的文件名? - Angular ngx-img-cropper: How do I get the filename of the uploaded image? Angular中如何将Heic图像文件转换为Jpeg或Png格式以供ngx-image-cropper使用? - How to convert a Heic image file to a Jpeg or Png in Angular for ngx-image-cropper to use? 如何在Angular 7中显示base64图像? - How do I display a base64 image in Angular 7? 通过 angular 6 应用程序中的 javascript 更改 base64 图像大小并下载不同扩展名(png、jpg、gif 等) - Change base64 image size and download with different extension(png, jpg, gif, etc…) by javascript in angular 6 application 如何将 SVG 文件转换为 NativeScript 中的 Base64String/JPG/PNG 以在 Img 标签中使用? - How to convert an SVG file to Base64String/JPG/PNG in NativeScript to use in Img Tag? 使用Angular 2从图像URL获取base64图像 - get base64 image from image url using angular 2 转变<img src="phone.png">到 Base64<img src="data:img/png;base64,...."> 通过编译应用程序,使应用程序立即加载图像 - Convert <img src="phone.png"> to Base64 <img src="data:img/png;base64,...."> by compiling the app, to make the image load instantly with the app 如何在Angular 7中获取Fusion图表的Base64图像URL - How to get Base64 image URL of Fusion chart in Angular 7 如何在Angular中将本地图像文件转换为base64? - How to convert local image file to base64 in Angular? 如何以角度调整base64图像的大小 - how to resize base64 image in angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM