应该很简单:
$ratio = 200/300;
$width = mt_rand(100, 250);
$height = round($width / $ratio);
看一些例子:
$ratio = 200/300;
echo "base aspect ratio: ", round($ratio, 2), "\n\n";
foreach(range(1,10) as $_) {
$width = mt_rand(100, 250);
$height = round($width / $ratio);
echo "http://placekitten.com/{$width}/{$height}\n";
echo "aspect ratio: ", round($width / $height, 2), "\n\n";
}
输出
base aspect ratio: 0.67
http://placekitten.com/229/344
aspect ratio: 0.67
http://placekitten.com/112/168
aspect ratio: 0.67
http://placekitten.com/241/362
aspect ratio: 0.67
http://placekitten.com/223/335
aspect ratio: 0.67
http://placekitten.com/196/294
aspect ratio: 0.67
http://placekitten.com/234/351
aspect ratio: 0.67
http://placekitten.com/116/174
aspect ratio: 0.67
http://placekitten.com/157/236
aspect ratio: 0.67
http://placekitten.com/165/248
aspect ratio: 0.67
http://placekitten.com/114/171
aspect ratio: 0.67