function scale_dimensions_within_limits($w,$h,$max_w,$max_h){  // $w is the width of the current rectangle   // $h is the height of the current rectangle   // $max_w is the maximum width that an image can be sized   // $max_h is the maximum height that an image can be sized   // **** here's where the magic is starts ****   // switch the concept of horiz/vertical/square to long/short side   $short_side_len = ($w   $long_side_len = ($w > $h ? $w : $h);   // set a variable to the variable name of the output variable  $ssvar = ($w > $h ? 'h':'w');   $lsvar = ($w > $h ? 'w':'h');   $maxlsvar = max_.$lsvar;   $maxssvar = max_.$ssvar;   // do the first pass on the long side  $ratio = $$maxlsvar/$long_side_len;   $newss = round($short_side_len * $ratio);   $newls = round($long_side_len * $ratio);   // *** note - the only coditional block!  // if short side is still out of limit, limit the short side and adjust   if($newss > $$maxssvar){     $ratio = $$maxssvar/$newss;     $newls = round($ratio*$newls);     $newss = $$maxssvar;   }   // **** here's where the magic ends ****   // re-couple the h/w (or w/h) with the long/shortside counterparts   // $$ means it's a variable variable (dynamic assignment)   $$ssvar = $newss;   $$lsvar = $newls;  // prep the return array   $dimensions['w'] = $w; // this is derived from either $ssvar or $lsvar   $dimensions['h'] = $h; return $dimensions; }
复制代码
php
   
 
   