当前位置:Gxlcms > PHP教程 > php缩略图代码,实现满窗格等比例缩小

php缩略图代码,实现满窗格等比例缩小

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

  1. /**
  2. * 等比例缩小的缩略图
  3. * edit bbs.it-home.org
  4. */
  5. function thumbs($FileName,$SaveTo,$SetW,$SetH){
  6. $IMGInfo= getimagesize($FileName);
  7. if(!$IMGInfo) return false;
  8. if($IMGInfo[mime]== "image/pjpeg" || $IMGInfo[mime]=="image/jpeg"){
  9. $ThisPhoto= imagecreatefromjpeg($FileName);
  10. }elseif($IMGInfo[mime]== "image/x-png" || $IMGInfo[mime]== "image/png"){
  11. $ThisPhoto= imagecreatefrompng($FileName);
  12. }elseif($IMGInfo[mime]== "image/gif"){
  13. $ThisPhoto=imagecreatefromgif($FileName);
  14. }
  15. $width=$IMGInfo[0];
  16. $height=$IMGInfo[1];
  17. $scalc = max($width/$SetW,$height/$SetH);
  18. $nw = intval($width/$scalc);
  19. $nh = intval($height/$scalc);
  20. echo "缩略大小:w$nw,h$nh
    ";
  21. if($SetW-$nw == 1){$nw = $SetW;}
  22. if($SetH-$nh == 1){$nh = $SetH;}
  23. echo "+修正1像素: w$nw,h$nh
    ";
  24. //补宽
  25. if($SetW-$nw > 0){
  26. $nh = $nh +(($nh/$nw) * ($SetW-$nw));
  27. echo "*需补宽".($SetW-$nw).",陪补高".(($nh/$nw) * ($SetW-$nw))."
    ";
  28. $nw = $SetW;
  29. }
  30. //补高
  31. if($SetH-$nh > 0){
  32. $nw = $nw + (($nw/$nh) * ($SetH-$nh));
  33. echo "*需补高".($SetH-$nh).",陪补宽". (($nw/$nh) * ($SetH-$nh)) ."
    ";
  34. $nh = $SetH;
  35. }
  36. $nw = intval($nw);
  37. $nh = intval($nh);
  38. echo "+修正大小:w$nw,h$nh
    ";
  39. $px = ($SetW - $nw)/2;
  40. $py = ($SetH - $nh)/2;
  41. echo "窗口大小:w$SetW,h$SetH
    ";
  42. echo "+偏移修正:x$px,y$py
    ";
  43. $NewPhoto=imagecreatetruecolor($SetW,$SetH);
  44. imagecopyresized($NewPhoto,$ThisPhoto,$px,$py,0,0,$nw,$nh,$width,$height);
  45. ImageJpeg ($NewPhoto,$SaveTo);
  46. return true;
  47. }
  48. thumbs('a.jpg','newa.jpg',150,70);
  49. ?>

2,css代码部

3,html页面中的图片地址

人气教程排行