PageRenderTime 52ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/startZine/src/main/admin/Lib/ORG/Image.class.php

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