ここで見つけた解決策に従って画像をメモリに読み込んでいますが、それは問題ありませんが、コールバック内にあるため、プロパティの幅と高さはその外部では定義されていません。
var originalWidth, originalHeight;
$("<img/>") // Make in memory copy of image to avoid css issues
.attr("src", $(img).attr("src"))
.load(function() {
originalWidth = this.width; // Note: $(this).width() will not
originalHeight = this.height; // work for in memory images.
});
console.log(originalWidth); // undefined obviously since `this` is only accessible within `load`s callback.
私は多分それを次のようなオブジェクト内に置くことを考えました:
var originalWidth, originalHeight;
var imgDimensions = {};
$("<img/>") // Make in memory copy of image to avoid css issues
.attr("src", $(img).attr("src"))
.load(function() {
imgDimensions.originalWidth = this.width; // Note: $(this).width() will not
imgDimensions.originalHeight = this.height; // work for in memory images.
});
console.log(imgDimensions['originalWidth']); // undefined also
load()内からこれらのプロパティを返すにはどうすればよいですか?