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

/code/addons/libs/Image.class.php

http://thinksns-2.googlecode.com/
PHP | 502 lines | 304 code | 20 blank | 178 comment | 59 complexity | 585f94d5a433cdc688aa38eb6b2567a5 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  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. if( $maxHeight=='auto' ){
  132. $scale = $maxWidth/$srcWidth;
  133. }else{
  134. $scale = min($maxWidth/$srcWidth, $maxHeight/$srcHeight); // ??????
  135. }
  136. if($scale>=1) {
  137. // ??????????
  138. $width = $srcWidth;
  139. $height = $srcHeight;
  140. }else{
  141. // ?????
  142. $width = (int)($srcWidth*$scale);
  143. $height = (int)($srcHeight*$scale);
  144. }
  145. // ????
  146. $createFun = 'ImageCreateFrom'.($type=='jpg'?'jpeg':$type);
  147. $srcImg = $createFun($image);
  148. //?????
  149. if($type!='gif' && function_exists('imagecreatetruecolor'))
  150. $thumbImg = imagecreatetruecolor($width, $height);
  151. else
  152. $thumbImg = imagecreate($width, $height);
  153. // ????
  154. if(function_exists("ImageCopyResampled"))
  155. imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth,$srcHeight);
  156. else
  157. imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth,$srcHeight);
  158. if('gif'==$type || 'png'==$type) {
  159. //imagealphablending($thumbImg, false);//?????????
  160. //imagesavealpha($thumbImg,true);//??????? alpha ????
  161. $background_color = imagecolorallocate($thumbImg, 0,255,0); // ??????
  162. imagecolortransparent($thumbImg,$background_color); // ????????????????????
  163. }
  164. // ?jpeg????????
  165. if('jpg'==$type || 'jpeg'==$type) imageinterlace($thumbImg,$interlace);
  166. //$gray=ImageColorAllocate($thumbImg,255,0,0);
  167. //ImageString($thumbImg,2,5,5,"ThinkPHP",$gray);
  168. // ????
  169. $imageFun = 'image'.($type=='jpg'?'jpeg':$type);
  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.Util.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. import('ORG.Util.String');
  289. $code = String::rand_string($length,4);
  290. $width = ($length*45)>$width?$length*45:$width;
  291. $_SESSION[$verifyName]= md5($code);
  292. $im=imagecreatetruecolor($width,$height);
  293. $borderColor = imagecolorallocate($im, 100, 100, 100); //???
  294. $bkcolor=imagecolorallocate($im,250,250,250);
  295. imagefill($im,0,0,$bkcolor);
  296. @imagerectangle($im, 0, 0, $width-1, $height-1, $borderColor);
  297. // ??
  298. for($i=0;$i<15;$i++){
  299. $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  300. imagearc($im,mt_rand(-10,$width),mt_rand(-10,$height),mt_rand(30,300),mt_rand(20,200),55,44,$fontcolor);
  301. }
  302. for($i=0;$i<255;$i++){
  303. $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  304. imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$fontcolor);
  305. }
  306. if(!is_file($fontface)) {
  307. $fontface = dirname(__FILE__)."/".$fontface;
  308. }
  309. for($i=0;$i<$length;$i++){
  310. $fontcolor=imagecolorallocate($im,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120)); //??????????????
  311. $codex= msubstr($code,$i,1);
  312. imagettftext($im,mt_rand(16,20),mt_rand(-60,60),40*$i+20,mt_rand(30,35),$fontcolor,$fontface,$codex);
  313. }
  314. Image::output($im,$type);
  315. }
  316. /**
  317. +----------------------------------------------------------
  318. * ??????????
  319. +----------------------------------------------------------
  320. * @static
  321. * @access public
  322. +----------------------------------------------------------
  323. * @param string $image ??????
  324. * @param string $type ???????????
  325. +----------------------------------------------------------
  326. * @return string
  327. +----------------------------------------------------------
  328. */
  329. static function showASCIIImg($image,$string='',$type='')
  330. {
  331. $info = Image::getImageInfo($image);
  332. if($info !== false) {
  333. $type = empty($type)?$info['type']:$type;
  334. unset($info);
  335. // ????
  336. $createFun = 'ImageCreateFrom'.($type=='jpg'?'jpeg':$type);
  337. $im = $createFun($image);
  338. $dx = imagesx($im);
  339. $dy = imagesy($im);
  340. $i = 0;
  341. $out = '<span style="padding:0px;margin:0;line-height:100%;font-size:1px;">';
  342. set_time_limit(0);
  343. for($y = 0; $y < $dy; $y++) {
  344. for($x=0; $x < $dx; $x++) {
  345. $col = imagecolorat($im, $x, $y);
  346. $rgb = imagecolorsforindex($im,$col);
  347. $str = empty($string)?'*':$string[$i++];
  348. $out .= sprintf('<span style="margin:0px;color:#%02x%02x%02x">'.$str.'</span>',$rgb['red'],$rgb['green'],$rgb['blue']);
  349. }
  350. $out .= "<br>\n";
  351. }
  352. $out .= '</span>';
  353. imagedestroy($im);
  354. return $out;
  355. }
  356. return false;
  357. }
  358. /**
  359. +----------------------------------------------------------
  360. * ?????????
  361. +----------------------------------------------------------
  362. * @static
  363. * @access public
  364. +----------------------------------------------------------
  365. * @param string $type ????
  366. * @param string $width ??
  367. * @param string $height ??
  368. +----------------------------------------------------------
  369. * @return string
  370. +----------------------------------------------------------
  371. */
  372. static function showAdvVerify($type='png',$width=180,$height=40,$verifyName='verifyCode')
  373. {
  374. $rand = range('a','z');
  375. shuffle($rand);
  376. $verifyCode = array_slice($rand,0,10);
  377. $letter = implode(" ",$verifyCode);
  378. $_SESSION[$verifyName] = $verifyCode;
  379. $im = imagecreate($width,$height);
  380. $r = array(225,255,255,223);
  381. $g = array(225,236,237,255);
  382. $b = array(225,236,166,125);
  383. $key = mt_rand(0,3);
  384. $backColor = imagecolorallocate($im, $r[$key],$g[$key],$b[$key]);
  385. $borderColor = imagecolorallocate($im, 100, 100, 100); //???
  386. imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
  387. imagerectangle($im, 0, 0, $width-1, $height-1, $borderColor);
  388. $numberColor = imagecolorallocate($im, 255,rand(0,100), rand(0,100));
  389. $stringColor = imagecolorallocate($im, rand(0,100), rand(0,100), 255);
  390. // ????
  391. /*
  392. for($i=0;$i<10;$i++){
  393. $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  394. imagearc($im,mt_rand(-10,$width),mt_rand(-10,$height),mt_rand(30,300),mt_rand(20,200),55,44,$fontcolor);
  395. }
  396. for($i=0;$i<255;$i++){
  397. $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  398. imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$fontcolor);
  399. }*/
  400. imagestring($im, 5, 5, 1, "0 1 2 3 4 5 6 7 8 9", $numberColor);
  401. imagestring($im, 5, 5, 20, $letter, $stringColor);
  402. Image::output($im,$type);
  403. }
  404. /**
  405. +----------------------------------------------------------
  406. * ??UPC-A???
  407. +----------------------------------------------------------
  408. * @static
  409. +----------------------------------------------------------
  410. * @param string $type ????
  411. * @param string $type ????
  412. * @param string $lw ????
  413. * @param string $hi ????
  414. +----------------------------------------------------------
  415. * @return string
  416. +----------------------------------------------------------
  417. */
  418. static function UPCA($code,$type='png',$lw=2,$hi=100) {
  419. static $Lencode = array('0001101','0011001','0010011','0111101','0100011',
  420. '0110001','0101111','0111011','0110111','0001011');
  421. static $Rencode = array('1110010','1100110','1101100','1000010','1011100',
  422. '1001110','1010000','1000100','1001000','1110100');
  423. $ends = '101';
  424. $center = '01010';
  425. /* UPC-A Must be 11 digits, we compute the checksum. */
  426. if ( strlen($code) != 11 ) { die("UPC-A Must be 11 digits."); }
  427. /* Compute the EAN-13 Checksum digit */
  428. $ncode = '0'.$code;
  429. $even = 0; $odd = 0;
  430. for ($x=0;$x<12;$x++) {
  431. if ($x % 2) { $odd += $ncode[$x]; } else { $even += $ncode[$x]; }
  432. }
  433. $code.=(10 - (($odd * 3 + $even) % 10)) % 10;
  434. /* Create the bar encoding using a binary string */
  435. $bars=$ends;
  436. $bars.=$Lencode[$code[0]];
  437. for($x=1;$x<6;$x++) {
  438. $bars.=$Lencode[$code[$x]];
  439. }
  440. $bars.=$center;
  441. for($x=6;$x<12;$x++) {
  442. $bars.=$Rencode[$code[$x]];
  443. }
  444. $bars.=$ends;
  445. /* Generate the Barcode Image */
  446. if ( $type!='gif' && function_exists('imagecreatetruecolor')) {
  447. $im = imagecreatetruecolor($lw*95+30,$hi+30);
  448. }else {
  449. $im = imagecreate($lw*95+30,$hi+30);
  450. }
  451. $fg = ImageColorAllocate($im, 0, 0, 0);
  452. $bg = ImageColorAllocate($im, 255, 255, 255);
  453. ImageFilledRectangle($im, 0, 0, $lw*95+30, $hi+30, $bg);
  454. $shift=10;
  455. for ($x=0;$x<strlen($bars);$x++) {
  456. if (($x<10) || ($x>=45 && $x<50) || ($x >=85)) { $sh=10; } else { $sh=0; }
  457. if ($bars[$x] == '1') { $color = $fg; } else { $color = $bg; }
  458. ImageFilledRectangle($im, ($x*$lw)+15,5,($x+1)*$lw+14,$hi+5+$sh,$color);
  459. }
  460. /* Add the Human Readable Label */
  461. ImageString($im,4,5,$hi-5,$code[0],$fg);
  462. for ($x=0;$x<5;$x++) {
  463. ImageString($im,5,$lw*(13+$x*6)+15,$hi+5,$code[$x+1],$fg);
  464. ImageString($im,5,$lw*(53+$x*6)+15,$hi+5,$code[$x+6],$fg);
  465. }
  466. ImageString($im,4,$lw*95+17,$hi-5,$code[11],$fg);
  467. /* Output the Header and Content. */
  468. Image::output($im,$type);
  469. }
  470. static function output($im,$type='png',$filename='')
  471. {
  472. header("Content-type: image/".$type);
  473. $ImageFun='image'.$type;
  474. if(empty($filename)) {
  475. $ImageFun($im);
  476. }else{
  477. $ImageFun($im,$filename);
  478. }
  479. imagedestroy($im);
  480. }
  481. }//?????
  482. ?>