<table cellspacing="0" cellpadding="0"><tr><td class="t_f" id="postmessage_61759"> <div class="blockcode"> <div id="code_eZy"><ol> <li>function scale_dimensions_within_limits($w,$h,$max_w,$max_h){</li> <li> // $w is the width of the current rectangle </li> <li> // $h is the height of the current rectangle </li> <li> // $max_w is the maximum width that an image can be sized </li> <li> // $max_h is the maximum height that an image can be sized </li> <li> <li> // **** Here's where the magic is starts **** </li> <li> // Switch the concept of horiz/vertical/square to long/short side </li> <li> $short_side_len = ($w < $h ? $w : $h); <li> $long_side_len = ($w > $h ? $w : $h); </li> <li> // Set a variable to the variable name of the output variable</li> <li> $ssvar = ($w > $h ? 'h':'w'); </li> <li> $lsvar = ($w > $h ? 'w':'h'); </li> <li> $maxLSvar = "max_".$lsvar; </li> <li> $maxSSvar = "max_".$ssvar; </li> <li> <li> // Do the first pass on the long side</li> <li> $ratio = $$maxLSvar/$long_side_len; </li> <li> $newSS = round($short_side_len * $ratio); </li> <li> $newLS = round($long_side_len * $ratio); </li> <li> <li> // *** Note - the only coditional block!</li> <li> // If short side is still out of limit, limit the short side and adjust </li> <li> if($newSS > $$maxSSvar){ </li> <li> $ratio = $$maxSSvar/$newSS; </li> <li> $newLS = round($ratio*$newLS); </li> <li> $newSS = $$maxSSvar; </li> <li> } </li> <li> <li> // **** Here's where the magic ends **** </li> <li> // Re-couple the h/w (or w/h) with the long/shortside counterparts </li> <li> // $$ means it's a variable variable (dynamic assignment) </li> <li> $$ssvar = $newSS; </li> <li> $$lsvar = $newLS;</li> <li> <li> // Prep the return array </li> <li> $dimensions['w'] = $w; // this is derived from either $ssvar or $lsvar </li> <li> $dimensions['h'] = $h; return $dimensions; </li> <li>}</li> </ol></div> <em onclick="copycode($('code_eZy'));">复制代码</em> </div> <br> </td></tr></table> <div class="ptg mbm mtn"> PHP</div> <div id="comment_61759" class="cm"> </div> <div id="post_rate_div_61759"></div> <br><br>