PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/Examples/Rbac/Lib/ORG/Util/Image.class.php

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