如何对多个图像进行角度压缩?在这里,我附上了一次只能压缩一个图像的代码



如何以角度压缩多个图像?在这里,我附上了一次只能压缩一个图像的代码,帮助我解决这个问题。我不知道如何对多个图像进行循环,我没有得到任何关于"ngx图像压缩"的文档

import {Component} from '@angular/core';
import {NgxImageCompressService} from 'ngx-image-compress';

@Component({
selector: 'app-root',
template: `
<div>
<button (click)="compressFile()">Upload and compress Image</button>
<img *ngIf="imgResultBeforeCompress" [src]="imgResultBeforeCompress" alt="">
<img *ngIf="imgResultAfterCompress" [src]="imgResultAfterCompress" alt="">
</div>
`
})
export class AppComponent {
constructor(private imageCompress: NgxImageCompressService) {}

imgResultBeforeCompress:string;
imgResultAfterCompress:string;
compressFile() {

this.imageCompress.uploadFile().then(({image, orientation}) => {

this.imgResultBeforeCompress = image;
console.warn('Size in bytes was:', this.imageCompress.byteCount(image));

this.imageCompress.compressFile(image, orientation, 50, 50).then(
result => {
this.imgResultAfterCompress = result;
console.warn('Size in bytes is now:', this.imageCompress.byteCount(result));
}
);

});

}
}

我不确定我是否答对了,希望以下其中一个能有所帮助:

  • 根据文档(https://github.com/dfa1234/ngx-image-compress#readme),似乎一次只能选择一个文件。因此,如果你想让用户上传多个文件,每次用户按下按钮上传时,都要压缩图像,并将结果保存在图像列表中。之后,您可以循环查看图像列表,并执行您想要的任何操作。

  • 如果您只想将此库用于压缩。您可以使用类型文件的普通输入,然后当使用选择图像时,对每个图像运行this.imageCompress.compressFile以获得压缩图像。

有一个更新,现在您可以使用uploadMultipleFiles()方法在ngx-image-compress中一次上载多个文件。

你可以在这里运行演示https://stackblitz.com/edit/ngx-compress-sample

uploadMultipleFiles() {
this.imageCompress.uploadMultipleFiles().then((multipleFiles: UploadResponse[]) => {
console.warn(`${multipleFiles.length} files selected`);
});
}

最新更新