首頁  >  文章  >  後端開發  >  PHP壓縮圖片

PHP壓縮圖片

WBOY
WBOY原創
2016-07-25 08:45:11960瀏覽
  1. function scale_dimensions_within_limits($w,$h,$max_w,$max_h){
  2. // $w is the width of the current rectangle
  3. // $h is the height of the current rectangle
  4. // $max_w is the maximum width that an image can be sized
  5. // $max_h is the maximum height that an image can be sized
  6. // **** Here's where the magic is starts ****
  7. // Switch the concept of horiz/vertical/square to long/short side
  8. $short_side_len = ($w < $h ? $w : $h);
  9. $long_side_len = ($w > $h ? $w : $h);
  10. // Set a variable to the variable name of the output variable
  11. $ssvar = ($w > $h ? 'h':'w');
  12. $lsvar = ($w > $h ? 'w':'h');
  13. $maxLSvar = "max_".$lsvar;
  14. $maxSSvar = "max_".$ssvar;
  15. // Do the first pass on the long side
  16. $ratio = $$maxLSvar/$long_side_len;
  17. $newSS = round($short_side_len * $ratio);
  18. $newLS = round($long_side_len * $ratio);
  19. // *** Note - the only coditional block!
  20. // If short side is still out of limit, limit the short side and adjust
  21. if($newSS > $$maxSSvar){
  22. $ratio = $$maxSSvar/$newSS;
  23. $newLS = round($ratio*$newLS);
  24. $newSS = $$maxSSvar;
  25. }
  26. // **** Here's where the magic ends ****
  27. // Re-couple the h/w (or w/h) with the long/shortside counterparts
  28. // $$ means it's a variable variable (dynamic assignment)
  29. $$ssvar = $newSS;
  30. $$lsvar = $newLS;
  31. // Prep the return array
  32. $dimensions['w'] = $w; // this is derived from either $ssvar or $lsvar
  33. $dimensions['h'] = $h; return $dimensions;
  34. }
复制代码

PHP


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn