Home >Backend Development >PHP Tutorial >PHP thumbnail code to achieve full pane equal proportion reduction

PHP thumbnail code to achieve full pane equal proportion reduction

WBOY
WBOYOriginal
2016-07-25 08:58:51894browse
  1. /**
  2. * Scaled down thumbnails
  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页面中的图片地址

复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn