PageRenderTime 53ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/addons/library/Image.class.php

https://github.com/cxc222/weibo
PHP | 858 lines | 567 code | 66 blank | 225 comment | 114 complexity | fc7ef1ae7b7cf1e9f6472e1021a3910d MD5 | raw file
Possible License(s): LGPL-2.1
  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
  24. {//类定义开始
  25. /**
  26. +----------------------------------------------------------
  27. * 取得图像信息
  28. *
  29. +----------------------------------------------------------
  30. * @static
  31. * @access public
  32. +----------------------------------------------------------
  33. * @param string $image 图像文件名
  34. +----------------------------------------------------------
  35. * @return mixed
  36. +----------------------------------------------------------
  37. */
  38. static function getImageInfo($img) {
  39. $imageInfo = getimagesize($img);
  40. if( $imageInfo!== false) {
  41. $imageType = strtolower(substr(image_type_to_extension($imageInfo[2]),1));
  42. $imageSize = filesize($img);
  43. $info = array(
  44. "width"=>$imageInfo[0],
  45. "height"=>$imageInfo[1],
  46. "type"=>$imageType,
  47. "size"=>$imageSize,
  48. "mime"=>$imageInfo['mime']
  49. );
  50. return $info;
  51. }else {
  52. return false;
  53. }
  54. }
  55. /**
  56. +----------------------------------------------------------
  57. * 显示服务器图像文件
  58. * 支持URL方式
  59. +----------------------------------------------------------
  60. * @static
  61. * @access public
  62. +----------------------------------------------------------
  63. * @param string $imgFile 图像文件名
  64. * @param string $text 文字字符串
  65. * @param string $width 图像宽度
  66. * @param string $height 图像高度
  67. +----------------------------------------------------------
  68. * @return void
  69. +----------------------------------------------------------
  70. */
  71. static function showImg($imgFile,$text='',$width=80,$height=30) {
  72. //获取图像文件信息
  73. $info = Image::getImageInfo($imgFile);
  74. if($info !== false) {
  75. $createFun = str_replace('/','createfrom',$info['mime']);
  76. $im = $createFun($imgFile);
  77. if($im) {
  78. $ImageFun= str_replace('/','',$info['mime']);
  79. if(!empty($text)) {
  80. $tc = imagecolorallocate($im, 0, 0, 0);
  81. imagestring($im, 3, 5, 5, $text, $tc);
  82. }
  83. if($info['type']=='png' || $info['type']=='gif') {
  84. imagealphablending($im, false);//取消默认的混色模式
  85. imagesavealpha($im,true);//设定保存完整的 alpha 通道信息
  86. }
  87. header("Content-type: ".$info['mime']);
  88. $ImageFun($im);
  89. imagedestroy($im);
  90. return ;
  91. }
  92. }
  93. //获取或者创建图像文件失败则生成空白PNG图片
  94. $im = imagecreatetruecolor($width, $height);
  95. $bgc = imagecolorallocate($im, 255, 255, 255);
  96. $tc = imagecolorallocate($im, 0, 0, 0);
  97. imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
  98. imagestring($im, 4, 5, 5, "NO PIC", $tc);
  99. Image::output($im);
  100. return ;
  101. }
  102. /**
  103. * 切割图片
  104. +----------------------------------------------------------
  105. * @static
  106. * @access public
  107. +----------------------------------------------------------
  108. * @param string $image 原图
  109. * @param string $cutfile 切割后的图片
  110. * @param int $cutWidth
  111. * @param int $cutHeight
  112. +----------------------------------------------------------
  113. * @return void
  114. */
  115. static function cut($image,$filename,$maxWidth='',$maxHeight=''){
  116. // 获取原图信息
  117. $info = Image::getImageInfo($image);
  118. //dump($image);
  119. if($info !== false) {
  120. $srcWidth = $info['width'];
  121. $srcHeight = $info['height'];
  122. $pathinfo = pathinfo($image);
  123. $type = $pathinfo['extension'];
  124. $type = empty($type)?$info['type']:$type;
  125. $type = strtolower($type);
  126. $interlace = $interlace? 1:0;
  127. unset($info);
  128. // 载入原图
  129. $createFun = 'ImageCreateFrom'.($type=='jpg'?'jpeg':$type);
  130. $srcImg = $createFun($image);
  131. //创建缩略图
  132. if($type!='gif' && function_exists('imagecreatetruecolor'))
  133. $thumbImg = imagecreatetruecolor($maxWidth, $maxHeight);
  134. else
  135. $thumbImg = imagecreate($maxWidth, $maxHeight);
  136. // 新建PNG缩略图通道透明处理
  137. if('png'==$type) {
  138. imagealphablending($thumbImg, false);//取消默认的混色模式
  139. imagesavealpha($thumbImg,true);//设定保存完整的 alpha 通道信息
  140. }elseif('gif'==$type) {
  141. // 新建GIF缩略图预处理,保证透明效果不失效
  142. $background_color = imagecolorallocate($thumbImg, 0,255,0); // 指派一个绿色
  143. imagecolortransparent($thumbImg,$background_color); // 设置为透明色,若注释掉该行则输出绿色的图
  144. }
  145. // 计算缩放比例
  146. if(($maxWidth/$maxHeight)>=($srcWidth/$srcHeight)){
  147. //宽不变,截高,从中间截取 y=
  148. $width = $srcWidth;
  149. $height = $srcWidth*($maxHeight/$maxWidth);
  150. $x = 0;
  151. $y = ($srcHeight-$height)*0.5;
  152. }else{
  153. //高不变,截宽,从中间截取,x=
  154. $width = $srcHeight*($maxWidth/$maxHeight);
  155. $height = $srcHeight;
  156. $x = ($srcWidth-$width)*0.5;
  157. $y = 0;
  158. }
  159. // 复制图片
  160. if(function_exists("ImageCopyResampled")){
  161. ImageCopyResampled($thumbImg, $srcImg, 0, 0, $x, $y, $maxWidth, $maxHeight, $width,$height);
  162. }else{
  163. ImageCopyResized($thumbImg, $srcImg, 0, 0, $x, $y, $maxWidth, $maxHeight, $width,$height);
  164. }
  165. ImageDestroy($srcImg);
  166. // 对jpeg图形设置隔行扫描
  167. if('jpg'==$type || 'jpeg'==$type) imageinterlace($thumbImg,$interlace);
  168. // 生成图片
  169. //$imageFun = 'image'.($type=='jpg'?'jpeg':$type);
  170. $imageFun = 'imagepng';
  171. $filename = empty($filename)? substr($image,0,strrpos($image, '.')).$suffix.'.'.$type : $filename;
  172. $imageFun($thumbImg,$filename);
  173. ImageDestroy($thumbImg);
  174. return $filename;
  175. }
  176. return false;
  177. }
  178. /**
  179. +----------------------------------------------------------
  180. * 生成缩略图
  181. +----------------------------------------------------------
  182. * @static
  183. * @access public
  184. +----------------------------------------------------------
  185. * @param string $image 原图
  186. * @param string $type 图像格式
  187. * @param string $thumbname 缩略图文件名
  188. * @param string $maxWidth 宽度
  189. * @param string $maxHeight 高度
  190. * @param string $position 缩略图保存目录
  191. * @param boolean $interlace 启用隔行扫描
  192. +----------------------------------------------------------
  193. * @return void
  194. +----------------------------------------------------------
  195. */
  196. static function thumb($image,$thumbname,$type='',$maxWidth=200,$maxHeight='auto',$interlace=true)
  197. {
  198. // 获取原图信息
  199. $info = Image::getImageInfo($image);
  200. if($info !== false) {
  201. $srcWidth = $info['width'];
  202. $srcHeight = $info['height'];
  203. $type = empty($type)?$info['type']:$type;
  204. $type = strtolower($type);
  205. $interlace = $interlace? 1:0;
  206. unset($info);
  207. if( $maxHeight=='auto' ){
  208. $scale = $maxWidth/$srcWidth;
  209. }else{
  210. $scale = min($maxWidth/$srcWidth, $maxHeight/$srcHeight); // 计算缩放比例
  211. }
  212. if($scale>=1) {
  213. // 超过原图大小不再缩略
  214. $width = $srcWidth;
  215. $height = $srcHeight;
  216. }else{
  217. // 缩略图尺寸
  218. $width = (int)($srcWidth*$scale);
  219. $height = (int)($srcHeight*$scale);
  220. }
  221. // 载入原图
  222. $createFun = 'ImageCreateFrom'.($type=='jpg'?'jpeg':$type);
  223. $srcImg = $createFun($image);
  224. //创建缩略图
  225. if($type!='gif' && function_exists('imagecreatetruecolor'))
  226. $thumbImg = imagecreatetruecolor($width, $height);
  227. else
  228. $thumbImg = imagecreate($width, $height);
  229. // 新建PNG缩略图通道透明处理
  230. if('png'==$type) {
  231. imagealphablending($thumbImg, false);//取消默认的混色模式
  232. imagesavealpha($thumbImg,true);//设定保存完整的 alpha 通道信息
  233. }elseif('gif'==$type) {
  234. // 新建GIF缩略图预处理,保证透明效果不失效
  235. $background_color = imagecolorallocate($thumbImg, 0,255,0); // 指派一个绿色
  236. imagecolortransparent($thumbImg,$background_color); // 设置为透明色,若注释掉该行则输出绿色的图
  237. }
  238. // 复制图片
  239. if(function_exists("ImageCopyResampled"))
  240. imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth,$srcHeight);
  241. else
  242. imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth,$srcHeight);
  243. /*if('gif'==$type || 'png'==$type) {
  244. //imagealphablending($thumbImg, false);//取消默认的混色模式
  245. //imagesavealpha($thumbImg,true);//设定保存完整的 alpha 通道信息
  246. $background_color = imagecolorallocate($thumbImg, 0,255,0); // 指派一个绿色
  247. imagecolortransparent($thumbImg,$background_color); // 设置为透明色,若注释掉该行则输出绿色的图
  248. }*/
  249. // 对jpeg图形设置隔行扫描
  250. if('jpg'==$type || 'jpeg'==$type) imageinterlace($thumbImg,$interlace);
  251. //$gray=ImageColorAllocate($thumbImg,255,0,0);
  252. //ImageString($thumbImg,2,5,5,"ThinkPHP",$gray);
  253. // 生成图片
  254. $imageFun = 'image'.($type=='jpg'?'jpeg':$type);
  255. $imageFun($thumbImg,$thumbname);
  256. imagedestroy($thumbImg);
  257. imagedestroy($srcImg);
  258. return $thumbname;
  259. }
  260. return false;
  261. }
  262. /**
  263. +----------------------------------------------------------
  264. * 根据给定的字符串生成图像
  265. +----------------------------------------------------------
  266. * @static
  267. * @access public
  268. +----------------------------------------------------------
  269. * @param string $string 字符串
  270. * @param string $size 图像大小 width,height 或者 array(width,height)
  271. * @param string $font 字体信息 fontface,fontsize 或者 array(fontface,fontsize)
  272. * @param string $type 图像格式 默认PNG
  273. * @param integer $disturb 是否干扰 1 点干扰 2 线干扰 3 复合干扰 0 无干扰
  274. * @param bool $border 是否加边框 array(color)
  275. +----------------------------------------------------------
  276. * @return string
  277. +----------------------------------------------------------
  278. */
  279. static function buildString($string,$rgb=array(),$filename='',$type='png',$disturb=1,$border=true) {
  280. if(is_string($size)) $size = explode(',',$size);
  281. $width = $size[0];
  282. $height = $size[1];
  283. if(is_string($font)) $font = explode(',',$font);
  284. $fontface = $font[0];
  285. $fontsize = $font[1];
  286. $length = strlen($string);
  287. $width = ($length*9+10)>$width?$length*9+10:$width;
  288. $height = 22;
  289. if ( $type!='gif' && function_exists('imagecreatetruecolor')) {
  290. $im = @imagecreatetruecolor($width,$height);
  291. }else {
  292. $im = @imagecreate($width,$height);
  293. }
  294. if(empty($rgb)) {
  295. $color = imagecolorallocate($im, 102, 104, 104);
  296. }else{
  297. $color = imagecolorallocate($im, $rgb[0], $rgb[1], $rgb[2]);
  298. }
  299. $backColor = imagecolorallocate($im, 255,255,255); //背景色(随机)
  300. $borderColor = imagecolorallocate($im, 100, 100, 100); //边框色
  301. $pointColor = imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); //点颜色
  302. @imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
  303. @imagerectangle($im, 0, 0, $width-1, $height-1, $borderColor);
  304. @imagestring($im, 5, 5, 3, $string, $color);
  305. if(!empty($disturb)) {
  306. // 添加干扰
  307. if($disturb=1 || $disturb=3) {
  308. for($i=0;$i<25;$i++){
  309. imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$pointColor);
  310. }
  311. }elseif($disturb=2 || $disturb=3){
  312. for($i=0;$i<10;$i++){
  313. imagearc($im,mt_rand(-10,$width),mt_rand(-10,$height),mt_rand(30,300),mt_rand(20,200),55,44,$pointColor);
  314. }
  315. }
  316. }
  317. Image::output($im,$type,$filename);
  318. }
  319. /**
  320. +----------------------------------------------------------
  321. * 生成图像验证码
  322. +----------------------------------------------------------
  323. * @static
  324. * @access public
  325. +----------------------------------------------------------
  326. * @param string $length 位数
  327. * @param string $mode 类型
  328. * @param string $type 图像格式
  329. * @param string $width 宽度
  330. * @param string $height 高度
  331. +----------------------------------------------------------
  332. * @return string
  333. +----------------------------------------------------------
  334. */
  335. static function buildImageVerify($length=4,$mode=1,$type='png',$width=48,$height=22,$verifyName='verify')
  336. {
  337. tsload(ADDON_PATH.'/library/String.class.php');
  338. $randval = String::rand_string($length,$mode);
  339. //转换成大写字母.
  340. $_SESSION[$verifyName]= md5(strtoupper($randval));
  341. $width = ($length*10+10)>$width?$length*10+10:$width;
  342. if ( $type!='gif' && function_exists('imagecreatetruecolor')) {
  343. $im = @imagecreatetruecolor($width,$height);
  344. }else {
  345. $im = @imagecreate($width,$height);
  346. }
  347. $r = Array(225,255,255,223);
  348. $g = Array(225,236,237,255);
  349. $b = Array(225,236,166,125);
  350. $key = mt_rand(0,3);
  351. $backColor = imagecolorallocate($im, $r[$key],$g[$key],$b[$key]); //背景色(随机)
  352. $borderColor = imagecolorallocate($im, 100, 100, 100); //边框色
  353. $pointColor = imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); //点颜色
  354. @imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
  355. @imagerectangle($im, 0, 0, $width-1, $height-1, $borderColor);
  356. $stringColor = imagecolorallocate($im,mt_rand(0,200),mt_rand(0,120),mt_rand(0,120));
  357. // 干扰
  358. for($i=0;$i<10;$i++){
  359. $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  360. imagearc($im,mt_rand(-10,$width),mt_rand(-10,$height),mt_rand(30,300),mt_rand(20,200),55,44,$fontcolor);
  361. }
  362. for($i=0;$i<25;$i++){
  363. $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  364. imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$pointColor);
  365. }
  366. for($i=0;$i<$length;$i++) {
  367. imagestring($im,5,$i*10+5,mt_rand(1,8),$randval{$i}, $stringColor);
  368. }
  369. // @imagestring($im, 5, 5, 3, $randval, $stringColor);
  370. Image::output($im,$type);
  371. }
  372. // 中文验证码
  373. static function GBVerify($length=4,$type='png',$width=180,$height=50,$fontface='simhei.ttf',$verifyName='verify') {
  374. tsload(ADDON_PATH.'/library/String.class.php');
  375. $code = String::rand_string($length,4);
  376. $width = ($length*45)>$width?$length*45:$width;
  377. $_SESSION[$verifyName]= md5($code);
  378. $im=imagecreatetruecolor($width,$height);
  379. $borderColor = imagecolorallocate($im, 100, 100, 100); //边框色
  380. $bkcolor=imagecolorallocate($im,250,250,250);
  381. imagefill($im,0,0,$bkcolor);
  382. @imagerectangle($im, 0, 0, $width-1, $height-1, $borderColor);
  383. // 干扰
  384. for($i=0;$i<15;$i++){
  385. $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  386. imagearc($im,mt_rand(-10,$width),mt_rand(-10,$height),mt_rand(30,300),mt_rand(20,200),55,44,$fontcolor);
  387. }
  388. for($i=0;$i<255;$i++){
  389. $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  390. imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$fontcolor);
  391. }
  392. if(!is_file($fontface)) {
  393. $fontface = dirname(__FILE__)."/".$fontface;
  394. }
  395. for($i=0;$i<$length;$i++){
  396. $fontcolor=imagecolorallocate($im,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120)); //这样保证随机出来的颜色较深。
  397. $codex= String::msubstr($code,$i,1);
  398. imagettftext($im,mt_rand(16,20),mt_rand(-60,60),40*$i+20,mt_rand(30,35),$fontcolor,$fontface,$codex);
  399. }
  400. Image::output($im,$type);
  401. }
  402. /**
  403. +----------------------------------------------------------
  404. * 把图像转换成字符显示
  405. +----------------------------------------------------------
  406. * @static
  407. * @access public
  408. +----------------------------------------------------------
  409. * @param string $image 要显示的图像
  410. * @param string $type 图像类型,默认自动获取
  411. +----------------------------------------------------------
  412. * @return string
  413. +----------------------------------------------------------
  414. */
  415. static function showASCIIImg($image,$string='',$type='')
  416. {
  417. $info = Image::getImageInfo($image);
  418. if($info !== false) {
  419. $type = empty($type)?$info['type']:$type;
  420. unset($info);
  421. // 载入原图
  422. $createFun = 'ImageCreateFrom'.($type=='jpg'?'jpeg':$type);
  423. $im = $createFun($image);
  424. $dx = imagesx($im);
  425. $dy = imagesy($im);
  426. $i = 0;
  427. $out = '<span style="padding:0px;margin:0;line-height:100%;font-size:1px;">';
  428. set_time_limit(0);
  429. for($y = 0; $y < $dy; $y++) {
  430. for($x=0; $x < $dx; $x++) {
  431. $col = imagecolorat($im, $x, $y);
  432. $rgb = imagecolorsforindex($im,$col);
  433. $str = empty($string)?'*':$string[$i++];
  434. $out .= sprintf('<span style="margin:0px;color:#%02x%02x%02x">'.$str.'</span>',$rgb['red'],$rgb['green'],$rgb['blue']);
  435. }
  436. $out .= "<br>\n";
  437. }
  438. $out .= '</span>';
  439. imagedestroy($im);
  440. return $out;
  441. }
  442. return false;
  443. }
  444. /**
  445. +----------------------------------------------------------
  446. * 生成高级图像验证码
  447. +----------------------------------------------------------
  448. * @static
  449. * @access public
  450. +----------------------------------------------------------
  451. * @param string $type 图像格式
  452. * @param string $width 宽度
  453. * @param string $height 高度
  454. +----------------------------------------------------------
  455. * @return string
  456. +----------------------------------------------------------
  457. */
  458. static function showAdvVerify($type='png',$width=180,$height=40,$verifyName='verifyCode')
  459. {
  460. $rand = range('a','z');
  461. shuffle($rand);
  462. $verifyCode = array_slice($rand,0,10);
  463. $letter = implode(" ",$verifyCode);
  464. $_SESSION[$verifyName] = $verifyCode;
  465. $im = imagecreate($width,$height);
  466. $r = array(225,255,255,223);
  467. $g = array(225,236,237,255);
  468. $b = array(225,236,166,125);
  469. $key = mt_rand(0,3);
  470. $backColor = imagecolorallocate($im, $r[$key],$g[$key],$b[$key]);
  471. $borderColor = imagecolorallocate($im, 100, 100, 100); //边框色
  472. imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
  473. imagerectangle($im, 0, 0, $width-1, $height-1, $borderColor);
  474. $numberColor = imagecolorallocate($im, 255,rand(0,100), rand(0,100));
  475. $stringColor = imagecolorallocate($im, rand(0,100), rand(0,100), 255);
  476. // 添加干扰
  477. /*
  478. for($i=0;$i<10;$i++){
  479. $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  480. imagearc($im,mt_rand(-10,$width),mt_rand(-10,$height),mt_rand(30,300),mt_rand(20,200),55,44,$fontcolor);
  481. }
  482. for($i=0;$i<255;$i++){
  483. $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  484. imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$fontcolor);
  485. }*/
  486. imagestring($im, 5, 5, 1, "0 1 2 3 4 5 6 7 8 9", $numberColor);
  487. imagestring($im, 5, 5, 20, $letter, $stringColor);
  488. Image::output($im,$type);
  489. }
  490. /**
  491. +----------------------------------------------------------
  492. * 生成UPC-A条形码
  493. +----------------------------------------------------------
  494. * @static
  495. +----------------------------------------------------------
  496. * @param string $type 图像格式
  497. * @param string $type 图像格式
  498. * @param string $lw 单元宽度
  499. * @param string $hi 条码高度
  500. +----------------------------------------------------------
  501. * @return string
  502. +----------------------------------------------------------
  503. */
  504. static function UPCA($code,$type='png',$lw=2,$hi=100) {
  505. static $Lencode = array('0001101','0011001','0010011','0111101','0100011',
  506. '0110001','0101111','0111011','0110111','0001011');
  507. static $Rencode = array('1110010','1100110','1101100','1000010','1011100',
  508. '1001110','1010000','1000100','1001000','1110100');
  509. $ends = '101';
  510. $center = '01010';
  511. /* UPC-A Must be 11 digits, we compute the checksum. */
  512. if ( strlen($code) != 11 ) { die("UPC-A Must be 11 digits."); }
  513. /* Compute the EAN-13 Checksum digit */
  514. $ncode = '0'.$code;
  515. $even = 0; $odd = 0;
  516. for ($x=0;$x<12;$x++) {
  517. if ($x % 2) { $odd += $ncode[$x]; } else { $even += $ncode[$x]; }
  518. }
  519. $code.=(10 - (($odd * 3 + $even) % 10)) % 10;
  520. /* Create the bar encoding using a binary string */
  521. $bars=$ends;
  522. $bars.=$Lencode[$code[0]];
  523. for($x=1;$x<6;$x++) {
  524. $bars.=$Lencode[$code[$x]];
  525. }
  526. $bars.=$center;
  527. for($x=6;$x<12;$x++) {
  528. $bars.=$Rencode[$code[$x]];
  529. }
  530. $bars.=$ends;
  531. /* Generate the Barcode Image */
  532. if ( $type!='gif' && function_exists('imagecreatetruecolor')) {
  533. $im = imagecreatetruecolor($lw*95+30,$hi+30);
  534. }else {
  535. $im = imagecreate($lw*95+30,$hi+30);
  536. }
  537. $fg = ImageColorAllocate($im, 0, 0, 0);
  538. $bg = ImageColorAllocate($im, 255, 255, 255);
  539. ImageFilledRectangle($im, 0, 0, $lw*95+30, $hi+30, $bg);
  540. $shift=10;
  541. for ($x=0;$x<strlen($bars);$x++) {
  542. if (($x<10) || ($x>=45 && $x<50) || ($x >=85)) { $sh=10; } else { $sh=0; }
  543. if ($bars[$x] == '1') { $color = $fg; } else { $color = $bg; }
  544. ImageFilledRectangle($im, ($x*$lw)+15,5,($x+1)*$lw+14,$hi+5+$sh,$color);
  545. }
  546. /* Add the Human Readable Label */
  547. ImageString($im,4,5,$hi-5,$code[0],$fg);
  548. for ($x=0;$x<5;$x++) {
  549. ImageString($im,5,$lw*(13+$x*6)+15,$hi+5,$code[$x+1],$fg);
  550. ImageString($im,5,$lw*(53+$x*6)+15,$hi+5,$code[$x+6],$fg);
  551. }
  552. ImageString($im,4,$lw*95+17,$hi-5,$code[11],$fg);
  553. /* Output the Header and Content. */
  554. Image::output($im,$type);
  555. }
  556. static function output($im,$type='png',$filename='')
  557. {
  558. header("Content-type: image/".$type);
  559. $ImageFun='image'.$type;
  560. if(empty($filename)) {
  561. $ImageFun($im);
  562. }else{
  563. $ImageFun($im,$filename);
  564. }
  565. imagedestroy($im);
  566. }
  567. }//类定义结束
  568. if(!function_exists('ImageCreateFrombmp')){
  569. function ImageCreateFrombmp($filename)
  570. {
  571. $buf=@file_get_contents($filename);
  572. if(strlen($buf)<54) return false;
  573. $file_header=unpack("sbfType/LbfSize/sbfReserved1/sbfReserved2/LbfOffBits",substr($buf,0,14));
  574. if($file_header["bfType"]!=19778) return false;
  575. $info_header=unpack("LbiSize/lbiWidth/lbiHeight/sbiPlanes/sbiBitCountLbiCompression/LbiSizeImage/lbiXPelsPerMeter/lbiYPelsPerMeter/LbiClrUsed/LbiClrImportant",substr($buf,14,40));
  576. if($info_header["biBitCountLbiCompression"]==2) return false;
  577. $line_len=round($info_header["biWidth"]*$info_header["biBitCountLbiCompression"]/8);
  578. $x=$line_len%4;
  579. if($x>0) $line_len+=4-$x;
  580. $img=imagecreatetruecolor($info_header["biWidth"],$info_header["biHeight"]);
  581. switch($info_header["biBitCountLbiCompression"]){
  582. case 4:
  583. $colorset=unpack("L*",substr($buf,54,64));
  584. for($y=0;$y<$info_header["biHeight"];$y++){
  585. $colors=array();
  586. $y_pos=$y*$line_len+$file_header["bfOffBits"];
  587. for($x=0;$x<$info_header["biWidth"];$x++){
  588. if($x%2)
  589. $colors[]=$colorset[(ord($buf[$y_pos+($x+1)/2])&0xf)+1];
  590. else
  591. $colors[]=$colorset[((ord($buf[$y_pos+$x/2+1])>>4)&0xf)+1];
  592. }
  593. imagesetstyle($img,$colors);
  594. imageline($img,0,$info_header["biHeight"]-$y-1,$info_header["biWidth"],$info_header["biHeight"]-$y-1,IMG_COLOR_STYLED);
  595. }
  596. break;
  597. case 8:
  598. $colorset=unpack("L*",substr($buf,54,1024));
  599. for($y=0;$y<$info_header["biHeight"];$y++){
  600. $colors=array();
  601. $y_pos=$y*$line_len+$file_header["bfOffBits"];
  602. for($x=0;$x<$info_header["biWidth"];$x++){
  603. $colors[]=$colorset[ord($buf[$y_pos+$x])+1];
  604. }
  605. imagesetstyle($img,$colors);
  606. imageline($img,0,$info_header["biHeight"]-$y-1,$info_header["biWidth"],$info_header["biHeight"]-$y-1,IMG_COLOR_STYLED);
  607. }
  608. break;
  609. case 16:
  610. for($y=0;$y<$info_header["biHeight"];$y++){
  611. $colors=array();
  612. $y_pos=$y*$line_len+$file_header["bfOffBits"];
  613. for($x=0;$x<$info_header["biWidth"];$x++){
  614. $i=$x*2;
  615. $color=ord($buf[$y_pos+$i])|(ord($buf[$y_pos+$i+1])<<8);
  616. $colors[]=imagecolorallocate($img,(($color>>10)&0x1f)*0xff/0x1f,(($color>>5)&0x1f)*0xff/0x1f,($color&0x1f)*0xff/0x1f);
  617. }
  618. imagesetstyle($img,$colors);
  619. imageline($img,0,$info_header["biHeight"]-$y-1,$info_header["biWidth"],$info_header["biHeight"]-$y-1,IMG_COLOR_STYLED);
  620. }
  621. break;
  622. case 24:
  623. for($y=0;$y<$info_header["biHeight"];$y++){
  624. $colors=array();
  625. $y_pos=$y*$line_len+$file_header["bfOffBits"];
  626. for($x=0;$x<$info_header["biWidth"];$x++){
  627. $i=$x*3;
  628. $colors[]=imagecolorallocate($img,ord($buf[$y_pos+$i+2]),ord($buf[$y_pos+$i+1]),ord($buf[$y_pos+$i]));
  629. }
  630. imagesetstyle($img,$colors);
  631. imageline($img,0,$info_header["biHeight"]-$y-1,$info_header["biWidth"],$info_header["biHeight"]-$y-1,IMG_COLOR_STYLED);
  632. }
  633. break;
  634. default:
  635. return false;
  636. break;
  637. }
  638. return $img;
  639. }
  640. function imagebmp(&$im, $filename = '', $bit = 8, $compression = 0)
  641. {
  642. if (!in_array($bit, array(1, 4, 8, 16, 24, 32)))
  643. {
  644. $bit = 8;
  645. }
  646. else if ($bit == 32) // todo:32 bit
  647. {
  648. $bit = 24;
  649. }
  650. $bits = pow(2, $bit);
  651. // 调整调色板
  652. imagetruecolortopalette($im, true, $bits);
  653. $width = imagesx($im);
  654. $height = imagesy($im);
  655. $colors_num = imagecolorstotal($im);
  656. if ($bit <= 8)
  657. {
  658. // 颜色索引
  659. $rgb_quad = '';
  660. for ($i = 0; $i < $colors_num; $i ++)
  661. {
  662. $colors = imagecolorsforindex($im, $i);
  663. $rgb_quad .= chr($colors['blue']) . chr($colors['green']) . chr($colors['red']) . "\0";
  664. }
  665. // 位图数据
  666. $bmp_data = '';
  667. // 非压缩
  668. if ($compression == 0 || $bit < 8)
  669. {
  670. if (!in_array($bit, array(1, 4, 8)))
  671. {
  672. $bit = 8;
  673. }
  674. $compression = 0;
  675. // 每行字节数必须为4的倍数,补齐。
  676. $extra = '';
  677. $padding = 4 - ceil($width / (8 / $bit)) % 4;
  678. if ($padding % 4 != 0)
  679. {
  680. $extra = str_repeat("\0", $padding);
  681. }
  682. for ($j = $height - 1; $j >= 0; $j --)
  683. {
  684. $i = 0;
  685. while ($i < $width)
  686. {
  687. $bin = 0;
  688. $limit = $width - $i < 8 / $bit ? (8 / $bit - $width + $i) * $bit : 0;
  689. for ($k = 8 - $bit; $k >= $limit; $k -= $bit)
  690. {
  691. $index = imagecolorat($im, $i, $j);
  692. $bin |= $index << $k;
  693. $i ++;
  694. }
  695. $bmp_data .= chr($bin);
  696. }
  697. $bmp_data .= $extra;
  698. }
  699. }
  700. // RLE8 压缩
  701. else if ($compression == 1 && $bit == 8)
  702. {
  703. for ($j = $height - 1; $j >= 0; $j --)
  704. {
  705. $last_index = "\0";
  706. $same_num = 0;
  707. for ($i = 0; $i <= $width; $i ++)
  708. {
  709. $index = imagecolorat($im, $i, $j);
  710. if ($index !== $last_index || $same_num > 255)
  711. {
  712. if ($same_num != 0)
  713. {
  714. $bmp_data .= chr($same_num) . chr($last_index);
  715. }
  716. $last_index = $index;
  717. $same_num = 1;
  718. }
  719. else
  720. {
  721. $same_num ++;
  722. }
  723. }
  724. $bmp_data .= "\0\0";
  725. }
  726. $bmp_data .= "\0\1";
  727. }
  728. $size_quad = strlen($rgb_quad);
  729. $size_data = strlen($bmp_data);
  730. }
  731. else
  732. {
  733. // 每行字节数必须为4的倍数,补齐。
  734. $extra = '';
  735. $padding = 4 - ($width * ($bit / 8)) % 4;
  736. if ($padding % 4 != 0)
  737. {
  738. $extra = str_repeat("\0", $padding);
  739. }
  740. // 位图数据
  741. $bmp_data = '';
  742. for ($j = $height - 1; $j >= 0; $j --)
  743. {
  744. for ($i = 0; $i < $width; $i ++)
  745. {
  746. $index = imagecolorat($im, $i, $j);
  747. $colors = imagecolorsforindex($im, $index);
  748. if ($bit == 16)
  749. {
  750. $bin = 0 << $bit;
  751. $bin |= ($colors['red'] >> 3) << 10;
  752. $bin |= ($colors['green'] >> 3) << 5;
  753. $bin |= $colors['blue'] >> 3;
  754. $bmp_data .= pack("v", $bin);
  755. }
  756. else
  757. {
  758. $bmp_data .= pack("c*", $colors['blue'], $colors['green'], $colors['red']);
  759. }
  760. // todo: 32bit;
  761. }
  762. $bmp_data .= $extra;
  763. }
  764. $size_quad = 0;
  765. $size_data = strlen($bmp_data);
  766. $colors_num = 0;
  767. }
  768. // 位图文件头
  769. $file_header = "BM" . pack("V3", 54 + $size_quad + $size_data, 0, 54 + $size_quad);
  770. // 位图信息头
  771. $info_header = pack("V3v2V*", 0x28, $width, $height, 1, $bit, $compression, $size_data, 0, 0, $colors_num, 0);
  772. // 写入文件
  773. if ($filename != '')
  774. {
  775. $fp = fopen($filename, "wb");
  776. fwrite($fp, $file_header);
  777. fwrite($fp, $info_header);
  778. fwrite($fp, $rgb_quad);
  779. fwrite($fp, $bmp_data);
  780. fclose($fp);
  781. return 1;
  782. }
  783. // 浏览器输出
  784. header("Content-Type: image/bmp");
  785. echo $file_header . $info_header;
  786. echo $rgb_quad;
  787. echo $bmp_data;
  788. return 1;
  789. }
  790. }
  791. ?>