PageRenderTime 54ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/App/ThinkPHP/Lib/ORG/Util/Image.class.php

https://github.com/udonmai/cippus
PHP | 576 lines | 346 code | 32 blank | 198 comment | 86 complexity | 601ed3c1ca72286ec73d70609526b4c3 MD5 | raw file
Possible License(s): Apache-2.0
  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. * @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::rand_string($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. $pointColor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //点颜色
  317. @imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
  318. @imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor);
  319. $stringColor = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));
  320. // 干扰
  321. for ($i = 0; $i < 10; $i++) {
  322. $fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  323. imagearc($im, mt_rand(-10, $width), mt_rand(-10, $height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $fontcolor);
  324. }
  325. for ($i = 0; $i < 25; $i++) {
  326. $fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  327. imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $pointColor);
  328. }
  329. for ($i = 0; $i < $length; $i++) {
  330. imagestring($im, 5, $i * 10 + 5, mt_rand(1, 8), $randval{$i}, $stringColor);
  331. }
  332. // @imagestring($im, 5, 5, 3, $randval, $stringColor);
  333. Image::output($im, $type);
  334. }
  335. // 中文验证码
  336. static function GBVerify($length=4, $type='png', $width=180, $height=50, $fontface='simhei.ttf', $verifyName='verify') {
  337. import('ORG.Util.String');
  338. $code = String::rand_string($length, 4);
  339. $width = ($length * 45) > $width ? $length * 45 : $width;
  340. $_SESSION[$verifyName] = md5($code);
  341. $im = imagecreatetruecolor($width, $height);
  342. $borderColor = imagecolorallocate($im, 100, 100, 100); //边框色
  343. $bkcolor = imagecolorallocate($im, 250, 250, 250);
  344. imagefill($im, 0, 0, $bkcolor);
  345. @imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor);
  346. // 干扰
  347. for ($i = 0; $i < 15; $i++) {
  348. $fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  349. imagearc($im, mt_rand(-10, $width), mt_rand(-10, $height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $fontcolor);
  350. }
  351. for ($i = 0; $i < 255; $i++) {
  352. $fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  353. imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $fontcolor);
  354. }
  355. if (!is_file($fontface)) {
  356. $fontface = dirname(__FILE__) . "/" . $fontface;
  357. }
  358. for ($i = 0; $i < $length; $i++) {
  359. $fontcolor = imagecolorallocate($im, mt_rand(0, 120), mt_rand(0, 120), mt_rand(0, 120)); //这样保证随机出来的颜色较深。
  360. $codex = msubstr($code, $i, 1);
  361. imagettftext($im, mt_rand(16, 20), mt_rand(-60, 60), 40 * $i + 20, mt_rand(30, 35), $fontcolor, $fontface, $codex);
  362. }
  363. Image::output($im, $type);
  364. }
  365. /**
  366. +----------------------------------------------------------
  367. * 把图像转换成字符显示
  368. +----------------------------------------------------------
  369. * @static
  370. * @access public
  371. +----------------------------------------------------------
  372. * @param string $image 要显示的图像
  373. * @param string $type 图像类型,默认自动获取
  374. +----------------------------------------------------------
  375. * @return string
  376. +----------------------------------------------------------
  377. */
  378. static function showASCIIImg($image, $string='', $type='') {
  379. $info = Image::getImageInfo($image);
  380. if ($info !== false) {
  381. $type = empty($type) ? $info['type'] : $type;
  382. unset($info);
  383. // 载入原图
  384. $createFun = 'ImageCreateFrom' . ($type == 'jpg' ? 'jpeg' : $type);
  385. $im = $createFun($image);
  386. $dx = imagesx($im);
  387. $dy = imagesy($im);
  388. $i = 0;
  389. $out = '<span style="padding:0px;margin:0;line-height:100%;font-size:1px;">';
  390. set_time_limit(0);
  391. for ($y = 0; $y < $dy; $y++) {
  392. for ($x = 0; $x < $dx; $x++) {
  393. $col = imagecolorat($im, $x, $y);
  394. $rgb = imagecolorsforindex($im, $col);
  395. $str = empty($string) ? '*' : $string[$i++];
  396. $out .= sprintf('<span style="margin:0px;color:#%02x%02x%02x">' . $str . '</span>', $rgb['red'], $rgb['green'], $rgb['blue']);
  397. }
  398. $out .= "<br>\n";
  399. }
  400. $out .= '</span>';
  401. imagedestroy($im);
  402. return $out;
  403. }
  404. return false;
  405. }
  406. /**
  407. +----------------------------------------------------------
  408. * 生成高级图像验证码
  409. +----------------------------------------------------------
  410. * @static
  411. * @access public
  412. +----------------------------------------------------------
  413. * @param string $type 图像格式
  414. * @param string $width 宽度
  415. * @param string $height 高度
  416. +----------------------------------------------------------
  417. * @return string
  418. +----------------------------------------------------------
  419. */
  420. static function showAdvVerify($type='png', $width=180, $height=40, $verifyName='verifyCode') {
  421. $rand = range('a', 'z');
  422. shuffle($rand);
  423. $verifyCode = array_slice($rand, 0, 10);
  424. $letter = implode(" ", $verifyCode);
  425. $_SESSION[$verifyName] = $verifyCode;
  426. $im = imagecreate($width, $height);
  427. $r = array(225, 255, 255, 223);
  428. $g = array(225, 236, 237, 255);
  429. $b = array(225, 236, 166, 125);
  430. $key = mt_rand(0, 3);
  431. $backColor = imagecolorallocate($im, $r[$key], $g[$key], $b[$key]);
  432. $borderColor = imagecolorallocate($im, 100, 100, 100); //边框色
  433. imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
  434. imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor);
  435. $numberColor = imagecolorallocate($im, 255, rand(0, 100), rand(0, 100));
  436. $stringColor = imagecolorallocate($im, rand(0, 100), rand(0, 100), 255);
  437. // 添加干扰
  438. /*
  439. for($i=0;$i<10;$i++){
  440. $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  441. imagearc($im,mt_rand(-10,$width),mt_rand(-10,$height),mt_rand(30,300),mt_rand(20,200),55,44,$fontcolor);
  442. }
  443. for($i=0;$i<255;$i++){
  444. $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  445. imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$fontcolor);
  446. } */
  447. imagestring($im, 5, 5, 1, "0 1 2 3 4 5 6 7 8 9", $numberColor);
  448. imagestring($im, 5, 5, 20, $letter, $stringColor);
  449. Image::output($im, $type);
  450. }
  451. /**
  452. +----------------------------------------------------------
  453. * 生成UPC-A条形码
  454. +----------------------------------------------------------
  455. * @static
  456. +----------------------------------------------------------
  457. * @param string $type 图像格式
  458. * @param string $type 图像格式
  459. * @param string $lw 单元宽度
  460. * @param string $hi 条码高度
  461. +----------------------------------------------------------
  462. * @return string
  463. +----------------------------------------------------------
  464. */
  465. static function UPCA($code, $type='png', $lw=2, $hi=100) {
  466. static $Lencode = array('0001101', '0011001', '0010011', '0111101', '0100011',
  467. '0110001', '0101111', '0111011', '0110111', '0001011');
  468. static $Rencode = array('1110010', '1100110', '1101100', '1000010', '1011100',
  469. '1001110', '1010000', '1000100', '1001000', '1110100');
  470. $ends = '101';
  471. $center = '01010';
  472. /* UPC-A Must be 11 digits, we compute the checksum. */
  473. if (strlen($code) != 11) {
  474. die("UPC-A Must be 11 digits.");
  475. }
  476. /* Compute the EAN-13 Checksum digit */
  477. $ncode = '0' . $code;
  478. $even = 0;
  479. $odd = 0;
  480. for ($x = 0; $x < 12; $x++) {
  481. if ($x % 2) {
  482. $odd += $ncode[$x];
  483. } else {
  484. $even += $ncode[$x];
  485. }
  486. }
  487. $code.= ( 10 - (($odd * 3 + $even) % 10)) % 10;
  488. /* Create the bar encoding using a binary string */
  489. $bars = $ends;
  490. $bars.=$Lencode[$code[0]];
  491. for ($x = 1; $x < 6; $x++) {
  492. $bars.=$Lencode[$code[$x]];
  493. }
  494. $bars.=$center;
  495. for ($x = 6; $x < 12; $x++) {
  496. $bars.=$Rencode[$code[$x]];
  497. }
  498. $bars.=$ends;
  499. /* Generate the Barcode Image */
  500. if ($type != 'gif' && function_exists('imagecreatetruecolor')) {
  501. $im = imagecreatetruecolor($lw * 95 + 30, $hi + 30);
  502. } else {
  503. $im = imagecreate($lw * 95 + 30, $hi + 30);
  504. }
  505. $fg = ImageColorAllocate($im, 0, 0, 0);
  506. $bg = ImageColorAllocate($im, 255, 255, 255);
  507. ImageFilledRectangle($im, 0, 0, $lw * 95 + 30, $hi + 30, $bg);
  508. $shift = 10;
  509. for ($x = 0; $x < strlen($bars); $x++) {
  510. if (($x < 10) || ($x >= 45 && $x < 50) || ($x >= 85)) {
  511. $sh = 10;
  512. } else {
  513. $sh = 0;
  514. }
  515. if ($bars[$x] == '1') {
  516. $color = $fg;
  517. } else {
  518. $color = $bg;
  519. }
  520. ImageFilledRectangle($im, ($x * $lw) + 15, 5, ($x + 1) * $lw + 14, $hi + 5 + $sh, $color);
  521. }
  522. /* Add the Human Readable Label */
  523. ImageString($im, 4, 5, $hi - 5, $code[0], $fg);
  524. for ($x = 0; $x < 5; $x++) {
  525. ImageString($im, 5, $lw * (13 + $x * 6) + 15, $hi + 5, $code[$x + 1], $fg);
  526. ImageString($im, 5, $lw * (53 + $x * 6) + 15, $hi + 5, $code[$x + 6], $fg);
  527. }
  528. ImageString($im, 4, $lw * 95 + 17, $hi - 5, $code[11], $fg);
  529. /* Output the Header and Content. */
  530. Image::output($im, $type);
  531. }
  532. static function output($im, $type='png', $filename='') {
  533. header("Content-type: image/" . $type);
  534. $ImageFun = 'image' . $type;
  535. if (empty($filename)) {
  536. $ImageFun($im);
  537. } else {
  538. $ImageFun($im, $filename);
  539. }
  540. imagedestroy($im);
  541. }
  542. }
  543. //类定义结束
  544. ?>