PHP压缩图片

WBOY
发布: 2016-07-25 08:45:11
原创
960 人浏览过
  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.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!