PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/class/image/ShowImage.class.php

https://github.com/hylinux/ltebbs
PHP | 297 lines | 163 code | 63 blank | 71 comment | 121 complexity | 8034343d17add2e72c8ce103055db97e MD5 | raw file
  1. <?php
  2. //vim:set expandtab tabstop=3 shiftwidth=3 softtabstop=3 foldcolumn=1 foldmethod=marker:
  3. /**
  4. * 项目: 5anet(BBS)
  5. * 文件: class/image/ShowImage.class.php
  6. *
  7. * PHP Version 5
  8. *
  9. * @package: class.image
  10. * @author: Mike.G Chinese Name: 黄叶 <hylinux@gmail.com>
  11. * @license: http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
  12. * @copyright: http://www.5anet.com
  13. * @version: $Id: ShowImage.class.php,v 1.1.1.1 2006-08-28 13:09:20 ghw Exp $
  14. * @date: $Date: 2006-08-28 13:09:20 $
  15. */
  16. class ShowImage {
  17. /**
  18. * 显示图片
  19. * @param: NULL
  20. * @return: NULL
  21. * @access: public
  22. * @static
  23. */
  24. public function run() {
  25. $imagedir = $_GET['image'];
  26. $imagedir = urldecode($imagedir);
  27. $imagedir = trim($imagedir);
  28. $width = $_GET['width'];
  29. $height = $_GET['height'];
  30. //生成缩略图
  31. $mysize = getimagesize($imagedir);
  32. $mymime = $mysize["mime"];
  33. //得出高和宽
  34. $mywidth = $mysize[0];
  35. $myheight = $mysize[1];
  36. //echo $mywidth."<br>".$myheight."<br>".$mymime."<br>";
  37. //exit;
  38. //如果图像超出这个范围即采用缩略图的
  39. //形式
  40. if ($mywidth > $width || $myheight > $height)
  41. {
  42. if ( $mywidth > $width && $myheight > $height ) {
  43. header("Content-type:".$mymime);
  44. if ( $mymime == "image/png" ) {
  45. $img = imagecreatefrompng($imagedir);
  46. } else if ($mymime == "image/jpeg" ) {
  47. $img = imagecreatefromjpeg($imagedir);
  48. } else if ( $mymime == "image/gif" ) {
  49. $img = imagecreatefromgif($imagedir);
  50. }
  51. if ( $width > $height ) {
  52. /*
  53. if ( $mywidth > $myheight ) {
  54. // $imgheight = $height;
  55. // $imgwidth = ceil($mywidth * ( $height / $myheight ) );
  56. $imgwidth = $width;
  57. $imgheight = ceil($myheight * ( $width / $mywidth ) );
  58. } else if ( $mywidth < $myheight ) {
  59. // $imgwidth = $width;
  60. // $imgheight = ceil($myheight * ( $width / $mywidth ) );
  61. $imgheight = $height;
  62. $imgwidth = ceil($mywidth * ( $height / $myheight ) );
  63. } else if ( $mywidth == $myheight ) {
  64. $imgheight = $height;
  65. $imgwidth = ceil($mywidth * ( $height / $myheight ) );
  66. }
  67. */
  68. $bi_width = $width / $mywidth ;
  69. $bi_height = $height / $myheight;
  70. if ( $bi_width > $bi_height ) {
  71. $imgwidth = ceil($bi_height * $mywidth);
  72. $imgheight = ceil($bi_height * $myheight);
  73. } else if ( $bi_width < $bi_height ) {
  74. $imgwidth = ceil($bi_width * $mywidth);
  75. $imgheight = ceil($bi_width * $myheight);
  76. } else if ( $bi_width == $bi_height ) {
  77. $imgwidth = ceil($bi_height * $mywidth);
  78. $imgheight = ceil($bi_height * $myheight);
  79. }
  80. // echo $imgwidth."\t".$imgheight."\t".$mywidth."\t".$myheight."<br>";
  81. // exit;
  82. $img2 = imagecreatetruecolor($imgwidth, $imgheight);
  83. #$bac = imagecolorallocate($img2, 255, 255, 255);
  84. imagecopyresampled($img2, $img, 0, 0, 0, 0, $imgwidth, $imgheight, $mywidth, $myheight);
  85. if ( $mymime == "image/png" ) {
  86. imagepng($img2);
  87. } else if ( $mymime == "image/jpeg" || $mymime == "image/jpg" ) {
  88. imagejpeg($img2);
  89. } else if ( $mymime == "image/gif" ) {
  90. imagegif($img2);
  91. }
  92. imagedestroy($img);
  93. imagedestroy($img2);
  94. } else if ( $width < $height ) {
  95. /*
  96. if ( $mywidth > $myheight ) {
  97. $imgheight = $height;
  98. $imgwidth = ceil($mywidth * ( $height / $myheight ) );
  99. } else if ( $mywidth < $myheight ) {
  100. $imgheight = $height;
  101. $imgwidth = ceil($mywidth * ( $height / $myheight ) );
  102. } else if ( $mywidth == $myheight ) {
  103. $imgheight = $height;
  104. $imgwidth = ceil($mywidth * ( $height / $myheight ) );
  105. }*/
  106. $bi_width = $width / $mywidth ;
  107. $bi_height = $height / $myheight;
  108. if ( $bi_width > $bi_height ) {
  109. $imgwidth = ceil($bi_height * $mywidth);
  110. $imgheight = ceil($bi_height * $myheight);
  111. } else if ( $bi_width < $bi_height ) {
  112. $imgwidth = ceil($bi_width * $mywidth);
  113. $imgheight = ceil($bi_width * $myheight);
  114. } else if ( $bi_width == $bi_height ) {
  115. $imgwidth = ceil($bi_height * $mywidth);
  116. $imgheight = ceil($bi_height * $myheight);
  117. }
  118. // echo $imgwidth."\t".$imgheight."\t".$mywidth."\t".$myheight."<br>";
  119. // exit;
  120. $img2 = imagecreatetruecolor($imgwidth, $imgheight);
  121. #$bac = imagecolorallocate($img2, 255, 255, 255);
  122. imagecopyresampled($img2, $img, 0, 0, 0, 0, $imgwidth, $imgheight, $mywidth, $myheight);
  123. if ( $mymime == "image/png" ) {
  124. imagepng($img2);
  125. } else if ( $mymime == "image/jpeg" || $mymime == "image/jpg" ) {
  126. imagejpeg($img2);
  127. } else if ( $mymime == "image/gif" ) {
  128. imagegif($img2);
  129. }
  130. imagedestroy($img);
  131. imagedestroy($img2);
  132. } else if ( $width == $height ) {
  133. $imgwidth = ceil($width * ( $height / $myheight)) ;
  134. $imgheight = $imgwidth;
  135. $img2 = imagecreatetruecolor($imgwidth, $imgheight);
  136. #$bac = imagecolorallocate($img2, 255, 255, 255);
  137. imagecopyresampled($img2, $img, 0, 0, 0, 0, $imgwidth, $imgheight, $mywidth, $myheight);
  138. if ( $mymime == "image/png" ) {
  139. imagepng($img2);
  140. } else if ( $mymime == "image/jpeg" || $mymime == "image/jpg" ) {
  141. imagejpeg($img2);
  142. } else if ( $mymime == "image/gif" ) {
  143. imagegif($img2);
  144. }
  145. imagedestroy($img);
  146. imagedestroy($img2);
  147. }
  148. } else {
  149. header("Content-type:".$mymime);
  150. if ( $mymime == "image/png" ) {
  151. $img = imagecreatefrompng($imagedir);
  152. } else if ($mymime == "image/jpeg" ) {
  153. $img = imagecreatefromjpeg($imagedir);
  154. } else if ( $mymime == "image/gif" ) {
  155. $img = imagecreatefromgif($imagedir);
  156. }
  157. if ( $mywidth > $myheight ) {
  158. //以宽度为主
  159. $imgwidth = $width;
  160. $imgheight = ceil($myheight * ($width/$mywidth));
  161. $img2 = imagecreatetruecolor($imgwidth, $imgheight);
  162. #$bac = imagecolorallocate($img2, 255, 255, 255);
  163. imagecopyresampled($img2, $img, 0, 0, 0, 0, $imgwidth, $imgheight, $mywidth, $myheight);
  164. if ( $mymime == "image/png" ) {
  165. imagepng($img2);
  166. } else if ( $mymime == "image/jpeg" || $mymime == "image/jpg" ) {
  167. imagejpeg($img2);
  168. } else if ( $mymime == "image/gif" ) {
  169. imagegif($img2);
  170. }
  171. imagedestroy($img);
  172. imagedestroy($img2);
  173. } else if ( $mywidth < $myheight ) {
  174. //以高度为主
  175. $imgwidth = ceil($mywidth * ( $height / $myheight)) ;
  176. $imgheight = $height;
  177. $img2 = imagecreatetruecolor($imgwidth, $imgheight);
  178. #$bac = imagecolorallocate($img2, 255, 255, 255);
  179. imagecopyresampled($img2, $img, 0, 0, 0, 0, $imgwidth, $imgheight, $mywidth, $myheight);
  180. if ( $mymime == "image/png" ) {
  181. imagepng($img2);
  182. } else if ( $mymime == "image/jpeg" || $mymime == "image/jpg" ) {
  183. imagejpeg($img2);
  184. } else if ( $mymime == "image/gif" ) {
  185. imagegif($img2);
  186. }
  187. imagedestroy($img);
  188. imagedestroy($img2);
  189. } else if ( $mywidth == $myheight ) {
  190. $imgwidth = ceil($mywidth * ( $height / $myheight)) ;
  191. $imgheight = $imgwidth;
  192. $img2 = imagecreatetruecolor($imgwidth, $imgheight);
  193. #$bac = imagecolorallocate($img2, 255, 255, 255);
  194. imagecopyresampled($img2, $img, 0, 0, 0, 0, $imgwidth, $imgheight, $mywidth, $myheight);
  195. if ( $mymime == "image/png" ) {
  196. imagepng($img2);
  197. } else if ( $mymime == "image/jpeg" || $mymime == "image/jpg" ) {
  198. imagejpeg($img2);
  199. } else if ( $mymime == "image/gif" ) {
  200. imagegif($img2);
  201. }
  202. imagedestroy($img);
  203. imagedestroy($img2);
  204. }
  205. }
  206. } else {
  207. header("Content-type:".$mymime);
  208. if ( $mymime == "image/png" ) {
  209. $img = imagecreatefrompng($imagedir);
  210. } else if ( $mymime == "image/jpeg" ) {
  211. $img = imagecreatefromjpeg($imagedir);
  212. }
  213. else
  214. {
  215. $img = imagecreatefromgif($imagedir);
  216. }
  217. $bac = imagecolorallocate($img, 255, 255, 255);
  218. if ( $mymime == "image/png" ) {
  219. imagepng($img);
  220. } else if ( $mymime == "image/jpeg" || $mymime == "image/jpg" ) {
  221. imagejpeg($img);
  222. } else if ( $mymime == "image/gif" ) {
  223. imagegif($img);
  224. }
  225. imagedestroy($img);
  226. }
  227. }
  228. }
  229. ?>