当前位置:Gxlcms > PHP教程 > PHP压缩图片

PHP压缩图片

时间:2021-07-01 10:21:17 帮助过:2人阅读

  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

人气教程排行