PHP生成等比缩略图类和自定义函数分享

这篇文章主要介绍了PHP生成等比缩略图类和自定义函数分享,分别封装成了一个类和自定义函数,需要的朋友可以参考下。

共有两种等比例缩略图方法可以借鉴

一、为类文件,实例化之后即可使用

二、为自定义方法,比较轻巧

类文件代码如下:

  1. $resizeimage = new resizeimage("./shawn.jpg", "200", "100", "0","../pic/shawnsun.jpg");
  2. //实例化下面的类,就能生成缩略图
  3. //其中,源文件和缩略图地址可以相同,200,100分别代表宽和高,第四个参数为可选 0不截图,1为截图
  4. <?php
  5. class resizeimage{
  6. //图片类型
  7. public $type;
  8. //实际宽度
  9. public $width;
  10. //实际高度
  11. public $height;
  12. //改变后的宽度
  13. public $resize_width;
  14. //改变后的高度
  15. public $resize_height;
  16. //是否裁图
  17. public $cut;
  18. //源图象
  19. public $srcimg;
  20. //目标图象地址
  21. public $dstimg;
  22. //临时创建的图象
  23. public $im;
  24. function resizeimage($img, $wid, $hei,$c,$dstpath){
  25. $this--->srcimg = $img;
  26. $this->resize_width = $wid;
  27. $this->resize_height = $hei;
  28. $this->cut = $c;
  29. //图片的类型
  30. $this->type = strtolower(substr(strrchr($this->srcimg,"."),1));
  31. //初始化图象
  32. $this->initi_img();
  33. //目标图象地址
  34. $this->dst_img($dstpath);
  35. //W & H
  36. $this->width = imagesx($this->im);
  37. $this->height = imagesy($this->im);
  38. //生成图象
  39. $this->newimg();
  40. ImageDestroy ($this->im);
  41. }
  42. function newimg(){
  43. //改变后的图象的比例
  44. $resize_ratio = ($this->resize_width)/($this->resize_height);
  45. //实际图象的比例
  46. $ratio = ($this->width)/($this->height);
  47. if(($this->cut)=="1")
  48. //裁图
  49. {
  50. if($ratio>=$resize_ratio)
  51. //高度优先
  52. {
  53. $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
  54. imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,
  55. $this->resize_height, (($this->height)*$resize_ratio),
  56. $this->height
  57. );
  58. ImageJpeg ($newimg,$this->dstimg);
  59. }
  60. if($ratio<$resize_ratio)
  61. //宽度优先
  62. {
  63. $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
  64. imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,
  65. $this->resize_height, $this->width,
  66. (($this->width)/$resize_ratio)
  67. );
  68. ImageJpeg ($newimg,$this->dstimg);
  69. }
  70. }
  71. else
  72. //不裁图
  73. {
  74. if($ratio>=$resize_ratio)
  75. {
  76. $newimg = imagecreatetruecolor($this->resize_width,
  77. ($this->resize_width)/$ratio
  78. );
  79. imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,
  80. ($this->resize_width)/$ratio, $this->width,
  81. $this->height
  82. );
  83. ImageJpeg ($newimg,$this->dstimg);
  84. }
  85. if($ratio<$resize_ratio)
  86. {
  87. $newimg = imagecreatetruecolor(($this->resize_height)*$ratio,
  88. $this->resize_height
  89. );
  90. imagecopyresampled($newimg, $this->im, 0, 0, 0, 0,
  91. ($this->resize_height)*$ratio,
  92. $this->resize_height, $this->width,
  93. $this->height
  94. );
  95. ImageJpeg ($newimg,$this->dstimg);
  96. }
  97. }
  98. }
  99. //初始化图象
  100. function initi_img(){
  101. if($this->type=="jpg")
  102. {
  103. $this->im = imagecreatefromjpeg($this->srcimg);
  104. }
  105. if($this->type=="gif")
  106. {
  107. $this->im = imagecreatefromgif($this->srcimg);
  108. }
  109. if($this->type=="png")
  110. {
  111. $this->im = imagecreatefrompng($this->srcimg);
  112. }
  113. }
  114. //图象目标地址
  115. function dst_img($dstpath){
  116. $full_length = strlen($this->srcimg);
  117. $type_length = strlen($this->type);
  118. $name_length = $full_length-$type_length;
  119. $name = substr($this->srcimg,0,$name_length-1);
  120. $this->dstimg = $dstpath;
  121. //echo $this->dstimg;
  122. }
  123. }
  124. ?>

自定义方法,代码如下:

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