PageRenderTime 48ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/Extend/Library/ORG/Util/Image.class.php

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