/lib/class/imageThumb.class.php

https://gitlab.com/imxieke/XCloud · PHP · 167 lines · 135 code · 6 blank · 26 comment · 28 complexity · b49f813bacf6ffa49f46bb7e776e1b6c MD5 · raw file

  1. <?php
  2. /**
  3. * 类名:CreatMiniature
  4. * 功能:生成多种类型的缩略图
  5. * 基本参数:$srcFile,$echoType
  6. * 方法用到的参数:
  7. * $toFile,生成的文件 * $toW,生成的宽 $toH,生成的高*
  8. * $bk1,背景颜色参数 以255为最高 * $bk2,背景颜色参数 * $bk3,背景颜色参数
  9. *
  10. * 例子:
  11. * include('thumb.php');
  12. * $cm=new CreatMiniature();
  13. * $cm->SetVar('1.jpg','file');
  14. * $cm->Distortion('dis_bei.jpg',150,200);
  15. * $cm->Prorate('pro_bei.jpg',150,200);//附带切割
  16. * $cm->Cut('cut_bei.jpg',150,200);
  17. * $cm->BackFill('fill_bei.jpg',150,200);
  18. */
  19. class CreatMiniature {
  20. // 公共变量
  21. var $srcFile = ''; //原图
  22. var $echoType; //输出图片类型,link--不保存为文件;file--保存为文件
  23. var $im = ''; //临时变量
  24. var $srcW = ''; //原图宽
  25. var $srcH = ''; //原图高
  26. // 设置变量及初始化
  27. function SetVar($srcFile, $echoType){
  28. $this->srcFile = $srcFile;
  29. $this->echoType = $echoType;
  30. $info = '';
  31. $data = GetImageSize($this->srcFile, $info);
  32. switch ($data[2]) {
  33. case 1:
  34. if (!function_exists('imagecreatefromgif')) {
  35. exit();
  36. }
  37. $this->im = ImageCreateFromGIF($this->srcFile);
  38. break;
  39. case 2:
  40. if (!function_exists('imagecreatefromjpeg')) {
  41. exit();
  42. }
  43. $this->im = ImageCreateFromJpeg($this->srcFile);
  44. break;
  45. case 3:
  46. $this->im = ImageCreateFromPNG($this->srcFile);
  47. break;
  48. }
  49. $this->srcW = ImageSX($this->im);
  50. $this->srcH = ImageSY($this->im);
  51. }
  52. // 生成扭曲型缩图
  53. function Distortion($toFile, $toW, $toH){
  54. $cImg = $this->CreatImage($this->im, $toW, $toH, 0, 0, 0, 0, $this->srcW, $this->srcH);
  55. return $this->EchoImage($cImg, $toFile);
  56. ImageDestroy($cImg);
  57. }
  58. // 生成按比例缩放的缩图
  59. function Prorate($toFile, $toW, $toH){
  60. $toWH = $toW / $toH;
  61. $srcWH = $this->srcW / $this->srcH;
  62. if ($toWH<=$srcWH) {
  63. $ftoW = $toW;
  64. $ftoH = $ftoW * ($this->srcH / $this->srcW);
  65. } else {
  66. $ftoH = $toH;
  67. $ftoW = $ftoH * ($this->srcW / $this->srcH);
  68. }
  69. if ($this->srcW > $toW || $this->srcH > $toH) {
  70. $cImg = $this->CreatImage($this->im, $ftoW, $ftoH, 0, 0, 0, 0, $this->srcW, $this->srcH);
  71. return $this->EchoImage($cImg, $toFile);
  72. ImageDestroy($cImg);
  73. } else {
  74. $cImg = $this->CreatImage($this->im, $this->srcW, $this->srcH, 0, 0, 0, 0, $this->srcW, $this->srcH);
  75. return $this->EchoImage($cImg, $toFile);
  76. ImageDestroy($cImg);
  77. }
  78. }
  79. // 生成最小裁剪后的缩图
  80. function Cut($toFile, $toW, $toH){
  81. $toWH = $toW / $toH;
  82. $srcWH = $this->srcW / $this->srcH;
  83. if ($toWH<=$srcWH) {
  84. $ctoH = $toH;
  85. $ctoW = $ctoH * ($this->srcW / $this->srcH);
  86. } else {
  87. $ctoW = $toW;
  88. $ctoH = $ctoW * ($this->srcH / $this->srcW);
  89. }
  90. $allImg = $this->CreatImage($this->im, $ctoW, $ctoH, 0, 0, 0, 0, $this->srcW, $this->srcH);
  91. $cImg = $this->CreatImage($allImg, $toW, $toH, 0, 0, ($ctoW - $toW) / 2, ($ctoH - $toH) / 2, $toW, $toH);
  92. return $this->EchoImage($cImg, $toFile);
  93. ImageDestroy($cImg);
  94. ImageDestroy($allImg);
  95. }
  96. // 生成背景填充的缩图,默认用白色填充剩余空间,传入$isAlpha为真时用透明色填充
  97. function BackFill($toFile, $toW, $toH,$isAlpha=false,$red=255, $green=255, $blue=255){
  98. $toWH = $toW / $toH;
  99. $srcWH = $this->srcW / $this->srcH;
  100. if ($toWH<=$srcWH) {
  101. $ftoW = $toW;
  102. $ftoH = $ftoW * ($this->srcH / $this->srcW);
  103. } else {
  104. $ftoH = $toH;
  105. $ftoW = $ftoH * ($this->srcW / $this->srcH);
  106. }
  107. if (function_exists('imagecreatetruecolor')) {
  108. @$cImg = ImageCreateTrueColor($toW, $toH);
  109. if (!$cImg) {
  110. $cImg = ImageCreate($toW, $toH);
  111. }
  112. } else {
  113. $cImg = ImageCreate($toW, $toH);
  114. }
  115. $fromTop = ($toH - $ftoH)/2;//从正中间填充
  116. $backcolor = imagecolorallocate($cImg,$red,$green, $blue); //填充的背景颜色
  117. if ($isAlpha){//填充透明色
  118. $backcolor=ImageColorTransparent($cImg,$backcolor);
  119. $fromTop = $toH - $ftoH;//从底部填充
  120. }
  121. ImageFilledRectangle($cImg, 0, 0, $toW, $toH, $backcolor);
  122. if ($this->srcW > $toW || $this->srcH > $toH) {
  123. $proImg = $this->CreatImage($this->im, $ftoW, $ftoH, 0, 0, 0, 0, $this->srcW, $this->srcH);
  124. if ($ftoW < $toW) {
  125. ImageCopy($cImg, $proImg, ($toW - $ftoW) / 2, 0, 0, 0, $ftoW, $ftoH);
  126. } else if ($ftoH < $toH) {
  127. ImageCopy($cImg, $proImg, 0, $fromTop, 0, 0, $ftoW, $ftoH);
  128. } else {
  129. ImageCopy($cImg, $proImg, 0, 0, 0, 0, $ftoW, $ftoH);
  130. }
  131. } else {
  132. ImageCopyMerge($cImg, $this->im, ($toW - $ftoW) / 2,$fromTop, 0, 0, $ftoW, $ftoH, 100);
  133. }
  134. return $this->EchoImage($cImg, $toFile);
  135. ImageDestroy($cImg);
  136. }
  137. function CreatImage($img, $creatW, $creatH, $dstX, $dstY, $srcX, $srcY, $srcImgW, $srcImgH){
  138. if (function_exists('imagecreatetruecolor')) {
  139. @$creatImg = ImageCreateTrueColor($creatW, $creatH);
  140. if ($creatImg)
  141. ImageCopyResampled($creatImg, $img, $dstX, $dstY, $srcX, $srcY, $creatW, $creatH, $srcImgW, $srcImgH);
  142. else {
  143. $creatImg = ImageCreate($creatW, $creatH);
  144. ImageCopyResized($creatImg, $img, $dstX, $dstY, $srcX, $srcY, $creatW, $creatH, $srcImgW, $srcImgH);
  145. }
  146. } else {
  147. $creatImg = ImageCreate($creatW, $creatH);
  148. ImageCopyResized($creatImg, $img, $dstX, $dstY, $srcX, $srcY, $creatW, $creatH, $srcImgW, $srcImgH);
  149. }
  150. return $creatImg;
  151. }
  152. // 输出图片,link---只输出,不保存文件。file--保存为文件
  153. function EchoImage($img, $to_File){
  154. switch ($this->echoType) {
  155. case 'link':return ImagePNG($img);break;
  156. case 'file':return ImagePNG($img, $to_File);break;
  157. //return ImageJpeg($img, $to_File);
  158. }
  159. }
  160. }