Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
angular4中图像上传中的图像压缩_Angular_Image Compression_Image Upload - Fatal编程技术网

angular4中图像上传中的图像压缩

angular4中图像上传中的图像压缩,angular,image-compression,image-upload,Angular,Image Compression,Image Upload,保存(事件:任意、类型、图像类型){ this.uploadImageFlag=true; const fileList:fileList=event.target.files; 如果(fileList.length>0){ 常量文件:文件=文件列表[0] this.files.set('files',file,file.name) const reader=new FileReader(); reader.onload=(事件:any)=>{ this.url2=event.target.re

保存(事件:任意、类型、图像类型){
this.uploadImageFlag=true;
const fileList:fileList=event.target.files;
如果(fileList.length>0){
常量文件:文件=文件列表[0]
this.files.set('files',file,file.name)
const reader=new FileReader();
reader.onload=(事件:any)=>{
this.url2=event.target.result;
this.upload=true;
}
reader.readAsDataURL(event.target.files[0]);
}
}

有许多三方模块用于此,不确定是否压缩图像,但您可以使用画布调整图像大小,并使用其数据URI导出相同的图像


如前所述,您可以看一看这里还有一个基本的

我为您的需要制作了这个库:

您在自述页面上有完整的示例。 如果您想了解如何使用它,这里有一个片段:


@组件({…})
导出类AppComponent{
构造函数(私有imageCompress:NgxImageCompressService){}
压缩文件(){
this.imageCompress.uploadFile().then({image,orientation})=>{
console.warn('Size before:',this.imageCompress.byteCount(结果));
这个.imageCompress.compressFile(图像,方向,50,50)。然后(
结果=>console.warn('Size after:',this.imageCompress.byteCount(结果));
);
});
}
}

按照下面的教程来实现压缩的目标


我有一个非常复杂的代码,我不想修改整个组件。如果第三方模块可以帮助我,请分享。另外,我很困惑调整大小和压缩是一样的吗?你可以检查一下这个照片压缩和照片调整大小是完全不同的两件事。压缩照片(jpg)时,您保持相同的图像大小,即像素的宽度和高度不会改变,但由于质量下降,文件大小会变小(以千字节为单位)(将其视为模糊小细节)。当调整照片的大小时,得到的图像大小在像素上是不同的,大多是较小的,但是它可能是大的。如果它符合你的需要,请考虑接受答案,它帮助社区,Shankice包,但是我正在使用另一种上传方法,所以它对我不起作用,因为我不能得到定位。对于@ Andrej:你现在得到了方向。