精品专区-精品自拍9-精品自拍三级乱伦-精品自拍视频-精品自拍视频曝光-精品自拍小视频

網站建設資訊

NEWS

網站建設資訊

Angular中怎么利用ng-img-max調整瀏覽器中的圖片

本篇文章給大家分享的是有關Angular中怎么利用 ng-img-max 調整瀏覽器中的圖片,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

創新互聯公司服務項目包括沙洋網站建設、沙洋網站制作、沙洋網頁制作以及沙洋網絡營銷策劃等。多年來,我們專注于互聯網行業,利用自身積累的技術優勢、行業經驗、深度合作伙伴關系等,向廣大中小型企業、政府機構等提供互聯網行業的解決方案,沙洋網站推廣取得了明顯的社會效益與經濟效益。目前,我們服務的客戶以成都為中心已經輻射到沙洋省份的部分城市,未來相信會繼續擴大服務區域并繼續獲得客戶的支持與信任!

安裝

首先,使用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文件輸入框到組件的模板中,像這樣:

在組件類中添加方法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('? Oh no!', error);
  }
 );
}

圖片預覽

您可能想要預覽要上傳到用戶的圖像。 您可以使用FileReader對象執行此操作。 您還需要使用Angular的DomSanitizer來使其信任使用FileReader對象創建的base64編碼數據URI:

現在,我們的組件內容是這樣的。組件中有趣的新方法是getImagePreview:

//: 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('? Oh no!', error);
   }
  );
 }

 getImagePreview(file: File) {
  const reader: FileReader = new FileReader();
  reader.readAsDataURL(file);
  reader.onload = () => {
   this.imagePreview = reader.result;
  };
 }
}

在我們的模板中,我們可以使用sanitizer來顯示如圖像:

//: app.component.html

以上就是Angular中怎么利用 ng-img-max 調整瀏覽器中的圖片,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注創新互聯行業資訊頻道。


網頁標題:Angular中怎么利用ng-img-max調整瀏覽器中的圖片
URL標題:http://m.jcarcd.cn/article/jsdodh.html
主站蜘蛛池模板: 乱老熟300部视频 | 91福利免费 | 狠狠热精品免费视频 | 国产大片在线播放 | 成人午夜福利在线 | 97导航| 精品动漫一区二区 | 中文字幕日本不卡 | 国产在线精品 | 国产一区二区三四区 | 国产精品片在线观看 | 欧美最新a级 | 日韩亚洲国产高清 | 绿巨人短视频app | 国产精品视频免费 | 日本三级a∨在线 | 91人人澡人 | 99r精品亚洲 | 18分钟处破好 | 91看片网站免费看 | 国产女主播勾搭美团 | 国产精品综合色 | 国语视频二区 | 不卡中文字幕 | 日韩午夜免费免费 | 国产射精在线观看 | 99久热国产| 精品国偷自产 | 韩国日本香港三级 | 成人午夜网址 | 91综合视频| 精品e本大| 午夜一区二区免 | 日韩在线欧美 | 国产欧美精品一 | 国产精品观看在 | 91成人精品在 | 国产xxx| 日本高清一 | 成人国产精品日韩 | 日韩欧美在线观看 |