0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Angular】画像を縮小しつつ上下中央の正方形にトリミングして保存する

Last updated at Posted at 2022-01-07

プロフィールのアイコンとかを保存したい時に容量削減しつつ、中央部分だけトリミングしたい…ということがあったので作ったサンプル。

スクリーンショット 0004-01-07 11.24.34.png

app.component.html
<div class="container">
  <div class="row">
    <div class="col">
      <label for="iconUploadForm" class="btn grey">画像をアップロード</label>
      <input id="iconUploadForm" type="file" accept="image/*" (change)="onChangeFileInput($event)" />
      </div>
    <div class="col">
      <button (click)="save()" class="btn blue">保存</button>
    </div>
  </div>
  <div id="canvas-box"></div>
</div>
app.component.ts
  onChangeFileInput(event:any){
    const file = event.target.files[0];
    if(!file){
      return;
    }
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = function () {
      // 縮小・トリミング後のサイズのキャンバスを作成
      const canvas = document.createElement('canvas');
      canvas.id = 'icon-canvas'
      const ctx = canvas.getContext('2d');
      const trimSize = 128; // 縮小後のサイズ
      canvas.width = trimSize; 
      canvas.height = trimSize;
      canvas.style.width = 100+'px'; // cssでの表示サイズ
      canvas.style.height = 100+'px';

      if(ctx){
        const image = new Image();
        image.src = reader.result as string;
        image.onload = () =>{
          let height = 0;
          let width = 0;
          let xOffset = 0;
          let yOffset = 0;
          if (image.width > image.height) {// 横長画像
            height = trimSize;
            width = image.width * (trimSize / image.height);
            xOffset = -(width - trimSize) / 2;
            yOffset = 0;
          } else { // 縦長画像
            width = trimSize;
            height = image.height * (trimSize / image.width);
            yOffset = -(height - trimSize) / 2;
            xOffset = 0;
          }
          // キャンバスに描画
          ctx.drawImage(image,xOffset,yOffset,width,height);        
        }
      }
      const div = document.getElementById('canvas-box');
      if(div){
        // キャンバスを追加
        div.appendChild(canvas);
      }
    }
  }

  save(){
    const canvas = document.getElementById('icon-canvas') as HTMLCanvasElement;
    if(canvas){
      const data = canvas.toDataURL();
      console.log(data);
    }
  }

128で画像は作成しているけど、画面表示上では100pxで表示している例
540kbくらいあった画像で128px四方のアイコンを作って、53kbくらいになっていました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?