PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Tpblog/ThinkPHP/Lib/ORG/Util/Image.class.php

http://tpblog.googlecode.com/
PHP | 508 lines | 292 code | 30 blank | 186 comment | 52 complexity | d344f16b29e1c2e81070847e69a04e52 MD5 | raw file
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2008 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. // $Id$
  12. /**
  13. +------------------------------------------------------------------------------
  14. * ??????
  15. +------------------------------------------------------------------------------
  16. * @category ORG
  17. * @package ORG
  18. * @subpackage Util
  19. * @author liu21st <liu21st@gmail.com>
  20. * @version $Id$
  21. +------------------------------------------------------------------------------
  22. */
  23. class Image extends Base
  24. {//?????
  25. /**
  26. +----------------------------------------------------------
  27. * ??????
  28. *
  29. +----------------------------------------------------------
  30. * @static
  31. * @access public
  32. +----------------------------------------------------------
  33. * @param string $image ?????
  34. +----------------------------------------------------------
  35. * @return mixed
  36. +----------------------------------------------------------
  37. */
  38. static function getImageInfo($img) {
  39. $imageInfo = getimagesize($img);
  40. if( $imageInfo!== false) {
  41. if(function_exists(image_type_to_extension)){
  42. $imageType = strtolower(substr(image_type_to_extension($imageInfo[2]),1));
  43. }else{
  44. $imageType = strtolower(substr($img,strrpos($img,'.')+1));
  45. }
  46. $imageSize = filesize($img);
  47. $info = array(
  48. "width"=>$imageInfo[0],
  49. "height"=>$imageInfo[1],
  50. "type"=>$imageType,
  51. "size"=>$imageSize,
  52. "mime"=>$imageInfo['mime']
  53. );
  54. return $info;
  55. }else {
  56. return false;
  57. }
  58. }
  59. /**
  60. +----------------------------------------------------------
  61. * ?????????
  62. * ??URL??
  63. +----------------------------------------------------------
  64. * @static
  65. * @access public
  66. +----------------------------------------------------------
  67. * @param string $imgFile ?????
  68. * @param string $text ?????
  69. * @param string $width ????
  70. * @param string $height ????
  71. +----------------------------------------------------------
  72. * @return void
  73. +----------------------------------------------------------
  74. */
  75. static function showImg($imgFile,$text='',$width=80,$height=30) {
  76. //????????
  77. $info = Image::getImageInfo($imgFile);
  78. if($info !== false) {
  79. $createFun = str_replace('/','createfrom',$info['mime']);
  80. $im = $createFun($imgFile);
  81. if($im) {
  82. $ImageFun= str_replace('/','',$info['mime']);
  83. if(!empty($text)) {
  84. $tc = imagecolorallocate($im, 0, 0, 0);
  85. imagestring($im, 3, 5, 5, $text, $tc);
  86. }
  87. if($info['type']=='png' || $info['type']=='gif') {
  88. imagealphablending($im, false);//?????????
  89. imagesavealpha($im,true);//??????? alpha ????
  90. }
  91. Header("Content-type: ".$info['mime']);
  92. $ImageFun($im);
  93. @ImageDestroy($im);
  94. return ;
  95. }
  96. }
  97. //?????????????????PNG??
  98. $im = imagecreatetruecolor($width, $height);
  99. $bgc = imagecolorallocate($im, 255, 255, 255);
  100. $tc = imagecolorallocate($im, 0, 0, 0);
  101. imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
  102. imagestring($im, 4, 5, 5, "NO PIC", $tc);
  103. Image::output($im);
  104. return ;
  105. }
  106. /**
  107. +----------------------------------------------------------
  108. * ?????
  109. +----------------------------------------------------------
  110. * @static
  111. * @access public
  112. +----------------------------------------------------------
  113. * @param string $image ??
  114. * @param string $type ????
  115. * @param string $filename ??????
  116. * @param string $maxWidth ??
  117. * @param string $maxHeight ??
  118. * @param string $position ???????
  119. * @param boolean $interlace ??????
  120. +----------------------------------------------------------
  121. * @return void
  122. +----------------------------------------------------------
  123. * @throws ThinkExecption
  124. +----------------------------------------------------------
  125. */
  126. static function thumb($image,$type='',$filename='',$maxWidth=200,$maxHeight=50,$interlace=true,$suffix='_thumb')
  127. {
  128. // ??????
  129. $info = Image::getImageInfo($image);
  130. if($info !== false) {
  131. $srcWidth = $info['width'];
  132. $srcHeight = $info['height'];
  133. $pathinfo = pathinfo($image);
  134. $type = $pathinfo['extension'];
  135. $type = empty($type)?$info['type']:$type;
  136. $type = strtolower($type);
  137. $interlace = $interlace? 1:0;
  138. unset($info);
  139. $scale = min($maxWidth/$srcWidth, $maxHeight/$srcHeight); // ??????
  140. if($scale>=1) {
  141. // ??????????
  142. $width = $srcWidth;
  143. $height = $srcHeight;
  144. }else{
  145. // ?????
  146. $width = (int)($srcWidth*$scale);
  147. $height = (int)($srcHeight*$scale);
  148. }
  149. // ????
  150. $createFun = 'ImageCreateFrom'.($type=='jpg'?'jpeg':$type);
  151. $srcImg = $createFun($image);
  152. //?????
  153. if($type!='gif' && function_exists('imagecreatetruecolor'))
  154. $thumbImg = imagecreatetruecolor($width, $height);
  155. else
  156. $thumbImg = imagecreate($width, $height);
  157. // ????
  158. if(function_exists("ImageCopyResampled"))
  159. ImageCopyResampled($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth,$srcHeight);
  160. else
  161. ImageCopyResized($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth,$srcHeight);
  162. if('gif'==$type || 'png'==$type) {
  163. //imagealphablending($thumbImg, false);//?????????
  164. //imagesavealpha($thumbImg,true);//??????? alpha ????
  165. $background_color = imagecolorallocate($thumbImg, 0,255,0); // ??????
  166. imagecolortransparent($thumbImg,$background_color); // ????????????????????
  167. }
  168. // ?jpeg????????
  169. if('jpg'==$type || 'jpeg'==$type) imageinterlace($thumbImg,$interlace);
  170. //$gray=ImageColorAllocate($thumbImg,255,0,0);
  171. //ImageString($thumbImg,2,5,5,"ThinkPHP",$gray);
  172. // ????
  173. $imageFun = 'image'.($type=='jpg'?'jpeg':$type);
  174. $filename = empty($filename)? substr($image,0,strrpos($image, '.')).$suffix.'.'.$type : $filename;
  175. $imageFun($thumbImg,$filename);
  176. ImageDestroy($thumbImg);
  177. ImageDestroy($srcImg);
  178. return $filename;
  179. }
  180. return false;
  181. }
  182. /**
  183. +----------------------------------------------------------
  184. * ???????
  185. +----------------------------------------------------------
  186. * @static
  187. * @access public
  188. +----------------------------------------------------------
  189. * @param string $length ??
  190. * @param string $mode ??
  191. * @param string $type ????
  192. * @param string $width ??
  193. * @param string $height ??
  194. +----------------------------------------------------------
  195. * @return string
  196. +----------------------------------------------------------
  197. * @throws ThinkExecption
  198. +----------------------------------------------------------
  199. */
  200. static function buildImageVerify($length=4,$mode=1,$type='png',$width=48,$height=22,$verifyName='verify')
  201. {
  202. $randval = build_verify($length,$mode);
  203. $_SESSION[$verifyName]= md5($randval);
  204. $width = ($length*9+10)>$width?$length*9+10:$width;
  205. if ( $type!='gif' && function_exists('imagecreatetruecolor')) {
  206. $im = @imagecreatetruecolor($width,$height);
  207. }else {
  208. $im = @imagecreate($width,$height);
  209. }
  210. $r = Array(225,255,255,223);
  211. $g = Array(225,236,237,255);
  212. $b = Array(225,236,166,125);
  213. $key = mt_rand(0,3);
  214. $backColor = imagecolorallocate($im, $r[$key],$g[$key],$b[$key]); //???????
  215. $borderColor = imagecolorallocate($im, 100, 100, 100); //???
  216. $pointColor = imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); //???
  217. @imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
  218. @imagerectangle($im, 0, 0, $width-1, $height-1, $borderColor);
  219. $stringColor = imagecolorallocate($im,mt_rand(0,200),mt_rand(0,120),mt_rand(0,120));
  220. // ??
  221. for($i=0;$i<10;$i++){
  222. $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  223. imagearc($im,mt_rand(-10,$width),mt_rand(-10,$height),mt_rand(30,300),mt_rand(20,200),55,44,$fontcolor);
  224. }
  225. for($i=0;$i<25;$i++){
  226. $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  227. imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$pointColor);
  228. }
  229. @imagestring($im, 5, 5, 3, $randval, $stringColor);
  230. Image::output($im,$type);
  231. }
  232. // ?????
  233. static function GBVerify($length=4,$type='png',$width=180,$height=50,$fontface='simhei.ttf',$verifyName='verify') {
  234. $code = rand_string($length,4);
  235. $width = ($length*45)>$width?$length*45:$width;
  236. $_SESSION[$verifyName]= md5($code);
  237. $im=imagecreatetruecolor($width,$height);
  238. $borderColor = imagecolorallocate($im, 100, 100, 100); //???
  239. $bkcolor=imagecolorallocate($im,250,250,250);
  240. imagefill($im,0,0,$bkcolor);
  241. @imagerectangle($im, 0, 0, $width-1, $height-1, $borderColor);
  242. // ??
  243. for($i=0;$i<15;$i++){
  244. $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  245. imagearc($im,mt_rand(-10,$width),mt_rand(-10,$height),mt_rand(30,300),mt_rand(20,200),55,44,$fontcolor);
  246. }
  247. for($i=0;$i<255;$i++){
  248. $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  249. imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$fontcolor);
  250. }
  251. if(!is_file($fontface)) {
  252. $fontface = dirname(__FILE__)."/".$fontface;
  253. }
  254. for($i=0;$i<$length;$i++){
  255. $fontcolor=imagecolorallocate($im,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120)); //??????????????
  256. $codex= msubstr($code,$i,1);
  257. imagettftext($im,mt_rand(16,20),mt_rand(-60,60),40*$i+20,mt_rand(30,35),$fontcolor,$fontface,$codex);
  258. }
  259. Image::output($im,$type);
  260. }
  261. /**
  262. +----------------------------------------------------------
  263. * ??????????
  264. +----------------------------------------------------------
  265. * @static
  266. * @access public
  267. +----------------------------------------------------------
  268. * @param string $image ??????
  269. * @param string $type ???????????
  270. +----------------------------------------------------------
  271. * @return string
  272. +----------------------------------------------------------
  273. * @throws ThinkExecption
  274. +----------------------------------------------------------
  275. */
  276. static function showASCIIImg($image,$string='',$type='')
  277. {
  278. $info = Image::getImageInfo($image);
  279. if($info !== false) {
  280. $type = empty($type)?$info['type']:$type;
  281. unset($info);
  282. // ????
  283. $createFun = 'ImageCreateFrom'.($type=='jpg'?'jpeg':$type);
  284. $im = $createFun($image);
  285. $dx = imagesx($im);
  286. $dy = imagesy($im);
  287. $i = 0;
  288. $out = '<span style="padding:0px;margin:0;line-height:100%;font-size:1px;">';
  289. set_time_limit(0);
  290. for($y = 0; $y < $dy; $y++) {
  291. for($x=0; $x < $dx; $x++) {
  292. $col = imagecolorat($im, $x, $y);
  293. $rgb = imagecolorsforindex($im,$col);
  294. $str = empty($string)?'*':$string[$i++];
  295. $out .= sprintf('<span style="margin:0px;color:#%02x%02x%02x">'.$str.'</span>',$rgb['red'],$rgb['green'],$rgb['blue']);
  296. }
  297. $out .= "<br>\n";
  298. }
  299. $out .= '</span>';
  300. imagedestroy($im);
  301. return $out;
  302. }
  303. return false;
  304. }
  305. /**
  306. +----------------------------------------------------------
  307. * ?????????
  308. +----------------------------------------------------------
  309. * @static
  310. * @access public
  311. +----------------------------------------------------------
  312. * @param string $type ????
  313. * @param string $width ??
  314. * @param string $height ??
  315. +----------------------------------------------------------
  316. * @return string
  317. +----------------------------------------------------------
  318. * @throws ThinkExecption
  319. +----------------------------------------------------------
  320. */
  321. static function showAdvVerify($type='png',$width=180,$height=40)
  322. {
  323. $rand = range('a','z');
  324. shuffle($rand);
  325. $verifyCode = array_slice($rand,0,10);
  326. $letter = implode(" ",$verifyCode);
  327. $_SESSION['verifyCode'] = $verifyCode;
  328. $im = imagecreate($width,$height);
  329. $r = array(225,255,255,223);
  330. $g = array(225,236,237,255);
  331. $b = array(225,236,166,125);
  332. $key = mt_rand(0,3);
  333. $backColor = imagecolorallocate($im, $r[$key],$g[$key],$b[$key]);
  334. $borderColor = imagecolorallocate($im, 100, 100, 100); //???
  335. imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
  336. imagerectangle($im, 0, 0, $width-1, $height-1, $borderColor);
  337. $numberColor = imagecolorallocate($im, 255,rand(0,100), rand(0,100));
  338. $stringColor = imagecolorallocate($im, rand(0,100), rand(0,100), 255);
  339. // ????
  340. for($i=0;$i<10;$i++){
  341. $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  342. imagearc($im,mt_rand(-10,$width),mt_rand(-10,$height),mt_rand(30,300),mt_rand(20,200),55,44,$fontcolor);
  343. }
  344. for($i=0;$i<255;$i++){
  345. $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  346. imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$fontcolor);
  347. }
  348. imagestring($im, 5, 5, 1, "0 1 2 3 4 5 6 7 8 9", $numberColor);
  349. imagestring($im, 5, 5, 20, $letter, $stringColor);
  350. Image::output($im,$type);
  351. }
  352. /**
  353. +----------------------------------------------------------
  354. * ??UPC-A???
  355. +----------------------------------------------------------
  356. * @static
  357. +----------------------------------------------------------
  358. * @param string $type ????
  359. * @param string $type ????
  360. * @param string $lw ????
  361. * @param string $hi ????
  362. +----------------------------------------------------------
  363. * @return string
  364. +----------------------------------------------------------
  365. * @throws ThinkExecption
  366. +----------------------------------------------------------
  367. */
  368. static function UPCA($code,$type='png',$lw=2,$hi=100) {
  369. static $Lencode = array('0001101','0011001','0010011','0111101','0100011',
  370. '0110001','0101111','0111011','0110111','0001011');
  371. static $Rencode = array('1110010','1100110','1101100','1000010','1011100',
  372. '1001110','1010000','1000100','1001000','1110100');
  373. $ends = '101';
  374. $center = '01010';
  375. /* UPC-A Must be 11 digits, we compute the checksum. */
  376. if ( strlen($code) != 11 ) { die("UPC-A Must be 11 digits."); }
  377. /* Compute the EAN-13 Checksum digit */
  378. $ncode = '0'.$code;
  379. $even = 0; $odd = 0;
  380. for ($x=0;$x<12;$x++) {
  381. if ($x % 2) { $odd += $ncode[$x]; } else { $even += $ncode[$x]; }
  382. }
  383. $code.=(10 - (($odd * 3 + $even) % 10)) % 10;
  384. /* Create the bar encoding using a binary string */
  385. $bars=$ends;
  386. $bars.=$Lencode[$code[0]];
  387. for($x=1;$x<6;$x++) {
  388. $bars.=$Lencode[$code[$x]];
  389. }
  390. $bars.=$center;
  391. for($x=6;$x<12;$x++) {
  392. $bars.=$Rencode[$code[$x]];
  393. }
  394. $bars.=$ends;
  395. /* Generate the Barcode Image */
  396. if ( $type!='gif' && function_exists('imagecreatetruecolor')) {
  397. $im = imagecreatetruecolor($lw*95+30,$hi+30);
  398. }else {
  399. $im = imagecreate($lw*95+30,$hi+30);
  400. }
  401. $fg = ImageColorAllocate($im, 0, 0, 0);
  402. $bg = ImageColorAllocate($im, 255, 255, 255);
  403. ImageFilledRectangle($im, 0, 0, $lw*95+30, $hi+30, $bg);
  404. $shift=10;
  405. for ($x=0;$x<strlen($bars);$x++) {
  406. if (($x<10) || ($x>=45 && $x<50) || ($x >=85)) { $sh=10; } else { $sh=0; }
  407. if ($bars[$x] == '1') { $color = $fg; } else { $color = $bg; }
  408. ImageFilledRectangle($im, ($x*$lw)+15,5,($x+1)*$lw+14,$hi+5+$sh,$color);
  409. }
  410. /* Add the Human Readable Label */
  411. ImageString($im,4,5,$hi-5,$code[0],$fg);
  412. for ($x=0;$x<5;$x++) {
  413. ImageString($im,5,$lw*(13+$x*6)+15,$hi+5,$code[$x+1],$fg);
  414. ImageString($im,5,$lw*(53+$x*6)+15,$hi+5,$code[$x+6],$fg);
  415. }
  416. ImageString($im,4,$lw*95+17,$hi-5,$code[11],$fg);
  417. /* Output the Header and Content. */
  418. Image::output($im,$type);
  419. }
  420. /**
  421. +----------------------------------------------------------
  422. * ???????
  423. +----------------------------------------------------------
  424. * @static public
  425. +----------------------------------------------------------
  426. * @param string $source ????
  427. * @param string $water ????
  428. * @param string $$savename ?????????
  429. * @param string $alpha ??????
  430. +----------------------------------------------------------
  431. * @return string
  432. +----------------------------------------------------------
  433. * @throws ThinkExecption
  434. +----------------------------------------------------------
  435. */
  436. static public function water($source,$water,$savename=null,$alpha=80)
  437. {
  438. //????????
  439. if(!file_exists($source)||!file_exists($water))
  440. return false;
  441. //????
  442. $sInfo=self::getImageInfo($source);
  443. $wInfo=self::getImageInfo($water);
  444. //????????????????
  445. if($sInfo["width"]<$wInfo["width"] || $sInfo['height']<$wInfo['height'])
  446. return false;
  447. //????
  448. $sCreateFun="imagecreatefrom".$sInfo['type'];
  449. $sImage=$sCreateFun($source);
  450. $wCreateFun="imagecreatefrom".$wInfo['type'];
  451. $wImage=$wCreateFun($water);
  452. //?????????
  453. imagealphablending($wImage, true);
  454. //????,?????????
  455. $posY=$sInfo["height"]-$wInfo["height"];
  456. $posX=$sInfo["width"]-$wInfo["width"];
  457. //??????
  458. imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo['width'],$wInfo['height'],$alpha);
  459. //????
  460. $ImageFun='Image'.$sInfo['type'];
  461. //???????????????????
  462. if(!$savename){
  463. $savename=$source;
  464. @unlink($source);
  465. }
  466. //????
  467. $ImageFun($sImage,$savename);
  468. imagedestroy($sImage);
  469. }
  470. static function output($im,$type='png')
  471. {
  472. header("Content-type: image/".$type);
  473. $ImageFun='Image'.$type;
  474. $ImageFun($im);
  475. imagedestroy($im);
  476. }
  477. }//?????
  478. ?>