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

/Core/Extend/Library/ORG/Util/FrankeImage.class.php

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