function image_size($sx, $sy, $dx, $dy, $enlarge = false) {
if (($sx <= $dx) && ($sy <= $dy) && ($enlarge === false)) {
$r['x'] = $sx;
$r['y'] = $sy;
}
else {
$ratio = $sy / $sx;
$r['x'] = $dx;
$r['y'] = $dy;
if ($ratio < 1) {
// ** resize along the x-axis **
$my = round($dx * $ratio);
if ($my <= $dy) {
// ** quick test just in case **
$r['y'] = $my;
}
else {
// ** nope, have to find the correct value for x **
while (round($r['x'] * $ratio) > $dy)
$r['x']--;
}
}
elseif ($ratio > 1) {
// ** resize along the x-axis **
$mx = round($dy / $ratio);
if ($mx <= $dx) {
// ** quick test just in case **
$r['x'] = $mx;
}
else {
// ** nope, have to find the correct value for y **
while (round($r['y'] / $ratio) > $dx)
$r['y']--;
}
}
else {
// ** the image is a square **
$r['x'] = $r['x'] = min($dx, $dy);
}
}
return $r;
}
