首页  >  文章  >  后端开发  >  PHP压缩图片

PHP压缩图片

WBOY
WBOY原创
2016-07-25 08:45:11932浏览
  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 $long_side_len = ($w > $h ? $w : $h);
  9. // Set a variable to the variable name of the output variable
  10. $ssvar = ($w > $h ? 'h':'w');
  11. $lsvar = ($w > $h ? 'w':'h');
  12. $maxLSvar = "max_".$lsvar;
  13. $maxSSvar = "max_".$ssvar;
  14. // Do the first pass on the long side
  15. $ratio = $$maxLSvar/$long_side_len;
  16. $newSS = round($short_side_len * $ratio);
  17. $newLS = round($long_side_len * $ratio);
  18. // *** Note - the only coditional block!
  19. // If short side is still out of limit, limit the short side and adjust
  20. if($newSS > $$maxSSvar){
  21. $ratio = $$maxSSvar/$newSS;
  22. $newLS = round($ratio*$newLS);
  23. $newSS = $$maxSSvar;
  24. }
  25. // **** Here's where the magic ends ****
  26. // Re-couple the h/w (or w/h) with the long/shortside counterparts
  27. // $$ means it's a variable variable (dynamic assignment)
  28. $$ssvar = $newSS;
  29. $$lsvar = $newLS;
  30. // Prep the return array
  31. $dimensions['w'] = $w; // this is derived from either $ssvar or $lsvar
  32. $dimensions['h'] = $h; return $dimensions;
  33. }
复制代码

PHP


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn