欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

PHP压缩图片

程序员文章站 2022-05-02 08:01:42
...
  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
相关标签: PHP压缩图片