[JavaScript]画像の寸法を取得する

Javascript

この例では、画像のサイズを取得するJavaScriptプログラムの作成方法を学習します。

例:画像の寸法を取得する

// program to get the dimension of an image

const img = new Image();

// get the image
img.src = '//cdn.programiz.com/sites/tutorial2program/files/cover-artwork.png';

// get height and width
img.onload = function() {
  console.log('width ' + this.width)
  console.log('height '+ this.height);
}

出力

width 1040
height 922

上記のプログラムでは、 new Image()コンストラクターを使用して画像オブジェクトを作成します。

Image()コンストラクターは、新しい画像要素インスタンスを作成します。

次に、 img.srcを使用して、画像のURLソースを使用して画像を追加します。

img.onload()関数は、画像の高さと幅にアクセスするために使用されます。

Source

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です