首頁 >後端開發 >php教程 >php縮圖程式碼,實現滿窗格等比例縮小

php縮圖程式碼,實現滿窗格等比例縮小

WBOY
WBOY原創
2016-07-25 08:58:51887瀏覽
  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页面中的图片地址

复制代码


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn