<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當前位置: 首頁 - 科技 - 知識百科 - 正文

        Angular使用 ng-img-max 調整瀏覽器中的圖片的示例代碼

        來源:懂視網 責編:小采 時間:2020-11-27 22:32:37
        文檔

        Angular使用 ng-img-max 調整瀏覽器中的圖片的示例代碼

        Angular使用 ng-img-max 調整瀏覽器中的圖片的示例代碼:你想在Angular應用程序中進行圖片上傳,是否想在圖片上傳之前在前端限制上傳圖片的尺寸?ng2-img-max模塊正是你所要的! ng2-img-max模塊會使用web sorkers 進行圖片大小的計算,并駐留在主線程中。 我們來看看他的用途: 安裝 首先,使用npm 或 Y
        推薦度:
        導讀Angular使用 ng-img-max 調整瀏覽器中的圖片的示例代碼:你想在Angular應用程序中進行圖片上傳,是否想在圖片上傳之前在前端限制上傳圖片的尺寸?ng2-img-max模塊正是你所要的! ng2-img-max模塊會使用web sorkers 進行圖片大小的計算,并駐留在主線程中。 我們來看看他的用途: 安裝 首先,使用npm 或 Y

        你想在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('😢 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
        
        <img
         *ngIf="imagePreview"
         [src]="sanitizer.bypassSecurityTrustUrl(imagePreview)">
        

        這就是它的全部! 您還可以查看同一作者的ng2-img-tools包,以獲得更多的瀏覽器端圖像處理(如裁剪)。

        聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        Angular使用 ng-img-max 調整瀏覽器中的圖片的示例代碼

        Angular使用 ng-img-max 調整瀏覽器中的圖片的示例代碼:你想在Angular應用程序中進行圖片上傳,是否想在圖片上傳之前在前端限制上傳圖片的尺寸?ng2-img-max模塊正是你所要的! ng2-img-max模塊會使用web sorkers 進行圖片大小的計算,并駐留在主線程中。 我們來看看他的用途: 安裝 首先,使用npm 或 Y
        推薦度:
        標簽: 圖片 使用 瀏覽器
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 国产精品va无码免费麻豆| 毛片高清视频在线看免费观看| yy6080久久亚洲精品| 亚洲av日韩综合一区二区三区| 永久中文字幕免费视频网站| 亚洲欧美日本韩国| 四虎永久免费地址在线观看| 国产亚洲精品91| 又粗又黄又猛又爽大片免费| 老司机免费午夜精品视频| 亚洲国产av无码精品| 久久国产精品免费一区| 亚洲AV综合色区无码一区| 67pao强力打造高清免费| 亚洲伊人久久大香线焦| 嫩草影院免费观看| 性生大片视频免费观看一级| 亚洲va久久久噜噜噜久久狠狠| 97av免费视频| 亚洲熟妇无码AV不卡在线播放| 免费国产成人午夜电影| 在线观看免费播放av片| 亚洲妓女综合网99| 日韩激情无码免费毛片| 乱淫片免费影院观看| 国精无码欧精品亚洲一区| 蜜臀AV免费一区二区三区| 亚洲午夜理论片在线观看| 免费观看四虎精品国产永久| 中文字幕在线成人免费看| 中文字幕亚洲免费无线观看日本 | 老司机免费午夜精品视频| 亚洲欧洲日产国码无码久久99| 久久精品国产免费观看三人同眠| 亚洲精品伊人久久久久| 亚洲精品麻豆av| 1000部羞羞禁止免费观看视频| 337P日本欧洲亚洲大胆艺术图| 亚洲V无码一区二区三区四区观看 亚洲αv久久久噜噜噜噜噜 | 亚洲精品和日本精品| 毛片免费全部播放无码|