PageRenderTime 54ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/ePHP/libraries/Image.class.php

http://php-framework-ephp.googlecode.com/
PHP | 249 lines | 150 code | 23 blank | 76 comment | 42 complexity | 5c691165ee18456ec527c6f35d067077 MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. /**
  3. +------------------------------------------------------------------------------
  4. * ?????????
  5. * <code>
  6. * ???????
  7. * ?view??????<img src="/?c=reg&a=code" onclick="this.src='/?c=reg&a=code'" />
  8. *
  9. * //?????
  10. * public function registerAction()
  11. * {
  12. * echo Image::chkVerify() ? '?????' : '?????';
  13. * }
  14. * public funciton codeAction()
  15. * {
  16. * Image::imgVerify();
  17. * }
  18. *
  19. * //???????
  20. * print_r(Image::getInfo('/home/a.jpg'));
  21. *
  22. * //??gd?????
  23. * Image::thumbImg('a.jpg','a-thumb.jpg',100,120);//?? a.jpg ????,??:100 ??:120
  24. *
  25. * </code>
  26. +------------------------------------------------------------------------------
  27. * @version 3.0
  28. * @author WangXian
  29. * @email admin@loopx.cn
  30. * @package libraries
  31. * @creation_date 2010-10-17
  32. * @last_modified 2010-12-25 19:14:58
  33. +------------------------------------------------------------------------------
  34. */
  35. class Image
  36. {
  37. /**
  38. * ???????
  39. * @param $imgFile ?????
  40. * @return array
  41. */
  42. static public function getInfo($imgFile)
  43. {
  44. $imageInfo = getimagesize($imgFile);
  45. if( $imageInfo!== false)
  46. {
  47. $imageType = strtolower(substr(image_type_to_extension($imageInfo[2]),1));
  48. $imageSize = filesize($imgFile);
  49. $info = array(
  50. "width"=>$imageInfo[0],
  51. "height"=>$imageInfo[1],
  52. "type"=>$imageType,
  53. "size"=>$imageSize,
  54. "mime"=>$imageInfo['mime']
  55. );
  56. return $info;
  57. }
  58. else return false;
  59. }
  60. /**
  61. * ?????
  62. * ??????????????????????????JPG
  63. * @param $srcfile:????
  64. * @param $dstfile:?????????
  65. * @param $thumbWidth:???????
  66. * @param $thumbHeight:???????
  67. */
  68. static public function thumbImg($srcfile,$dstfile,$thumbWidth,$thumbHeight)
  69. {
  70. //?????
  71. $tow = $thumbWidth;
  72. $toh = $thumbHeight;
  73. $make_max = 0;
  74. $maxtow = $thumbWidth;
  75. $maxtoh = $thumbHeight;
  76. if($maxtow >= 300 && $maxtoh >= 300)
  77. {
  78. $make_max = 1;
  79. }
  80. //??????
  81. $im = '';
  82. if(true == ($data = getimagesize($srcfile)) )
  83. {
  84. if($data[2] == 1)
  85. {
  86. $make_max = 0;//gif???
  87. if(function_exists("imagecreatefromgif"))
  88. $im = imagecreatefromgif($srcfile);
  89. }
  90. elseif($data[2] == 2)
  91. {
  92. if(function_exists("imagecreatefromjpeg"))
  93. $im = imagecreatefromjpeg($srcfile);
  94. }
  95. elseif($data[2] == 3)
  96. {
  97. if(function_exists("imagecreatefrompng"))
  98. $im = imagecreatefrompng($srcfile);
  99. }
  100. }
  101. if(!$im) return '';
  102. $srcw = imagesx($im);
  103. $srch = imagesy($im);
  104. $towh = $tow/$toh;
  105. $srcwh = $srcw/$srch;
  106. if($towh <= $srcwh)
  107. {
  108. $ftow = $tow;
  109. $ftoh = $ftow*($srch/$srcw);
  110. $fmaxtow = $maxtow;
  111. $fmaxtoh = $fmaxtow*($srch/$srcw);
  112. }
  113. else
  114. {
  115. $ftoh = $toh;
  116. $ftow = $ftoh*($srcw/$srch);
  117. $fmaxtoh = $maxtoh;
  118. $fmaxtow = $fmaxtoh*($srcw/$srch);
  119. }
  120. if($srcw <= $maxtow && $srch <= $maxtoh)
  121. {
  122. $make_max = 0;//???
  123. }
  124. if($srcw > $tow || $srch > $toh)
  125. {
  126. if(function_exists("imagecreatetruecolor") && function_exists("imagecopyresampled") && @$ni = imagecreatetruecolor($ftow, $ftoh))
  127. {
  128. imagecopyresampled($ni, $im, 0, 0, 0, 0, $ftow, $ftoh, $srcw, $srch);
  129. //???
  130. if($make_max && @$maxni = imagecreatetruecolor($fmaxtow, $fmaxtoh))
  131. {
  132. imagecopyresampled($maxni, $im, 0, 0, 0, 0, $fmaxtow, $fmaxtoh, $srcw, $srch);
  133. }
  134. }
  135. elseif(function_exists("imagecreate") && function_exists("imagecopyresized") && @$ni = imagecreate($ftow, $ftoh))
  136. {
  137. imagecopyresized($ni, $im, 0, 0, 0, 0, $ftow, $ftoh, $srcw, $srch);
  138. //???
  139. if($make_max && @$maxni = imagecreate($fmaxtow, $fmaxtoh))
  140. {
  141. imagecopyresized($maxni, $im, 0, 0, 0, 0, $fmaxtow, $fmaxtoh, $srcw, $srch);
  142. }
  143. }
  144. else return '';
  145. if(function_exists('imagejpeg'))
  146. {
  147. imagejpeg($ni, $dstfile);
  148. //???
  149. if($make_max) imagejpeg($maxni, $srcfile);
  150. }
  151. elseif(function_exists('imagepng'))
  152. {
  153. imagepng($ni, $dstfile);
  154. //???
  155. if($make_max) imagepng($maxni, $srcfile);
  156. }
  157. imagedestroy($ni);
  158. if($make_max) imagedestroy($maxni);
  159. }
  160. imagedestroy($im);
  161. return file_exists($dstfile);
  162. }
  163. /**
  164. * ???
  165. * ????,50x24
  166. *
  167. * @param integer $length :?????
  168. * @param integer $mode : 0??????1???2?????3????,5???+??
  169. * @param string $type :????
  170. * @param boolean $hasborder :??????
  171. * @return binary
  172. */
  173. static public function imgVerify($length=4,$mode=3,$type='png',$hasborder=true)
  174. {
  175. $randval = Func::randString($length,$mode);
  176. $_SESSION['imgVerifyCode']=md5(strtolower($randval));
  177. //dump($_SESSION);exit;
  178. $width=50;
  179. $height=24;
  180. $width = ($length*9+10)>$width ? $length*9+10 : $width;
  181. if ( $type!='gif' && function_exists('imagecreatetruecolor'))
  182. $im = @imagecreatetruecolor($width,$height);
  183. else $im = @imagecreate($width,$height);
  184. //$r = array(225,255,255,223);
  185. //$g = array(225,236,237,255);
  186. //$b = array(225,236,166,125);
  187. //$key = mt_rand(0,3);
  188. //$backColor = imagecolorallocate($im, $r[$key],$g[$key],$b[$key]);
  189. $backColor = imagecolorallocate($im, 252,252,252); //???
  190. if($hasborder) $border_color = 238;
  191. else $border_color = 255;
  192. $borderColor = imagecolorallocate($im, $border_color,$border_color,$border_color); //???
  193. $pointColor = imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); //???
  194. @imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
  195. @imagerectangle($im, 0, 0, $width-1, $height-1, $borderColor);
  196. $stringColor = imagecolorallocate($im,mt_rand(0,200),mt_rand(0,120),mt_rand(0,120));
  197. // ??
  198. for($i=0;$i<10;$i++)
  199. {
  200. $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  201. imagearc($im,mt_rand(-10,$width),mt_rand(-10,$height),mt_rand(30,300),mt_rand(20,200),55,44,$fontcolor);
  202. }
  203. for($i=0;$i<25;$i++)
  204. {
  205. $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  206. imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$pointColor);
  207. }
  208. @imagestring($im, 5, 5, 3, $randval, $stringColor);
  209. header("Content-type: image/".$type);
  210. $ImageFun='Image'.$type;
  211. $ImageFun($im);
  212. imagedestroy($im);
  213. }
  214. /**
  215. * ????????????
  216. * @param $verifyCode ????????
  217. * @return boolean
  218. */
  219. static public function chkVerify($verifyCode)
  220. {
  221. if(empty($_SESSION['imgVerifyCode'])) return false;
  222. return $_SESSION['imgVerifyCode'] == md5(strtolower($verifyCode));
  223. }
  224. }
  225. /* End of file Image.class.php */
  226. /* Location: ./_framework/libraries/Image.class.php */