PageRenderTime 43ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/Blog/Lib/ORG/Image.class.php

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