ImageAreaSelectをWebサイトの例で呼び出し、クロップ選択の適切なスケーリングの元の画像サイズパラメータを取得するのに苦労しています。使用可能なクロップ領域はコンテナの全幅です(私はむしろイメージの幅になります)。そして、私はcssでmax-widthパラメータを使用して、ロードされたファイルの幅をコンテナよりも小さく制限しました。セレクタをリサイズする例はたくさんありますが、元の幅と高さを取得するにはどうすればよいですか?私は別のキャンバスで後でjsピクセル操作のために開いたファイル名を正常に保存していますが、大きすぎるイメージが読み込まれた場合は、選択結果を再スケーリングする必要があります。ファイルを開くときと呼び出したときに元の画像サイズを取得する方法imageareaselect
HTML:
<div class="container demo">
<div style="float: left; width: 50%;">
<div class="frame" style="margin: 0 0.3em; width: 800px; height: 800px;">
<input type="file" id="file"/>
<br />
<div id="view"> Your image will load here</div>
</div>
</div>
</div>
CSS:
.imgContainer img { max-width: 600px;}
JS:
function preview(img, selection) {
if (!selection.width || !selection.height)
return;
// or do some conditional scaling here if image is large
x_1 = selection.x1; //I use these globals elsewhere for pixel calcs
y_1 = selection.y1;
S_wid = selection.width;
S_hei = selection.height;
L = Math.floor((S_wid * S_hei)/4096); //4-bit ave color density
}
$(function() {
$('#view').imgAreaSelect({
handles: true,
fadeSpeed: 200,
// imageHeight: originalHeight, //how to get these, or do elsewhere?
// imageWidth: originalWidth,
onSelectChange: preview
});
});
$(window).load(function() {
$('#file').change(function() {
var oFReader = new FileReader();
oFReader.readAsDataURL(this.files[0]);
console.log(this.files[0]);
oFReader.onload = function (oFREvent) {
$('#view').html('<img src="' + oFREvent.target.result + '">');
fname = oFREvent.target.result; //global, used later elsewhere
// some way to get original image sizes here, or do elsewhere?
};
});
});
私が提案ソリューションの多くをしようとしてきたが、それに何かを得ることができていません正しく機能します。 .width()
js関数の呼び出しは、コンテナの幅を返します。 .naturalWidth
のコールは、src="some file"
というよく定義されたimg IDを持っていない限り、定義されていません。getElementById
jqueryでこのように開かれたファイルのimg IDを取得または定義する方法がわかりません。うまくいけば、私はここで紛失している単純なものがあります。助けを前にありがとう。
このアプローチは、私がアクセスしようとしていた元の高さと幅のパラメータを提供します。この情報にアクセスするためのシンプルで直接的なコード改訂をありがとう! –