你想在Angular应用程序中进行图片上传,是否想在图片上传之前在前端限制上传图片的尺寸?ng2-img-max模块正是你所要的! ng2-img-max模块会使用web sorkers 进行图片大小的计算,并驻留在主线程中。
我们来看看他的用途:
安装
首先,使用npm 或 Yarn安装模块:
$ npm install ng2-img-max blueimp-canvas-to-blob --save # or Yarn : $ yarn add ng2-img-max blueimp-canvas-to-blob
blueimp-canvas-to-blob是一个polyfill,以便canvas.toBlob()可以在Safari和旧版本的Internet Explorer等浏览器上使用。
将polyfill脚本包含在项目中。 如果您使用Angular CLI,您可以将脚本添加到.angular-cli.json文件中:
//: .angular-cli.json ... "scripts": [ "../node_modules/blueimp-canvas-to-blob/js/canvas-to-blob.min.js" ], //...
将脚本添加到Angular CLI配置后,您将需要重新启动本地服务。
现在我们将模块导入应用模块或功能模块:
//: app.module.ts
//...
import { Ng2ImgMaxModule } from 'ng2-img-max';
@NgModule({
declarations: [ AppComponent ],
imports: [
//...
ng2ImgMaxModule
],
providers: [],
bootstrap: [ AppComponent ]
})
export class AppModule {}
最后,ng2-img-max服务可以导入并注入到这样的组件中:
import { Component } from '@angular/core';
import { Ng2ImgMaxService } from 'ng2-img-max';
@Component({ ... })
export class AppComponent {
constructor(private ng2ImgMax: Ng2ImgMaxService ) {}
}
使用
我们添加一个File文件输入框到组件的模板中,像这样:
<input type='file' (change)="onImageChange($event)" accept="image/*" />
在组件类中添加方法onImageChange, 它将会限制图片的宽高为:400px,300px:
updateImage: Blob;
constructor(private ng2ImgMax: Ng2ImgMaxService) {}
onImageChange(event){
let image = event.target.files[0];
this.ng2ImgMax.resizeImage(image,400,300).subscribe(result=> {
this.uploadImage = result;
},
error=> {
console.log('error:',error);
})
}
如果您有多个图像需要一次性调整大小,请改用resize方法,并将图片文件数组作为第一个参数传入。
结果是Blob类型,但是如果需要,可以使用File构造函数将其转换为正确的文件:
//: app.component.ts
uploadedImage: File;
constructor(private ng2ImgMax: Ng2ImgMaxService) {}
onImageChange(event){
let image = event.target.files[0];
this.ng2ImgMax.resizeImage(image,400,300).subscribe(result=> {
this.uploadedImage = new File([result],result.name);
},
error=> {
console.log('error',error);
})
}
您现在可以将文件上传到您的后端。 不要忘记在后端做好验证,因为这里的内容会阻止一些用户将超大或非图像文件直接上传到后端。
只限制宽度或高度
假设您只想将高度限制为300px,并相应调整宽度,以保持宽高比相同。 只要设置任何一阀值到10000:
//...
onImageChange(event) {
let image = event.target.files[0];
this.ng2ImgMax.resizeImage(image,10000,300).subscribe(result=> {
this.uploadedImage = new File([result],result.name);
},
error=> {
console.log('error:',error);
});
}
压缩代替Resizing
您还可以使用compress或compressImage方法执行有损压缩,而不是调整图像大小。 只需传递最大值(兆字节)。 你显然想要运行一些测试,看看你想要走多远的几个小时,同时保持图像看起来很好。
在以下示例中,我们将生成的图像限制为大约75Kb:
onImageChange(event) {
let image = event.target.files[0];
this.ng2ImgMax.compressImage(image, 0.075).subscribe(
result => {
this.uploadedImage = new File([result], result.name);
this.getImagePreview(this.uploadedImage);
},
error => {
console.log('"htmlcode">
//: app.component.ts
import { Component } from '@angular/core';
import { Ng2ImgMaxService } from 'ng2-img-max';
import { DomSanitizer } from '@angular/platform-browser';
@Component({ ... })
export class AppComponent {
uploadedImage: File;
imagePreview: string;
constructor(
private ng2ImgMax: Ng2ImgMaxService,
public sanitizer: DomSanitizer
) {}
onImageChange(event) {
let image = event.target.files[0];
this.ng2ImgMax.resizeImage(image, 10000, 375).subscribe(
result => {
this.uploadedImage = new File([result], result.name);
this.getImagePreview(this.uploadedImage);
},
error => {
console.log('"htmlcode">
//: app.component.html
<img
*ngIf="imagePreview"
[src]="sanitizer.bypassSecurityTrustUrl(imagePreview)">
这就是它的全部! 您还可以查看同一作者的ng2-img-tools包,以获得更多的浏览器端图像处理(如裁剪)。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]