PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/firstrend/src/core/class/image.class.php

http://ownerpress.googlecode.com/
PHP | 492 lines | 340 code | 53 blank | 99 comment | 72 complexity | 225b4209f1bf5ff193d3741f91a18030 MD5 | raw file
Possible License(s): Apache-2.0, AGPL-1.0, GPL-2.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ?????????? (Build on ThinkPHP)
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2011 http://fanwe.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. /**
  8. * image.class.php
  9. *
  10. * ?????
  11. *
  12. * @package class
  13. * @author awfigq <awfigq@qq.com>
  14. */
  15. class Image
  16. {
  17. /**
  18. * ????
  19. */
  20. var $file = array();
  21. /**
  22. * ????
  23. */
  24. var $dir = 'public';
  25. /**
  26. * ????
  27. */
  28. var $error_code = 0;
  29. /**
  30. * ??????KB
  31. */
  32. var $max_size = 2048;
  33. function Image()
  34. {
  35. }
  36. /**
  37. * ??????
  38. * @param array $file ?????
  39. * @param string $dir ?????
  40. * @return bool
  41. */
  42. function init($file, $dir = 'temp')
  43. {
  44. if(!is_array($file) || empty($file) || !$this->isUploadFile($file['tmp_name']) || trim($file['name']) == '' || $file['size'] == 0)
  45. {
  46. $this->file = array();
  47. $this->error_code = -1;
  48. return false;
  49. }
  50. else
  51. {
  52. $file['size'] = intval($file['size']);
  53. $file['name'] = trim($file['name']);
  54. $file['thumb'] = '';
  55. $file['ext'] = $this->fileExt($file['name']);
  56. $file['name'] = htmlspecialchars($file['name'], ENT_QUOTES);
  57. $file['is_image'] = $this->isImageExt($file['ext']);
  58. $file['is_convert'] = false;
  59. $info = $this->getImageInfo($file['tmp_name']);
  60. if($info['type'] != 'jpg' && $info['type'] != 'jpeg')
  61. {
  62. $file['ext'] = $ext;
  63. $file['is_convert'] = true;
  64. }
  65. $file['file_dir'] = $this->getTargetDir($dir);
  66. $file['prefix'] = md5(microtime(true)).random('6');
  67. $file['target'] = $file['file_dir'].'/'.$file['prefix'].'.jpg';
  68. $file['local_target'] = FANWE_ROOT.$file['target'];
  69. $this->file = &$file;
  70. $this->error_code = 0;
  71. return true;
  72. }
  73. }
  74. /**
  75. * ????
  76. * @return bool
  77. */
  78. function save()
  79. {
  80. if(empty($this->file) || empty($this->file['tmp_name']))
  81. $this->error_code = -101;
  82. elseif(!$this->file['is_image'])
  83. $this->error_code = -102;
  84. elseif(!$this->saveFile($this->file['tmp_name'], $this->file['local_target'],$this->file['is_convert']))
  85. $this->error_code = -103;
  86. elseif($this->file['is_image'] && (!$this->file['image_info'] = $this->getImageInfo($this->file['local_target'], true)))
  87. {
  88. $this->error_code = -104;
  89. @unlink($this->file['local_target']);
  90. }
  91. else
  92. {
  93. $this->error_code = 0;
  94. return true;
  95. }
  96. return false;
  97. }
  98. /**
  99. * ??????
  100. * @return number
  101. */
  102. function error()
  103. {
  104. return $this->error_code;
  105. }
  106. /**
  107. * ???????
  108. * @return string
  109. */
  110. function fileExt($file_name)
  111. {
  112. return addslashes(strtolower(substr(strrchr($file_name, '.'), 1, 10)));
  113. }
  114. /**
  115. * ??????????????
  116. * @param string $ext ???
  117. * @return bool
  118. */
  119. function isImageExt($ext)
  120. {
  121. static $img_ext = array('jpg', 'jpeg', 'png', 'bmp','gif','giff');
  122. return in_array($ext, $img_ext) ? 1 : 0;
  123. }
  124. /**
  125. * ??????
  126. * @param string $target ????
  127. * @return mixed
  128. */
  129. function getImageInfo($target)
  130. {
  131. static $infos = array();
  132. if(!isset($infos[$target]))
  133. {
  134. $infos[$target] = false;
  135. $ext = Image::fileExt($target);
  136. $is_image = Image::isImageExt($ext);
  137. if(!$is_image && $ext != 'tmp')
  138. return false;
  139. elseif(!is_readable($target))
  140. return false;
  141. elseif($image_info = @getimagesize($target))
  142. {
  143. $file_size = floatval(@filesize($target));
  144. $file_size = $file_size / 1024;
  145. if($file_size > $this->max_size)
  146. return false;
  147. list($width, $height, $type) = !empty($image_info) ? $image_info : array('', '', '');
  148. if($is_image && !in_array($type, array(1,2,3,6,13)))
  149. return false;
  150. $image_info['type'] = strtolower(substr(image_type_to_extension($image_info[2]),1));
  151. $infos[$target] = $image_info;
  152. return $image_info;
  153. }
  154. else
  155. return false;
  156. }
  157. return $infos[$target];
  158. }
  159. /**
  160. * ??????????
  161. * @param string $source ????
  162. * @return bool
  163. */
  164. function isUploadFile($source)
  165. {
  166. return $source && ($source != 'none') && (is_uploaded_file($source) || is_uploaded_file(str_replace('\\\\', '\\', $source)));
  167. }
  168. /**
  169. * ???????
  170. * @param string $dir ???????
  171. * @return string
  172. */
  173. function getTargetDir($dir)
  174. {
  175. if($dir == 'temp')
  176. $dir = './public/upload/temp/'.fToDate(NULL,'Y/m/d/H');
  177. else
  178. $dir = './public/upload/'.$dir.'/'.fToDate(NULL,'Y/m/d');
  179. makeDir(FANWE_ROOT.$dir);
  180. return $dir;
  181. }
  182. /**
  183. * ????
  184. * @param string $source ?????
  185. * @param string $target ??????
  186. * @return bool
  187. */
  188. private function saveFile($source, $target,$is_convert = false)
  189. {
  190. if(!Image::isUploadFile($source))
  191. $succeed = false;
  192. elseif($is_convert && $this->convertType($source,$target))
  193. $succeed = true;
  194. elseif(@copy($source, $target))
  195. $succeed = true;
  196. elseif(function_exists('move_uploaded_file') && @move_uploaded_file($source, $target))
  197. $succeed = true;
  198. elseif (@is_readable($source) && (@$fp_s = fopen($source, 'rb')) && (@$fp_t = fopen($target, 'wb')))
  199. {
  200. while (!feof($fp_s))
  201. {
  202. $s = @fread($fp_s, 1024 * 512);
  203. @fwrite($fp_t, $s);
  204. }
  205. fclose($fp_s);
  206. fclose($fp_t);
  207. $succeed = true;
  208. }
  209. if($succeed)
  210. {
  211. $this->error_code = 0;
  212. @chmod($target, 0644);
  213. @unlink($source);
  214. }
  215. else
  216. {
  217. $this->error_code = 0;
  218. }
  219. return $succeed;
  220. }
  221. public function convertType($source,$target)
  222. {
  223. $info = Image::getImageInfo($source);
  224. if($info !== false)
  225. {
  226. $width = $info[0];
  227. $height = $info[1];
  228. $type = $info['type'];
  229. // ????
  230. $createFun = 'imagecreatefrom'.($type=='jpg'?'jpeg':$type);
  231. if(!function_exists($createFun))
  232. $createFun = 'imagecreatefromjpeg';
  233. $srcImg = $createFun($source);
  234. if('gif'==$type || 'png'==$type)
  235. imagecolorallocate($srcImg,255,255,255);
  236. // ?jpeg????????
  237. if('jpg'==$type || 'jpeg'==$type)
  238. imageinterlace($srcImg,1);
  239. if($source == $target)
  240. @unlink($source);
  241. // ????
  242. imagefilter($srcImg, IMG_FILTER_CONTRAST,-2);
  243. imagejpeg($srcImg,$target,IMAGE_CREATE_QUALITY);
  244. imagedestroy($srcImg);
  245. return true;
  246. }
  247. return false;
  248. }
  249. public function thumb($image,$maxWidth=200,$maxHeight=50,$gen = 0,$interlace=true,$filepath = '')
  250. {
  251. $info = Image::getImageInfo($image);
  252. if($info !== false)
  253. {
  254. $srcWidth = $info[0];
  255. $srcHeight = $info[1];
  256. $type = $info['type'];
  257. $interlace = $interlace? 1:0;
  258. unset($info);
  259. if($maxWidth > 0 && $maxHeight > 0)
  260. {
  261. //$scale = min($maxWidth/$srcWidth, $maxHeight/$srcHeight); // ??????
  262. //???????
  263. $scale = $maxWidth/$srcWidth;
  264. }
  265. elseif($maxWidth == 0)
  266. $scale = $maxHeight/$srcHeight;
  267. elseif($maxHeight == 0)
  268. $scale = $maxWidth/$srcWidth;
  269. if($scale >= 1)
  270. {
  271. // ??????????
  272. $width = $srcWidth;
  273. $height = $srcHeight;
  274. }
  275. else
  276. {
  277. // ?????
  278. $width = (int)($srcWidth*$scale);
  279. $height = (int)($srcHeight*$scale);
  280. }
  281. if($gen == 1)
  282. {
  283. $width = $maxWidth;
  284. $height = $maxHeight;
  285. }
  286. $paths = pathinfo($image);
  287. $ext = Image::fileExt($image);
  288. if(empty($filepath))
  289. $thumbname = str_replace('.'.$paths['extension'],'',$image).'_'.$maxWidth.'x'.$maxHeight.'.'.$ext;
  290. else
  291. $thumbname = $filepath;
  292. $thumburl = str_replace(FANWE_ROOT,'',$thumbname);
  293. // ????
  294. $createFun = 'imagecreatefrom'.($type=='jpg'?'jpeg':$type);
  295. if(!function_exists($createFun))
  296. $createFun = 'imagecreatefromjpeg';
  297. $srcImg = $createFun($image);
  298. //?????
  299. if($type!='gif' && function_exists('imagecreatetruecolor'))
  300. $thumbImg = imagecreatetruecolor($width, $height);
  301. else
  302. $thumbImg = imagecreate($width, $height);
  303. $x = 0;
  304. $y = 0;
  305. if($gen == 1 && $maxWidth > 0 && $maxHeight > 0)
  306. {
  307. $resize_ratio = $maxWidth/$maxHeight;
  308. $src_ratio = $srcWidth/$srcHeight;
  309. if($src_ratio >= $resize_ratio)
  310. {
  311. $x = ($srcWidth - ($resize_ratio * $srcHeight)) / 2;
  312. $width = ($height * $srcWidth) / $srcHeight;
  313. }
  314. else
  315. {
  316. $y = ($srcHeight - ( (1 / $resize_ratio) * $srcWidth)) / 2;
  317. $height = ($width * $srcHeight) / $srcWidth;
  318. }
  319. }
  320. // ????
  321. if(function_exists("imagecopyresampled"))
  322. imagecopyresampled($thumbImg, $srcImg, 0, 0, $x, $y, $width, $height, $srcWidth,$srcHeight);
  323. else
  324. imagecopyresized($thumbImg, $srcImg, 0, 0, $x, $y, $width, $height, $srcWidth,$srcHeight);
  325. if('gif'==$type || 'png'==$type) {
  326. $background_color = imagecolorallocate($thumbImg, 0,255,0); // ??????
  327. imagecolortransparent($thumbImg,$background_color); // ????????????????????
  328. }
  329. // ?jpeg????????
  330. if('jpg'==$type || 'jpeg'==$type)
  331. imageinterlace($thumbImg,$interlace);
  332. // ????
  333. imagefilter($thumbImg, IMG_FILTER_CONTRAST,-1);
  334. // ????
  335. imagejpeg($thumbImg,$thumbname,IMAGE_CREATE_QUALITY);
  336. imagedestroy($thumbImg);
  337. imagedestroy($srcImg);
  338. return array('url'=>$thumburl,'path'=>$thumbname);
  339. }
  340. return false;
  341. }
  342. public function water($source,$water,$alpha=80,$position="4")
  343. {
  344. //????????
  345. if(!file_exists($source)||!file_exists($water))
  346. return false;
  347. //????
  348. $sInfo = Image::getImageInfo($source);
  349. $wInfo = Image::getImageInfo($water);
  350. //????????????????
  351. if($sInfo["0"] < $wInfo["0"] || $sInfo['1'] < $wInfo['1'])
  352. return false;
  353. //????
  354. $sCreateFun="imagecreatefrom".$sInfo['type'];
  355. if(!function_exists($sCreateFun))
  356. $sCreateFun = 'imagecreatefromjpeg';
  357. $sImage=$sCreateFun($source);
  358. $wCreateFun="imagecreatefrom".$wInfo['type'];
  359. if(!function_exists($wCreateFun))
  360. $wCreateFun = 'imagecreatefromjpeg';
  361. $wImage=$wCreateFun($water);
  362. //?????????
  363. imagealphablending($wImage, true);
  364. switch (intval($position))
  365. {
  366. case 0: break;
  367. //??
  368. case 1:
  369. $posY=0;
  370. $posX=0;
  371. //??????
  372. imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo[0],$wInfo[1],$alpha);
  373. break;
  374. //??
  375. case 2:
  376. $posY=0;
  377. $posX=$sInfo[0]-$wInfo[0];
  378. //??????
  379. imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo[0],$wInfo[1],$alpha);
  380. break;
  381. //??
  382. case 3:
  383. $posY=$sInfo[1]-$wInfo[1];
  384. $posX=0;
  385. //??????
  386. imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo[0],$wInfo[1],$alpha);
  387. break;
  388. //??
  389. case 4:
  390. $posY=$sInfo[1]-$wInfo[1];
  391. $posX=$sInfo[0]-$wInfo[0];
  392. //??????
  393. imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo[0],$wInfo[1],$alpha);
  394. break;
  395. //??
  396. case 5:
  397. $posY=$sInfo[1]/2-$wInfo[1]/2;
  398. $posX=$sInfo[0]/2-$wInfo[0]/2;
  399. //??????
  400. imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo[0],$wInfo[1],$alpha);
  401. break;
  402. }
  403. //???????????????????
  404. @unlink($source);
  405. //????
  406. imagejpeg($sImage,$source,IMAGE_CREATE_QUALITY);
  407. imagedestroy($sImage);
  408. imagedestroy($wImage);
  409. }
  410. }
  411. if(!function_exists('image_type_to_extension'))
  412. {
  413. function image_type_to_extension($imagetype)
  414. {
  415. if(empty($imagetype))
  416. return false;
  417. switch($imagetype)
  418. {
  419. case IMAGETYPE_GIF : return '.gif';
  420. case IMAGETYPE_JPEG : return '.jpeg';
  421. case IMAGETYPE_PNG : return '.png';
  422. case IMAGETYPE_SWF : return '.swf';
  423. case IMAGETYPE_PSD : return '.psd';
  424. case IMAGETYPE_BMP : return '.bmp';
  425. case IMAGETYPE_TIFF_II : return '.tiff';
  426. case IMAGETYPE_TIFF_MM : return '.tiff';
  427. case IMAGETYPE_JPC : return '.jpc';
  428. case IMAGETYPE_JP2 : return '.jp2';
  429. case IMAGETYPE_JPX : return '.jpf';
  430. case IMAGETYPE_JB2 : return '.jb2';
  431. case IMAGETYPE_SWC : return '.swc';
  432. case IMAGETYPE_IFF : return '.aiff';
  433. case IMAGETYPE_WBMP : return '.wbmp';
  434. case IMAGETYPE_XBM : return '.xbm';
  435. default : return false;
  436. }
  437. }
  438. }
  439. ?>