PHP压缩图片

WBOY
Libérer: 2016-07-25 08:45:11
original
995 Les gens l'ont consulté
  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


Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal