アロハ、
これらの情報に基づいて、プログラムでJavascriptで画像の縦横比が適切かどうかを判断/計算するにはどうすればよいですか?
例: 以下は問題ありません:
Width = 570px
Height = 520px
Ratio = 10
Aspect = 57:52
これは良くありません:
Width = 815px
Height = 85px
Ratio = 5
Aspect = 163:17
アロハ、
これらの情報に基づいて、プログラムでJavascriptで画像の縦横比が適切かどうかを判断/計算するにはどうすればよいですか?
例: 以下は問題ありません:
Width = 570px
Height = 520px
Ratio = 10
Aspect = 57:52
これは良くありません:
Width = 815px
Height = 85px
Ratio = 5
Aspect = 163:17
その「比率」値が最大許容値である場合:
if (Ratio < (Width / Height)) {
... bad ratio ...
}
縦横比を正方形の 20% 以内にしたい場合は、次のようにします。
maxOff=0.2; //percent margin of aspect ratio acceptance... (20%)
if ((width/height)>=(1-maxOff)&&(width/height)<=(1+maxOff)) {
//image is ok
}
Height を Width で割り、その結果を、X より大きく Y より小さい数値を受け入れる if ステートメントに渡さないのはなぜでしょうか? 割り算の結果が合わない場合は比率がずれています。
var nh=0;
var nw=0;
if (Width>Height)
{
nw=100;//or whatever you want the new thing to be.
nh=(100*Height)/Width;
}
else
{
nh=100;//same as before just switched
nw=(100*Height)/Width;
}
次に、サイズをnw、nhに設定します