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

/Extend/Engine/Sae/Lib/Extend/Library/ORG/Util/Image_sae.class.php

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