PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Inc/Lib/ORG/Util/Image.class.php

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