Image.class.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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. /**
  12. * 图像操作类库
  13. * @category ORG
  14. * @package ORG
  15. * @subpackage Util
  16. * @author liu21st <liu21st@gmail.com>
  17. */
  18. class Image {
  19. /**
  20. * 取得图像信息
  21. * @static
  22. * @access public
  23. * @param string $image 图像文件名
  24. * @return mixed
  25. */
  26. static function getImageInfo($img) {
  27. $imageInfo = getimagesize($img);
  28. if ($imageInfo !== false) {
  29. $imageType = strtolower(substr(image_type_to_extension($imageInfo[2]), 1));
  30. $imageSize = filesize($img);
  31. $info = array(
  32. "width" => $imageInfo[0],
  33. "height" => $imageInfo[1],
  34. "type" => $imageType,
  35. "size" => $imageSize,
  36. "mime" => $imageInfo['mime']
  37. );
  38. return $info;
  39. } else {
  40. return false;
  41. }
  42. }
  43. /**
  44. * 为图片添加水印
  45. * @static public
  46. * @param string $source 原文件名
  47. * @param string $water 水印图片
  48. * @param string $$savename 添加水印后的图片名
  49. * @param string $alpha 水印的透明度
  50. * @return void
  51. */
  52. static public function water($source, $water, $savename=null, $alpha=80) {
  53. //检查文件是否存在
  54. if (!file_exists($source) || !file_exists($water))
  55. return false;
  56. //图片信息
  57. $sInfo = self::getImageInfo($source);
  58. $wInfo = self::getImageInfo($water);
  59. //如果图片小于水印图片,不生成图片
  60. if ($sInfo["width"] < $wInfo["width"] || $sInfo['height'] < $wInfo['height'])
  61. return false;
  62. //建立图像
  63. $sCreateFun = "imagecreatefrom" . $sInfo['type'];
  64. $sImage = $sCreateFun($source);
  65. $wCreateFun = "imagecreatefrom" . $wInfo['type'];
  66. $wImage = $wCreateFun($water);
  67. //设定图像的混色模式
  68. imagealphablending($wImage, true);
  69. //图像位置,默认为右下角右对齐
  70. $posY = $sInfo["height"] - $wInfo["height"];
  71. $posX = $sInfo["width"] - $wInfo["width"];
  72. //生成混合图像
  73. imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo['width'], $wInfo['height'], $alpha);
  74. //输出图像
  75. $ImageFun = 'Image' . $sInfo['type'];
  76. //如果没有给出保存文件名,默认为原图像名
  77. if (!$savename) {
  78. $savename = $source;
  79. @unlink($source);
  80. }
  81. //保存图像
  82. $ImageFun($sImage, $savename);
  83. imagedestroy($sImage);
  84. }
  85. function showImg($imgFile, $text='', $x='10', $y='10', $alpha='50') {
  86. //获取图像文件信息
  87. //2007/6/26 增加图片水印输出,$text为图片的完整路径即可
  88. $info = Image::getImageInfo($imgFile);
  89. if ($info !== false) {
  90. $createFun = str_replace('/', 'createfrom', $info['mime']);
  91. $im = $createFun($imgFile);
  92. if ($im) {
  93. $ImageFun = str_replace('/', '', $info['mime']);
  94. //水印开始
  95. if (!empty($text)) {
  96. $tc = imagecolorallocate($im, 0, 0, 0);
  97. if (is_file($text) && file_exists($text)) {//判断$text是否是图片路径
  98. // 取得水印信息
  99. $textInfo = Image::getImageInfo($text);
  100. $createFun2 = str_replace('/', 'createfrom', $textInfo['mime']);
  101. $waterMark = $createFun2($text);
  102. //$waterMark=imagecolorallocatealpha($text,255,255,0,50);
  103. $imgW = $info["width"];
  104. $imgH = $info["width"] * $textInfo["height"] / $textInfo["width"];
  105. //$y = ($info["height"]-$textInfo["height"])/2;
  106. //设置水印的显示位置和透明度支持各种图片格式
  107. imagecopymerge($im, $waterMark, $x, $y, 0, 0, $textInfo['width'], $textInfo['height'], $alpha);
  108. } else {
  109. imagestring($im, 80, $x, $y, $text, $tc);
  110. }
  111. //ImageDestroy($tc);
  112. }
  113. //水印结束
  114. if ($info['type'] == 'png' || $info['type'] == 'gif') {
  115. imagealphablending($im, FALSE); //取消默认的混色模式
  116. imagesavealpha($im, TRUE); //设定保存完整的 alpha 通道信息
  117. }
  118. Header("Content-type: " . $info['mime']);
  119. $ImageFun($im);
  120. @ImageDestroy($im);
  121. return;
  122. }
  123. //保存图像
  124. $ImageFun($sImage, $savename);
  125. imagedestroy($sImage);
  126. //获取或者创建图像文件失败则生成空白PNG图片
  127. $im = imagecreatetruecolor(80, 30);
  128. $bgc = imagecolorallocate($im, 255, 255, 255);
  129. $tc = imagecolorallocate($im, 0, 0, 0);
  130. imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
  131. imagestring($im, 4, 5, 5, "no pic", $tc);
  132. Image::output($im);
  133. return;
  134. }
  135. }
  136. /**
  137. * 生成缩略图
  138. * @static
  139. * @access public
  140. * @param string $image 原图
  141. * @param string $type 图像格式
  142. * @param string $thumbname 缩略图文件名
  143. * @param string $maxWidth 宽度
  144. * @param string $maxHeight 高度
  145. * @param string $position 缩略图保存目录
  146. * @param boolean $interlace 启用隔行扫描
  147. * @return void
  148. */
  149. static function thumb($image, $thumbname, $type='', $maxWidth=200, $maxHeight=50, $interlace=true) {
  150. // 获取原图信息
  151. $info = Image::getImageInfo($image);
  152. if ($info !== false) {
  153. $srcWidth = $info['width'];
  154. $srcHeight = $info['height'];
  155. $type = empty($type) ? $info['type'] : $type;
  156. $type = strtolower($type);
  157. $interlace = $interlace ? 1 : 0;
  158. unset($info);
  159. $scale = min($maxWidth / $srcWidth, $maxHeight / $srcHeight); // 计算缩放比例
  160. if ($scale >= 1) {
  161. // 超过原图大小不再缩略
  162. $width = $srcWidth;
  163. $height = $srcHeight;
  164. } else {
  165. // 缩略图尺寸
  166. $width = (int) ($srcWidth * $scale);
  167. $height = (int) ($srcHeight * $scale);
  168. }
  169. // 载入原图
  170. $createFun = 'ImageCreateFrom' . ($type == 'jpg' ? 'jpeg' : $type);
  171. if(!function_exists($createFun)) {
  172. return false;
  173. }
  174. $srcImg = $createFun($image);
  175. //创建缩略图
  176. if ($type != 'gif' && function_exists('imagecreatetruecolor'))
  177. $thumbImg = imagecreatetruecolor($width, $height);
  178. else
  179. $thumbImg = imagecreate($width, $height);
  180. //png和gif的透明处理 by luofei614
  181. if('png'==$type){
  182. imagealphablending($thumbImg, false);//取消默认的混色模式(为解决阴影为绿色的问题)
  183. imagesavealpha($thumbImg,true);//设定保存完整的 alpha 通道信息(为解决阴影为绿色的问题)
  184. }elseif('gif'==$type){
  185. $trnprt_indx = imagecolortransparent($srcImg);
  186. if ($trnprt_indx >= 0) {
  187. //its transparent
  188. $trnprt_color = imagecolorsforindex($srcImg , $trnprt_indx);
  189. $trnprt_indx = imagecolorallocate($thumbImg, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
  190. imagefill($thumbImg, 0, 0, $trnprt_indx);
  191. imagecolortransparent($thumbImg, $trnprt_indx);
  192. }
  193. }
  194. // 复制图片
  195. if (function_exists("ImageCopyResampled"))
  196. imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight);
  197. else
  198. imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight);
  199. // 对jpeg图形设置隔行扫描
  200. if ('jpg' == $type || 'jpeg' == $type)
  201. imageinterlace($thumbImg, $interlace);
  202. // 生成图片
  203. $imageFun = 'image' . ($type == 'jpg' ? 'jpeg' : $type);
  204. $imageFun($thumbImg, $thumbname);
  205. imagedestroy($thumbImg);
  206. imagedestroy($srcImg);
  207. return $thumbname;
  208. }
  209. return false;
  210. }
  211. /**
  212. * 生成特定尺寸缩略图 解决原版缩略图不能满足特定尺寸的问题 PS:会裁掉图片不符合缩略图比例的部分
  213. * @static
  214. * @access public
  215. * @param string $image 原图
  216. * @param string $type 图像格式
  217. * @param string $thumbname 缩略图文件名
  218. * @param string $maxWidth 宽度
  219. * @param string $maxHeight 高度
  220. * @param boolean $interlace 启用隔行扫描
  221. * @return void
  222. */
  223. static function thumb2($image, $thumbname, $type='', $maxWidth=200, $maxHeight=50, $interlace=true) {
  224. // 获取原图信息
  225. $info = Image::getImageInfo($image);
  226. if ($info !== false) {
  227. $srcWidth = $info['width'];
  228. $srcHeight = $info['height'];
  229. $type = empty($type) ? $info['type'] : $type;
  230. $type = strtolower($type);
  231. $interlace = $interlace ? 1 : 0;
  232. unset($info);
  233. $scale = max($maxWidth / $srcWidth, $maxHeight / $srcHeight); // 计算缩放比例
  234. //判断原图和缩略图比例 如原图宽于缩略图则裁掉两边 反之..
  235. if($maxWidth / $srcWidth > $maxHeight / $srcHeight){
  236. //高于
  237. $srcX = 0;
  238. $srcY = ($srcHeight - $maxHeight / $scale) / 2 ;
  239. $cutWidth = $srcWidth;
  240. $cutHeight = $maxHeight / $scale;
  241. }else{
  242. //宽于
  243. $srcX = ($srcWidth - $maxWidth / $scale) / 2;
  244. $srcY = 0;
  245. $cutWidth = $maxWidth / $scale;
  246. $cutHeight = $srcHeight;
  247. }
  248. // 载入原图
  249. $createFun = 'ImageCreateFrom' . ($type == 'jpg' ? 'jpeg' : $type);
  250. $srcImg = $createFun($image);
  251. //创建缩略图
  252. if ($type != 'gif' && function_exists('imagecreatetruecolor'))
  253. $thumbImg = imagecreatetruecolor($maxWidth, $maxHeight);
  254. else
  255. $thumbImg = imagecreate($maxWidth, $maxHeight);
  256. // 复制图片
  257. if (function_exists("ImageCopyResampled"))
  258. imagecopyresampled($thumbImg, $srcImg, 0, 0, $srcX, $srcY, $maxWidth, $maxHeight, $cutWidth, $cutHeight);
  259. else
  260. imagecopyresized($thumbImg, $srcImg, 0, 0, $srcX, $srcY, $maxWidth, $maxHeight, $cutWidth, $cutHeight);
  261. if ('gif' == $type || 'png' == $type) {
  262. //imagealphablending($thumbImg, false);//取消默认的混色模式
  263. //imagesavealpha($thumbImg,true);//设定保存完整的 alpha 通道信息
  264. $background_color = imagecolorallocate($thumbImg, 0, 255, 0); // 指派一个绿色
  265. imagecolortransparent($thumbImg, $background_color); // 设置为透明色,若注释掉该行则输出绿色的图
  266. }
  267. // 对jpeg图形设置隔行扫描
  268. if ('jpg' == $type || 'jpeg' == $type)
  269. imageinterlace($thumbImg, $interlace);
  270. // 生成图片
  271. $imageFun = 'image' . ($type == 'jpg' ? 'jpeg' : $type);
  272. $imageFun($thumbImg, $thumbname);
  273. imagedestroy($thumbImg);
  274. imagedestroy($srcImg);
  275. return $thumbname;
  276. }
  277. return false;
  278. }
  279. /**
  280. * 根据给定的字符串生成图像
  281. * @static
  282. * @access public
  283. * @param string $string 字符串
  284. * @param string $size 图像大小 width,height 或者 array(width,height)
  285. * @param string $font 字体信息 fontface,fontsize 或者 array(fontface,fontsize)
  286. * @param string $type 图像格式 默认PNG
  287. * @param integer $disturb 是否干扰 1 点干扰 2 线干扰 3 复合干扰 0 无干扰
  288. * @param bool $border 是否加边框 array(color)
  289. * @return string
  290. */
  291. static function buildString($string, $rgb=array(), $filename='', $type='png', $disturb=1, $border=true) {
  292. if (is_string($size))
  293. $size = explode(',', $size);
  294. $width = $size[0];
  295. $height = $size[1];
  296. if (is_string($font))
  297. $font = explode(',', $font);
  298. $fontface = $font[0];
  299. $fontsize = $font[1];
  300. $length = strlen($string);
  301. $width = ($length * 9 + 10) > $width ? $length * 9 + 10 : $width;
  302. $height = 22;
  303. if ($type != 'gif' && function_exists('imagecreatetruecolor')) {
  304. $im = @imagecreatetruecolor($width, $height);
  305. } else {
  306. $im = @imagecreate($width, $height);
  307. }
  308. if (empty($rgb)) {
  309. $color = imagecolorallocate($im, 102, 104, 104);
  310. } else {
  311. $color = imagecolorallocate($im, $rgb[0], $rgb[1], $rgb[2]);
  312. }
  313. $backColor = imagecolorallocate($im, 255, 255, 255); //背景色(随机)
  314. $borderColor = imagecolorallocate($im, 100, 100, 100); //边框色
  315. $pointColor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //点颜色
  316. @imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
  317. @imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor);
  318. @imagestring($im, 5, 5, 3, $string, $color);
  319. if (!empty($disturb)) {
  320. // 添加干扰
  321. if ($disturb = 1 || $disturb = 3) {
  322. for ($i = 0; $i < 25; $i++) {
  323. imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $pointColor);
  324. }
  325. } elseif ($disturb = 2 || $disturb = 3) {
  326. for ($i = 0; $i < 10; $i++) {
  327. imagearc($im, mt_rand(-10, $width), mt_rand(-10, $height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $pointColor);
  328. }
  329. }
  330. }
  331. Image::output($im, $type, $filename);
  332. }
  333. /**
  334. * 生成图像验证码
  335. * @static
  336. * @access public
  337. * @param string $length 位数
  338. * @param string $mode 类型
  339. * @param string $type 图像格式
  340. * @param string $width 宽度
  341. * @param string $height 高度
  342. * @return string
  343. */
  344. static function buildImageVerify($length=4, $mode=1, $type='png', $width=48, $height=22, $verifyName='verify') {
  345. import('ORG.Util.String');
  346. $randval = String::randString($length, $mode);
  347. session($verifyName, md5($randval));
  348. $width = ($length * 10 + 10) > $width ? $length * 10 + 10 : $width;
  349. if ($type != 'gif' && function_exists('imagecreatetruecolor')) {
  350. $im = imagecreatetruecolor($width, $height);
  351. } else {
  352. $im = imagecreate($width, $height);
  353. }
  354. $r = Array(225, 255, 255, 223);
  355. $g = Array(225, 236, 237, 255);
  356. $b = Array(225, 236, 166, 125);
  357. $key = mt_rand(0, 3);
  358. $backColor = imagecolorallocate($im, $r[$key], $g[$key], $b[$key]); //背景色(随机)
  359. $borderColor = imagecolorallocate($im, 100, 100, 100); //边框色
  360. imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
  361. imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor);
  362. $stringColor = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));
  363. // 干扰
  364. for ($i = 0; $i < 10; $i++) {
  365. imagearc($im, mt_rand(-10, $width), mt_rand(-10, $height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $stringColor);
  366. }
  367. for ($i = 0; $i < 25; $i++) {
  368. imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $stringColor);
  369. }
  370. for ($i = 0; $i < $length; $i++) {
  371. imagestring($im, 5, $i * 10 + 5, mt_rand(1, 8), $randval{$i}, $stringColor);
  372. }
  373. Image::output($im, $type);
  374. }
  375. // 中文验证码
  376. static function GBVerify($length=4, $type='png', $width=180, $height=50, $fontface='simhei.ttf', $verifyName='verify') {
  377. import('ORG.Util.String');
  378. $code = String::randString($length, 4);
  379. $width = ($length * 45) > $width ? $length * 45 : $width;
  380. session($verifyName, md5($code));
  381. $im = imagecreatetruecolor($width, $height);
  382. $borderColor = imagecolorallocate($im, 100, 100, 100); //边框色
  383. $bkcolor = imagecolorallocate($im, 250, 250, 250);
  384. imagefill($im, 0, 0, $bkcolor);
  385. @imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor);
  386. // 干扰
  387. for ($i = 0; $i < 15; $i++) {
  388. $fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  389. imagearc($im, mt_rand(-10, $width), mt_rand(-10, $height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $fontcolor);
  390. }
  391. for ($i = 0; $i < 255; $i++) {
  392. $fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  393. imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $fontcolor);
  394. }
  395. if (!is_file($fontface)) {
  396. $fontface = dirname(__FILE__) . "/" . $fontface;
  397. }
  398. for ($i = 0; $i < $length; $i++) {
  399. $fontcolor = imagecolorallocate($im, mt_rand(0, 120), mt_rand(0, 120), mt_rand(0, 120)); //这样保证随机出来的颜色较深。
  400. $codex = String::msubstr($code, $i, 1);
  401. imagettftext($im, mt_rand(16, 20), mt_rand(-60, 60), 40 * $i + 20, mt_rand(30, 35), $fontcolor, $fontface, $codex);
  402. }
  403. Image::output($im, $type);
  404. }
  405. /**
  406. * 把图像转换成字符显示
  407. * @static
  408. * @access public
  409. * @param string $image 要显示的图像
  410. * @param string $type 图像类型,默认自动获取
  411. * @return string
  412. */
  413. static function showASCIIImg($image, $string='', $type='') {
  414. $info = Image::getImageInfo($image);
  415. if ($info !== false) {
  416. $type = empty($type) ? $info['type'] : $type;
  417. unset($info);
  418. // 载入原图
  419. $createFun = 'ImageCreateFrom' . ($type == 'jpg' ? 'jpeg' : $type);
  420. $im = $createFun($image);
  421. $dx = imagesx($im);
  422. $dy = imagesy($im);
  423. $i = 0;
  424. $out = '<span style="padding:0px;margin:0;line-height:100%;font-size:1px;">';
  425. set_time_limit(0);
  426. for ($y = 0; $y < $dy; $y++) {
  427. for ($x = 0; $x < $dx; $x++) {
  428. $col = imagecolorat($im, $x, $y);
  429. $rgb = imagecolorsforindex($im, $col);
  430. $str = empty($string) ? '*' : $string[$i++];
  431. $out .= sprintf('<span style="margin:0px;color:#%02x%02x%02x">' . $str . '</span>', $rgb['red'], $rgb['green'], $rgb['blue']);
  432. }
  433. $out .= "<br>\n";
  434. }
  435. $out .= '</span>';
  436. imagedestroy($im);
  437. return $out;
  438. }
  439. return false;
  440. }
  441. /**
  442. * 生成UPC-A条形码
  443. * @static
  444. * @param string $type 图像格式
  445. * @param string $type 图像格式
  446. * @param string $lw 单元宽度
  447. * @param string $hi 条码高度
  448. * @return string
  449. */
  450. static function UPCA($code, $type='png', $lw=2, $hi=100) {
  451. static $Lencode = array('0001101', '0011001', '0010011', '0111101', '0100011',
  452. '0110001', '0101111', '0111011', '0110111', '0001011');
  453. static $Rencode = array('1110010', '1100110', '1101100', '1000010', '1011100',
  454. '1001110', '1010000', '1000100', '1001000', '1110100');
  455. $ends = '101';
  456. $center = '01010';
  457. /* UPC-A Must be 11 digits, we compute the checksum. */
  458. if (strlen($code) != 11) {
  459. die("UPC-A Must be 11 digits.");
  460. }
  461. /* Compute the EAN-13 Checksum digit */
  462. $ncode = '0' . $code;
  463. $even = 0;
  464. $odd = 0;
  465. for ($x = 0; $x < 12; $x++) {
  466. if ($x % 2) {
  467. $odd += $ncode[$x];
  468. } else {
  469. $even += $ncode[$x];
  470. }
  471. }
  472. $code.= ( 10 - (($odd * 3 + $even) % 10)) % 10;
  473. /* Create the bar encoding using a binary string */
  474. $bars = $ends;
  475. $bars.=$Lencode[$code[0]];
  476. for ($x = 1; $x < 6; $x++) {
  477. $bars.=$Lencode[$code[$x]];
  478. }
  479. $bars.=$center;
  480. for ($x = 6; $x < 12; $x++) {
  481. $bars.=$Rencode[$code[$x]];
  482. }
  483. $bars.=$ends;
  484. /* Generate the Barcode Image */
  485. if ($type != 'gif' && function_exists('imagecreatetruecolor')) {
  486. $im = imagecreatetruecolor($lw * 95 + 30, $hi + 30);
  487. } else {
  488. $im = imagecreate($lw * 95 + 30, $hi + 30);
  489. }
  490. $fg = ImageColorAllocate($im, 0, 0, 0);
  491. $bg = ImageColorAllocate($im, 255, 255, 255);
  492. ImageFilledRectangle($im, 0, 0, $lw * 95 + 30, $hi + 30, $bg);
  493. $shift = 10;
  494. for ($x = 0; $x < strlen($bars); $x++) {
  495. if (($x < 10) || ($x >= 45 && $x < 50) || ($x >= 85)) {
  496. $sh = 10;
  497. } else {
  498. $sh = 0;
  499. }
  500. if ($bars[$x] == '1') {
  501. $color = $fg;
  502. } else {
  503. $color = $bg;
  504. }
  505. ImageFilledRectangle($im, ($x * $lw) + 15, 5, ($x + 1) * $lw + 14, $hi + 5 + $sh, $color);
  506. }
  507. /* Add the Human Readable Label */
  508. ImageString($im, 4, 5, $hi - 5, $code[0], $fg);
  509. for ($x = 0; $x < 5; $x++) {
  510. ImageString($im, 5, $lw * (13 + $x * 6) + 15, $hi + 5, $code[$x + 1], $fg);
  511. ImageString($im, 5, $lw * (53 + $x * 6) + 15, $hi + 5, $code[$x + 6], $fg);
  512. }
  513. ImageString($im, 4, $lw * 95 + 17, $hi - 5, $code[11], $fg);
  514. /* Output the Header and Content. */
  515. Image::output($im, $type);
  516. }
  517. static function output($im, $type='png', $filename='') {
  518. header("Content-type: image/" . $type);
  519. $ImageFun = 'image' . $type;
  520. if (empty($filename)) {
  521. $ImageFun($im);
  522. } else {
  523. $ImageFun($im, $filename);
  524. }
  525. imagedestroy($im);
  526. }
  527. }