QrCode.php 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\utils;
  8. class QrCode{
  9. public static function image($text, $pixel = 200, $icon = false, $distinguish = 'L', $type = 'PNG', $margin = 0, $color = false, $spec=1, $stream = false, $OutputPath = null){
  10. //string 文字
  11. $string = new qrcode_string();
  12. $string->initial($text, $distinguish, intval($spec));
  13. $datacode = $string->getByteStream();
  14. $version = $string->version;
  15. //pattern 模式
  16. $pattern = new qrcode_pattern();
  17. $width = $pattern->getWidth($version);
  18. $frame = $pattern->newFrame($version);
  19. //exchange 转换
  20. $exchange = new qrcode_exchange();
  21. $masked = $exchange->run($string, $width, $frame, $datacode);
  22. $tab = $exchange->binarize($masked);
  23. if(true === $stream) return $tab;
  24. //image 图像
  25. $img = new qrcode_image();
  26. $im = $img->ImgColor($tab, $pixel, $icon, $margin, $color);
  27. if(!is_string($OutputPath)){
  28. $OutputPath = null;
  29. }
  30. if(strtoupper($type) == 'PNG'){
  31. header('Content-type: image/PNG');
  32. ImagePNG($im, $OutputPath);
  33. }else{
  34. header('Content-type: image/JPEG');
  35. ImageJPEG($im, $OutputPath);//合成输出
  36. }
  37. ImageDestroy($im); //结束图形
  38. return $im;
  39. }
  40. }//END class qrcode
  41. /*
  42. * QR Code Image 图像处理
  43. * Author: weibo.com/yakeing
  44. * ImageCreate 256色
  45. * ImageCreateTrueColor 真彩色
  46. * ImageCopyResized 粗糙速度快
  47. * ImageCopyResampled 平滑速度慢
  48. */
  49. class qrcode_image{
  50. //图像与颜色
  51. function ImgColor($frame, $pixel, $iconurl, $margin, $color){
  52. //$pixel = $pixel/count($frame);
  53. if(is_array($color) && 2 == count($color)){ //RGB
  54. $RGB = array();
  55. foreach($color as $value){
  56. $matches = array();
  57. if(preg_match('/^([0-9]{0,3}),([0-9]{0,3}),([0-9]{0,3})$/', $value, $matches)){
  58. array_shift($matches);
  59. $RGB[] = array_map(
  60. function($person) {
  61. return ($person > 255) ? 255 : $person;
  62. }, $matches);
  63. }else{
  64. $RGB[] = array(255, 0, 0);
  65. }
  66. }
  67. }else if(preg_match('/^[#]?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2}),[#]?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i', $color, $matches)){ //十六进制
  68. array_shift($matches);
  69. $RGB = array_chunk(
  70. array_map(function($person) {
  71. return hexdec($person);
  72. }, $matches), 3);
  73. }else{
  74. $RGB = array(array(255, 0, 0), array(0, 0, 0));
  75. }
  76. $im = $this->image($frame, $pixel, $margin, $RGB);
  77. if(is_file($iconurl)) $im = $this->addIcon($im, $iconurl, $RGB);
  78. return $im;
  79. } //END img
  80. //构造图层
  81. private function image($frame, $PixelPoint, $blank, $RGB){
  82. $PixelPoint = intval($PixelPoint);
  83. $blank = intval($blank);
  84. if($PixelPoint < 1) $PixelPoint = 1;
  85. $h = count($frame);
  86. $w = strlen($frame[0]);
  87. // $imgW = $h * $PixelPoint;
  88. // $imgH = $w * $PixelPoint;
  89. $imgW = $imgH = $PixelPoint;
  90. $chart = ImageCreateTrueColor($w, $h);//真彩
  91. $black = ImageColorAllocate($chart, $RGB[1][0], $RGB[1][1], $RGB[1][2]);//底色黑
  92. ImageFill($chart, 0, 0, ImageColorAllocate($chart, $RGB[0][0], $RGB[0][1], $RGB[0][2]));//图形着色->底色白
  93. for($y=0; $y<$h; ++$y){
  94. for($x=0; $x<$w; ++$x){
  95. if ($frame[$y][$x] == '1'){
  96. ImageSetPixel($chart,$x,$y,$black); //画一个单一像素
  97. }
  98. }
  99. }
  100. $basics = ImageCreateTrueColor($imgW, $imgH);
  101. ImageCopyResized($basics, $chart, 0, 0, 0, 0, $imgW, $imgH, $w, $h);//复制新图并调整大小
  102. ImageDestroy($chart); //销毁一图像
  103. if($blank > 0){//追加边框
  104. $rimW = $imgW+$blank;
  105. $rimH = $imgH+$blank;
  106. $rim = ImageCreateTrueColor($rimW, $rimH);
  107. ImageFill($rim, 0, 0, ImageColorAllocate($rim, $RGB[0][0], $RGB[0][1], $RGB[0][2]));
  108. ImageCopyResized($rim, $basics, $blank/2, $blank/2, 0, 0, $imgW, $imgH, $imgW, $imgH);
  109. ImageDestroy($basics);
  110. return $rim;
  111. }else{
  112. return $basics;
  113. }
  114. } //END image
  115. //ADD ICON (3.7948)
  116. // http://php.net/manual/zh/function.exif-imagetype.php 图像类型常量
  117. private function addIcon($im, $icon, $RGB){
  118. if(function_exists('exif_imagetype')){
  119. $type = exif_imagetype($icon);
  120. }else{
  121. list($width, $height, $type, $attr) = getimagesize($icon);
  122. }
  123. switch($type){
  124. case 1: //IMAGETYPE_GIF
  125. $ico = ImageCreateFromGIF($icon);
  126. break;
  127. case 2: //IMAGETYPE_JPEG
  128. $ico = ImageCreateFromJPEG($icon);
  129. break;
  130. case 3: //IMAGETYPE_PNG
  131. $ico = ImageCreateFromPNG($icon);
  132. break;
  133. default: exit('Image type or image path error. Support type: GIF JPG PNG');
  134. }
  135. $x = ImagesX($ico);
  136. $y = ImagesY($ico);
  137. $RadiusIco = $this->radiusImage($ico, $x, $y);
  138. //white Background
  139. $BackgX = $x/8+$x;
  140. $BackgY = $y/8+$y;
  141. $white_im = ImageCreateTrueColor($BackgX, $BackgY);
  142. $white_colo = ImageColorAllocate($white_im, $RGB[0][0], $RGB[0][1], $RGB[0][2]);
  143. ImageFill($white_im, 0, 0, $white_colo);
  144. $white_icox = $BackgX/2-$x/2+1;
  145. $white_icoy = $BackgY/2-$y/2+1;
  146. ImageCopyResized($white_im, $RadiusIco, $white_icox, $white_icoy, 0, 0, $x, $y, $x, $y);
  147. ImageDestroy($RadiusIco);
  148. $RadiusWhite = $this->radiusImage($white_im, $BackgX, $BackgY);
  149. ImageDestroy($white_im);
  150. //Transparent Background + alpha
  151. $AlphaIm = ImageCreateTrueColor($BackgX, $BackgY);
  152. $AlphaColo = ImageColorAllocateAlpha($AlphaIm, 255, 0, 0, 127);//分配颜色 + alpha
  153. ImageFill($AlphaIm, 0, 0, $AlphaColo);
  154. ImageCopyResized($AlphaIm, $RadiusWhite, 0, 0, 0, 0, $BackgX, $BackgY, $BackgX, $BackgY);
  155. ImageDestroy($RadiusWhite);
  156. imagesavealpha($AlphaIm, true);
  157. //QR Background Synthesis
  158. $radiusX = ImagesX($AlphaIm);
  159. $RadiusY = ImagesY($AlphaIm);
  160. $qrX = ImagesX($im);
  161. $qrY = ImagesY($im);
  162. $w = $qrX/3.7948;
  163. $h = $qrY/3.7948;
  164. $newX = $qrX/2-($w/2)+1;
  165. $newY = $qrY/2-($h/2)+1;
  166. ImageCopyResampled($im,$AlphaIm,$newX,$newY,0,0,$w,$h,$radiusX,$RadiusY);
  167. ImageDestroy($AlphaIm);
  168. return $im;
  169. } //END addIcon
  170. //图片圆角
  171. //GD 2.0.1 或更高版本
  172. private function radiusImage($im, $width, $height){
  173. $image = ImageCreateTrueColor($width, $height); //新建真彩图像
  174. $bgcolor = ImageColorAllocate($image, 255, 0, 0);
  175. ImageFill($image, 0, 0, $bgcolor);
  176. ImageCopyMerge($image, $im, 0, 0, 0, 0, $width, $height, 100);
  177. //radius Image
  178. $radiusX = $width/4;
  179. $radiusY = $height/4;
  180. $img = ImageCreateTrueColor($radiusX, $radiusY); //新建真彩图像
  181. $fgcolor = ImageColorAllocate($img, 0, 0, 0);
  182. $bgcolor = ImageColorAllocate($img, 255, 0, 0);
  183. ImageFill($img, 0, 0, $bgcolor);
  184. //180 270 开始角度到结束角度
  185. ImageFilledArc($img, $radiusX, $radiusY, $radiusX*2, $radiusY*2, 180, 270, $fgcolor, IMG_ARC_PIE);
  186. imagecolortransparent($img, $fgcolor);
  187. // Top left
  188. ImageCopyMerge($image, $img, 0, 0, 0, 0, $width, $height, 100);
  189. // Bottom left
  190. $bottom_left = ImageRotate($img, 90, $bgcolor); //角度旋转
  191. ImageCopyMerge($image, $bottom_left, 0, $height - $radiusY+1, 0, 0, $width, $height, 100);
  192. ImageDestroy($bottom_left);
  193. // Top right
  194. $top_right = ImageRotate($img, 180, $bgcolor);
  195. ImageCopyMerge($image, $top_right, $width - $radiusX+1, $height - $radiusY+1, 0, 0, $width, $height, 100);
  196. ImageDestroy($top_right);
  197. // Bottom right
  198. $bottom_right = ImageRotate($img, 270, $bgcolor);
  199. ImageCopyMerge($image, $bottom_right, $width - $radiusX+1, 0, 0, 0, $width, $height, 100);
  200. ImageDestroy($bottom_right);
  201. ImageDestroy($img);
  202. ImageColorTransparent($image, $bgcolor); //设置颜色为透明
  203. return $image;
  204. } //END radiusImage
  205. }//END class qrcode_image
  206. /*
  207. * QR Code Exchange 转换
  208. *
  209. * PHP QR Code is distributed under LGPL 3
  210. * Copyright (C) 2010 Dominik Dzienia
  211. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  212. */
  213. class qrcode_exchange{
  214. //二进制转换
  215. function binarize($frame){
  216. $len = count($frame);
  217. foreach ($frame as &$frameLine) {
  218. for($i=0; $i<$len; ++$i) {
  219. $frameLine[$i] = (ord($frameLine[$i])&1)?'1':'0';
  220. }
  221. }
  222. return $frame;
  223. }//END binarize
  224. //run 初始run
  225. function run($input, $width, $frame, $datacode){
  226. $this->runLength = array();
  227. $this->runLength = array_fill(0, 177 + 1, 0);
  228. $raw = new qrcode_string();
  229. $raw->datacode = $datacode;
  230. $raw->setString($input);
  231. $filler = new qrcode_exchange();
  232. $filler->initframe($width, $frame);
  233. for($i=0; $i<$raw->dataLength + $raw->eccLength; ++$i) {
  234. $code = $raw->getCode();
  235. $bit = 0x80;
  236. for($j=0; $j<8; ++$j) {
  237. $addr = $filler->nexts();
  238. $filler->setFrameAt($addr, 0x02 | (($bit & $code) != 0));
  239. $bit = $bit >> 1;
  240. }
  241. }
  242. $j = $this->getRemainder($input->version);
  243. for($i=0; $i<$j; ++$i) {
  244. $addr = $filler->nexts();
  245. $filler->setFrameAt($addr, 0x02);
  246. }
  247. return $this->mask($width, $filler->frame, $input->level);
  248. }//END run
  249. //initframe 初始框架数据
  250. private function initframe($width, $frame){
  251. $this->width = $width;
  252. $this->frame = $frame;
  253. $this->x = $width - 1;
  254. $this->y = $width - 1;
  255. $this->dir = -1;
  256. $this->bit = -1;
  257. }//END initframe
  258. //get 得到剩余
  259. private function getRemainder($version){
  260. return qrcode_table::$capacity[$version][2];
  261. }//END getRemainder
  262. //set设置框架
  263. private function setFrameAt($at, $val){
  264. $this->frame[$at['y']][$at['x']] = chr($val);
  265. }//END setFrameAt
  266. //next 紧邻
  267. private function nexts(){
  268. do {
  269. if($this->bit == -1) {
  270. $this->bit = 0;
  271. return array('x'=>$this->x, 'y'=>$this->y);
  272. }
  273. $x = $this->x;
  274. $y = $this->y;
  275. $w = $this->width;
  276. if($this->bit == 0) {
  277. --$x;
  278. ++$this->bit;
  279. } else {
  280. ++$x;
  281. $y += $this->dir;
  282. --$this->bit;
  283. }
  284. if($this->dir < 0) {
  285. if($y < 0) {
  286. $y = 0;
  287. $x -= 2;
  288. $this->dir = 1;
  289. if($x == 6) {
  290. --$x;
  291. $y = 9;
  292. }
  293. }
  294. } else {
  295. if($y == $w) {
  296. $y = $w - 1;
  297. $x -= 2;
  298. $this->dir = -1;
  299. if($x == 6) {
  300. --$x;
  301. $y -= 8;
  302. }
  303. }
  304. }
  305. if($x < 0 || $y < 0) return null;
  306. $this->x = $x;
  307. $this->y = $y;
  308. } while(ord($this->frame[$y][$x]) & 0x80);
  309. return array('x'=>$x, 'y'=>$y);
  310. }//END next
  311. //mask 掩码
  312. private function mask($width, $frame, $level){
  313. $minDemerit = PHP_INT_MAX;
  314. $bestMaskNum = 0;
  315. $bestMask = array();
  316. $checked_masks = array(0,1,2,3,4,5,6,7);
  317. $howManuOut = 8-(2 % 9);
  318. for ($i = 0; $i < $howManuOut; ++$i) {
  319. $remPos = rand (0, count($checked_masks)-1);
  320. unset($checked_masks[$remPos]);
  321. $checked_masks = array_values($checked_masks);
  322. }
  323. $bestMask = $frame;
  324. foreach($checked_masks as $i) {
  325. $mask = array_fill(0, $width, str_repeat("\0", $width));
  326. $demerit = 0;
  327. $blacks = 0;
  328. $blacks = $this->makeMaskNo($i, $width, $frame, $mask);
  329. $blacks += $this->writeFormatInformation($width, $mask, $i, $level);
  330. $blacks = (int)(100 * $blacks / ($width * $width));
  331. $demerit = (int)((int)(abs($blacks - 50) / 5) * 10);
  332. $demerit += $this->evaluateSymbol($width, $mask);
  333. if($demerit < $minDemerit) {
  334. $minDemerit = $demerit;
  335. $bestMask = $mask;
  336. $bestMaskNum = $i;
  337. }
  338. }
  339. return $bestMask;
  340. }//END mask
  341. //make 做掩码开关
  342. private function makeMaskNo($maskNo, $width, $s, &$d, $maskGenOnly = false) {
  343. $b = 0;
  344. $bitMask = array();
  345. $bitMask = $this->generateMaskNo($maskNo, $width, $s, $d);
  346. if ($maskGenOnly) return;
  347. $d = $s;
  348. for($y=0; $y<$width; ++$y) {
  349. for($x=0; $x<$width; ++$x) {
  350. if($bitMask[$y][$x] == 1) $d[$y][$x] = chr(ord($s[$y][$x]) ^ (int)$bitMask[$y][$x]);
  351. $b += (int)(ord($d[$y][$x]) & 1);
  352. }
  353. }
  354. return $b;
  355. }//NED makeMaskNo
  356. //generate 生成掩码开关
  357. private function generateMaskNo($maskNo, $width, $frame){
  358. $bitMask = array_fill(0, $width, array_fill(0, $width, 0));
  359. for($y=0; $y<$width; ++$y) {
  360. for($x=0; $x<$width; ++$x) {
  361. if(ord($frame[$y][$x]) & 0x80) {
  362. $bitMask[$y][$x] = 0;
  363. } else {
  364. $maskFunc = call_user_func(array($this, 'mask'.$maskNo), $x, $y);
  365. $bitMask[$y][$x] = ($maskFunc == 0)?1:0;
  366. }
  367. }
  368. }
  369. return $bitMask;
  370. }//NED generateMaskNo
  371. private function mask0($x, $y) { return ($x+$y)&1; }
  372. private function mask1($x, $y) { return ($y&1); }
  373. private function mask2($x, $y) { return ($x%3); }
  374. private function mask3($x, $y) { return ($x+$y)%3; }
  375. private function mask4($x, $y) { return (((int)($y/2))+((int)($x/3)))&1; }
  376. private function mask5($x, $y) { return (($x*$y)&1)+($x*$y)%3; }
  377. private function mask6($x, $y) { return ((($x*$y)&1)+($x*$y)%3)&1; }
  378. private function mask7($x, $y) { return ((($x*$y)%3)+(($x+$y)&1))&1; }
  379. //write 写信息
  380. private function writeFormatInformation($width, &$frame, $mask, $level){
  381. $blacks = 0;
  382. $format = $this->getFormatInfo($mask, $level);
  383. for($i=0; $i<8; ++$i) {
  384. if($format & 1) {
  385. $blacks += 2;
  386. $v = 0x85;
  387. } else {
  388. $v = 0x84;
  389. }
  390. $frame[8][$width - 1 - $i] = chr($v);
  391. if($i < 6) {
  392. $frame[$i][8] = chr($v);
  393. } else {
  394. $frame[$i + 1][8] = chr($v);
  395. }
  396. $format = $format >> 1;
  397. }
  398. for($i=0; $i<7; ++$i) {
  399. if($format & 1) {
  400. $blacks += 2;
  401. $v = 0x85;
  402. } else {
  403. $v = 0x84;
  404. }
  405. $frame[$width - 7 + $i][8] = chr($v);
  406. if($i == 0) {
  407. $frame[8][7] = chr($v);
  408. } else {
  409. $frame[8][6 - $i] = chr($v);
  410. }
  411. $format = $format >> 1;
  412. }
  413. return $blacks;
  414. }//END writeFormatInformation
  415. //get 获得格式信息
  416. private function getFormatInfo($mask, $level){
  417. if($mask < 0 || $mask > 7) return 0;
  418. if($level < 0 || $level > 3) return 0;
  419. return qrcode_table::$formatInfo[$level][$mask];
  420. }//END getFormatInfo
  421. //evaluate 评价符号
  422. private function evaluateSymbol($width, $frame){
  423. $head = 0;
  424. $demerit = 0;
  425. for($y=0; $y<$width; ++$y) {
  426. $head = 0;
  427. $this->runLength[0] = 1;
  428. $frameY = $frame[$y];
  429. if ($y>0) $frameYM = $frame[$y-1];
  430. for($x=0; $x<$width; ++$x) {
  431. if(($x > 0) && ($y > 0)) {
  432. $b22 = ord($frameY[$x]) & ord($frameY[$x-1]) & ord($frameYM[$x]) & ord($frameYM[$x-1]);
  433. $w22 = ord($frameY[$x]) | ord($frameY[$x-1]) | ord($frameYM[$x]) | ord($frameYM[$x-1]);
  434. if(($b22 | ($w22 ^ 1))&1) $demerit += 3;
  435. }
  436. if(($x == 0) && (ord($frameY[$x]) & 1)) {
  437. $this->runLength[0] = -1;
  438. $head = 1;
  439. $this->runLength[$head] = 1;
  440. } else if($x > 0) {
  441. if((ord($frameY[$x]) ^ ord($frameY[$x-1])) & 1) {
  442. ++$head;
  443. $this->runLength[$head] = 1;
  444. } else {
  445. ++$this->runLength[$head];
  446. }
  447. }
  448. }
  449. $demerit += $this->calcN1N3($head+1);
  450. }
  451. for($x=0; $x<$width; ++$x) {
  452. $head = 0;
  453. $this->runLength[0] = 1;
  454. for($y=0; $y<$width; ++$y) {
  455. if($y == 0 && (ord($frame[$y][$x]) & 1)) {
  456. $this->runLength[0] = -1;
  457. $head = 1;
  458. $this->runLength[$head] = 1;
  459. } else if($y > 0) {
  460. if((ord($frame[$y][$x]) ^ ord($frame[$y-1][$x])) & 1) {
  461. ++$head;
  462. $this->runLength[$head] = 1;
  463. }else{
  464. ++$this->runLength[$head];
  465. }
  466. }
  467. }
  468. $demerit += $this->calcN1N3($head+1);
  469. }
  470. return $demerit;
  471. }//END evaluateSymbol
  472. //calc 计算n1 n3模式
  473. private function calcN1N3($length){
  474. $demerit = 0;
  475. for($i=0; $i<$length; ++$i) {
  476. if($this->runLength[$i] >= 5) {
  477. $demerit += (3 + ($this->runLength[$i] - 5));
  478. }
  479. if($i & 1) {
  480. if(($i >= 3) && ($i < ($length-2)) && ($this->runLength[$i] % 3 == 0)) {
  481. $fact = (int)($this->runLength[$i] / 3);
  482. if(($this->runLength[$i-2] == $fact) && ($this->runLength[$i-1] == $fact) && ($this->runLength[$i+1] == $fact) && ($this->runLength[$i+2] == $fact)) {
  483. if(($this->runLength[$i-3] < 0) || ($this->runLength[$i-3] >= (4 * $fact))) {
  484. $demerit += 40;
  485. } else if((($i+3) >= $length) || ($this->runLength[$i+3] >= (4 * $fact))) {
  486. $demerit += 40;
  487. }
  488. }
  489. }
  490. }
  491. }
  492. return $demerit;
  493. }//END calcN1N3
  494. }//END class qrcode_exchange
  495. /*
  496. * QR Code String 文本处理
  497. *
  498. * PHP QR Code is distributed under LGPL 3
  499. * Copyright (C) 2010 Dominik Dzienia
  500. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  501. */
  502. class qrcode_string{
  503. //initial 初始化 public
  504. function initial($dataStr, $level, $spec){
  505. $this->dataStr = $dataStr; //Str 字符串
  506. $this->level = $this->GetGrade($level); //level 识别率
  507. if($spec < 1 ){
  508. $spec = 1;
  509. }elseif($spec > 40 ){
  510. $spec = 40;
  511. }
  512. $this->version = $spec; //version 版本
  513. $this->splitString();
  514. }//END initial
  515. //get 获取当前识别率 lovel
  516. private function GetGrade($i){
  517. return qrcode_table::$grade[strtoupper($i)];
  518. }//END GetGrade
  519. //----------------------------------------NO: 第四分支 附加 to------------------------------------------
  520. //set 字符 public
  521. function setString($input){
  522. $this->newcode();
  523. $spec = array(0,0,0,0,0);
  524. $this->getEccSpec($input->version, $input->level, $spec);
  525. $this->version = $input->version;
  526. $this->b1 = $this->rsBlockNum1($spec);
  527. $this->dataLength = $this->rsDataLength($spec);
  528. $this->eccLength = $this->rsEccLength($spec);
  529. $this->ecccode = array_fill(0, $this->eccLength, 0);
  530. $this->blocks = $this->rsBlockNum($spec);
  531. $ret = $this->init($spec);
  532. if($ret < 0) return null;
  533. $this->count = 0;
  534. return $this;
  535. }//END setString
  536. //new 新类
  537. private function newinit(){
  538. $this->alpha_to = $this->index_of = $this->genpoly = array(); // Generator polynomial
  539. $this->mm = $this->nn = $this->nroots = $this->fcr = $this->prim = $this->iprim = $this->pad = $this->gfpoly = NULL;
  540. }//NED newinit
  541. //new 新类
  542. private function newcode(){
  543. $this->ecccode = $this->rsblocks = array(); //of RSblock
  544. $this->blocks = $this->count = $this->dataLength = $this->eccLength = $this->b1 = NULL;
  545. }//NED newcode
  546. //ECC 获得ECC规格 ok
  547. private function getEccSpec($version, $level, array &$spec){
  548. if (count($spec) < 5) $spec = array(0,0,0,0,0);
  549. $b1 = qrcode_table::$eccTable[$version][$level][0];
  550. $b2 = qrcode_table::$eccTable[$version][$level][1];
  551. $data = $this->getDataLength($version, $level);
  552. $ecc = $this->getECCLength($version, $level);
  553. if($b2 == 0) {
  554. $spec[0] = $b1;
  555. $spec[1] = (int)($data / $b1);
  556. $spec[2] = (int)($ecc / $b1);
  557. $spec[3] = 0;
  558. $spec[4] = 0;
  559. } else {
  560. $spec[0] = $b1;
  561. $spec[1] = (int)($data / ($b1 + $b2));
  562. $spec[2] = (int)($ecc / ($b1 + $b2));
  563. $spec[3] = $b2;
  564. $spec[4] = $spec[1] + 1;
  565. }
  566. }//END getEccSpec
  567. //get 获取ECC长度
  568. private function getECCLength($version, $level){
  569. return qrcode_table::$capacity[$version][3][$level];
  570. }//END getECCLength
  571. private function rsBlockNum($spec) { return $spec[0] + $spec[3]; }
  572. private function rsEccLength($spec) { return ($spec[0] + $spec[3]) * $spec[2]; }
  573. private function rsDataLength($spec) { return ($spec[0] * $spec[1]) + ($spec[3] * $spec[4]); }
  574. private function rsBlockNum1($spec) { return $spec[0]; }
  575. private function rsDataCodes1($spec) { return $spec[1]; }
  576. private function rsEccCodes1($spec) { return $spec[2]; }
  577. private function rsBlockNum2($spec) { return $spec[3]; }
  578. private function rsDataCodes2($spec) { return $spec[4]; }
  579. private function rsEccCodes2($spec) { return $spec[2]; }
  580. //init 初始init
  581. private function init(array $spec){
  582. $this->items = array();
  583. $dl = $this->rsDataCodes1($spec);
  584. $el = $this->rsEccCodes1($spec);
  585. $rs = $this->init_rs(8, 0x11d, 0, 1, $el, 255 - $dl - $el);
  586. $blockNo = 0;
  587. $dataPos = 0;
  588. $eccPos = 0;
  589. for($i=0; $i<$this->rsBlockNum1($spec); ++$i) {
  590. $ecc = array_slice($this->ecccode,$eccPos);
  591. $this->rsblocks[$blockNo] = $this->qrrsblock($dl, array_slice($this->datacode, $dataPos), $el, $ecc, $rs);
  592. $this->ecccode = array_merge(array_slice($this->ecccode,0, $eccPos), $ecc);
  593. $dataPos += $dl;
  594. $eccPos += $el;
  595. ++$blockNo;
  596. }
  597. if($this->rsBlockNum2($spec) == 0) return 0;
  598. $dl = $this->rsDataCodes2($spec);
  599. $el = $this->rsEccCodes2($spec);
  600. $rs = $this->init_rs(8, 0x11d, 0, 1, $el, 255 - $dl - $el);
  601. if($rs == NULL) return -1;
  602. for($i=0; $i<$this->rsBlockNum2($spec); ++$i) {
  603. $ecc = array_slice($this->ecccode,$eccPos);
  604. $this->rsblocks[$blockNo] = $this->qrrsblock($dl, array_slice($this->datacode, $dataPos), $el, $ecc, $rs);
  605. $this->ecccode = array_merge(array_slice($this->ecccode,0, $eccPos), $ecc);
  606. $dataPos += $dl;
  607. $eccPos += $el;
  608. ++$blockNo;
  609. }
  610. return 0;
  611. }//END init
  612. //get 获取带吗 public
  613. function getCode(){
  614. if($this->count < $this->dataLength) {
  615. $row = $this->count % $this->blocks;
  616. $col = $this->count / $this->blocks;
  617. if($col >= $this->rsblocks[0]->dataLength) $row += $this->b1;
  618. $ret = $this->rsblocks[$row]->data[$col];
  619. } else if($this->count < $this->dataLength + $this->eccLength) {
  620. $row = ($this->count - $this->dataLength) % $this->blocks;
  621. $col = ($this->count - $this->dataLength) / $this->blocks;
  622. $ret = $this->rsblocks[$row]->ecc[$col];
  623. } else {
  624. return 0;
  625. }
  626. ++$this->count;
  627. return $ret;
  628. }//END get Code
  629. //init RS
  630. private function init_rs($symsize, $gfpoly, $fcr, $prim, $nroots, $pad){
  631. foreach($this->items as $rs) {
  632. if($rs->pad != $pad) continue;
  633. if($rs->nroots != $nroots) continue;
  634. if($rs->mm != $symsize) continue;
  635. if($rs->gfpoly != $gfpoly) continue;
  636. if($rs->fcr != $fcr) continue;
  637. if($rs->prim != $prim) continue;
  638. return $rs;
  639. }
  640. $rs = $this->init_rs_char($symsize, $gfpoly, $fcr, $prim, $nroots, $pad);
  641. array_unshift($this->items, $rs);
  642. return $rs;
  643. }//END init_rs
  644. //init RS字符
  645. private function init_rs_char($symsize, $gfpoly, $fcr, $prim, $nroots, $pad){
  646. $rs = null;
  647. // Check parameter ranges
  648. if($symsize < 0 || $symsize > 8) return $rs;
  649. if($fcr < 0 || $fcr >= (1<<$symsize)) return $rs;
  650. if($prim <= 0 || $prim >= (1<<$symsize)) return $rs;
  651. if($nroots < 0 || $nroots >= (1<<$symsize)) return $rs; // Can't have more roots than symbol values!
  652. if($pad < 0 || $pad >= ((1<<$symsize) -1 - $nroots)) return $rs; // Too much padding
  653. $rs = new qrcode_string();
  654. $rs->newinit();
  655. $rs->mm = $symsize;
  656. $rs->nn = (1<<$symsize)-1;
  657. $rs->pad = $pad;
  658. $rs->alpha_to = array_fill(0, $rs->nn+1, 0);
  659. $rs->index_of = array_fill(0, $rs->nn+1, 0);
  660. // PHP style macro replacement ;)
  661. $NN =& $rs->nn;
  662. $A0 =& $NN;
  663. // Generate Galois field lookup tables
  664. $rs->index_of[0] = $A0; // log(zero) = -inf
  665. $rs->alpha_to[$A0] = 0; // alpha**-inf = 0
  666. $sr = 1;
  667. for($i=0; $i<$rs->nn; ++$i) {
  668. $rs->index_of[$sr] = $i;
  669. $rs->alpha_to[$i] = $sr;
  670. $sr <<= 1;
  671. if($sr & (1<<$symsize)) {
  672. $sr ^= $gfpoly;
  673. }
  674. $sr &= $rs->nn;
  675. }
  676. if($sr != 1){
  677. // field generator polynomial is not primitive!
  678. $rs = NULL;
  679. return $rs;
  680. }
  681. $rs->genpoly = array_fill(0, $nroots+1, 0);
  682. $rs->fcr = $fcr;
  683. $rs->prim = $prim;
  684. $rs->nroots = $nroots;
  685. $rs->gfpoly = $gfpoly;
  686. for($iprim=1;($iprim % $prim) != 0;$iprim += $rs->nn)
  687. ; // intentional empty-body loop!
  688. $rs->iprim = (int)($iprim / $prim);
  689. $rs->genpoly[0] = 1;
  690. for ($i = 0,$root=$fcr*$prim; $i < $nroots; ++$i, $root += $prim) {
  691. $rs->genpoly[$i+1] = 1;
  692. // Multiply rs->genpoly[] by @**(root + x)
  693. for ($j = $i; $j > 0; --$j) {
  694. if ($rs->genpoly[$j] != 0) {
  695. $rs->genpoly[$j] = $rs->genpoly[$j-1] ^ $rs->alpha_to[$rs->modnn($rs->index_of[$rs->genpoly[$j]] + $root)];
  696. } else {
  697. $rs->genpoly[$j] = $rs->genpoly[$j-1];
  698. }
  699. }
  700. // rs->genpoly[0] can never be zero
  701. $rs->genpoly[0] = $rs->alpha_to[$rs->modnn($rs->index_of[$rs->genpoly[0]] + $root)];
  702. }
  703. // convert rs->genpoly[] to index form for quicker encoding
  704. for ($i = 0; $i <= $nroots; ++$i) $rs->genpoly[$i] = $rs->index_of[$rs->genpoly[$i]];
  705. return $rs;
  706. }//END init_rs_char
  707. //modnn
  708. private function modnn($x){
  709. while ($x >= $this->nn) {
  710. $x -= $this->nn;
  711. $x = ($x >> $this->mm) + ($x & $this->nn);
  712. }
  713. return $x;
  714. }//END modnn
  715. //qrrsblock RSB锁
  716. private function qrrsblock($dl, $data, $el, &$ecc, $rs){
  717. $qrcode = new qrcode_string();
  718. $rs->encode_rs_char($data, $ecc);
  719. $qrcode->dataLength = $dl;
  720. $qrcode->data = $data;
  721. $qrcode->eccLength = $el;
  722. $qrcode->ecc = $ecc;
  723. return $qrcode;
  724. }//END qrrsblock
  725. //encode 编码RS字符
  726. private function encode_rs_char($data, &$parity){
  727. $MM =& $this->mm;
  728. $NN =& $this->nn;
  729. $ALPHA_TO =& $this->alpha_to;
  730. $INDEX_OF =& $this->index_of;
  731. $GENPOLY =& $this->genpoly;
  732. $NROOTS =& $this->nroots;
  733. $FCR =& $this->fcr;
  734. $PRIM =& $this->prim;
  735. $IPRIM =& $this->iprim;
  736. $PAD =& $this->pad;
  737. $A0 =& $NN;
  738. $parity = array_fill(0, $NROOTS, 0);
  739. for($i=0; $i< ($NN-$NROOTS-$PAD); ++$i) {
  740. $feedback = $INDEX_OF[$data[$i] ^ $parity[0]];
  741. if($feedback != $A0) {
  742. $feedback = $this->modnn($NN - $GENPOLY[$NROOTS] + $feedback);
  743. for($j=1;$j<$NROOTS;++$j) {
  744. $parity[$j] ^= $ALPHA_TO[$this->modnn($feedback + $GENPOLY[$NROOTS-$j])];
  745. }
  746. }
  747. array_shift($parity);
  748. if($feedback != $A0) {
  749. array_push($parity, $ALPHA_TO[$this->modnn($feedback + $GENPOLY[0])]);
  750. } else {
  751. array_push($parity, 0);
  752. }
  753. }
  754. }//END encode_rs_char
  755. //----------------------------------------NO: 第三分支 流系列 to------------------------------------------
  756. //get 得到字节流 public
  757. function getByteStream(){
  758. $bstream = $this->getBitStream();
  759. if($bstream == null) return null;
  760. return $bstream->toByte();
  761. }// END getByteStream
  762. //get 编码流
  763. private function getBitStream(){
  764. $bstream = $this->mergeBitStream();
  765. if($bstream == null) return null;
  766. $ret = $this->appendPaddingBit($bstream);
  767. if($ret < 0) return null;
  768. return $bstream;
  769. }//END getBitStream
  770. //to 字节
  771. private function toByte(){
  772. $size = $this->size();
  773. if($size == 0) return array();
  774. $data = array_fill(0, (int)(($size + 7) / 8), 0);
  775. $bytes = (int)($size / 8);
  776. $p = 0;
  777. for($i=0; $i<$bytes; ++$i) {
  778. $v = 0;
  779. for($j=0; $j<8; ++$j) {
  780. $v = $v << 1;
  781. $v |= $this->data[$p];
  782. ++$p;
  783. }
  784. $data[$i] = $v;
  785. }
  786. if($size & 7) {
  787. $v = 0;
  788. for($j=0; $j<($size & 7); ++$j) {
  789. $v = $v << 1;
  790. $v |= $this->data[$p];
  791. ++$p;
  792. }
  793. $data[$bytes] = $v;
  794. }
  795. return $data;
  796. }// toByte
  797. //merge 合并流
  798. private function mergeBitStream(){
  799. if($this->convertData() < 0) return null;
  800. $bstream = new qrcode_string();
  801. $bstream->data = array();
  802. foreach($this->items as $item){
  803. $ret = $bstream->append_size($item->bstream);
  804. if($ret < 0) return null;
  805. }
  806. return $bstream;
  807. }//END mergeBitStream
  808. //size 数组数量
  809. private function size(){
  810. return count($this->data);
  811. }//END size
  812. //append 追加
  813. private function append($mode, $size, $data){
  814. try {
  815. $entry = new qrcode_string();
  816. $entry->data = array();
  817. $entry->assignment($mode, $size, $data);
  818. $this->items[] = $entry;
  819. return 0;
  820. } catch (\Exception $e){
  821. return -1;
  822. }
  823. }//END append
  824. //size 追加size
  825. private function append_size($arg){
  826. if (is_null($arg)) return -1;
  827. if($arg->size() == 0) return 0;
  828. if($this->size() == 0){
  829. $this->data = $arg->data;
  830. return 0;
  831. }
  832. $this->data = array_values(array_merge($this->data, $arg->data));
  833. return 0;
  834. }//END append_size
  835. //QRinputItem 赋值
  836. private function assignment($mode, $size, $data, $bstream = null){
  837. $setData = array_slice($data, 0, $size);
  838. if (count($setData) < $size) $setData = array_merge($setData, array_fill(0,$size-count($setData),0));
  839. if(!$this->check($mode, $size, $setData)){
  840. //ERR::\Exception('Error m:'.$mode.',s:'.$size.',d:'.join(',',$setData));
  841. return null;
  842. }
  843. $this->mode = $mode;
  844. $this->size = $size;
  845. $this->data = $setData;
  846. $this->bstream = $bstream;
  847. return $this;
  848. }//END assignment
  849. //check 检查
  850. private function check($mode, $size, $data){
  851. if($size <= 0) return false;
  852. switch($mode) {
  853. case 0: return $this->checkModeNum($size, $data); break;
  854. case 1: return $this->checkModeAn($size, $data); break;
  855. // case 3: return self::checkModeKanji($size, $data); break;
  856. case 2: return true; break;
  857. case 4: return true; break;
  858. default: break;
  859. }
  860. return false;
  861. }//END check
  862. //check 检查数模式
  863. private function checkModeNum($size, $data){
  864. for($i=0; $i<$size; ++$i) {
  865. if((ord($data[$i]) < ord('0')) || (ord($data[$i]) > ord('9'))) return false;
  866. }
  867. return true;
  868. }//END checkModeNum
  869. //check 检查单模式
  870. private function checkModeAn($size, $data){
  871. for($i=0; $i<$size; ++$i) {
  872. if ($this->lookAnTable(ord($data[$i])) == -1) return false;
  873. }
  874. return true;
  875. }//END checkModeAn
  876. //append 附加填充位
  877. private function appendPaddingBit($bstream){
  878. $bits = $bstream->size();
  879. $maxwords = $this->getDataLength($this->version, $this->level);
  880. $maxbits = $maxwords * 8;
  881. if ($maxbits == $bits) return 0;
  882. if ($maxbits - $bits < 5) return $bstream->appendNum($maxbits - $bits, 0);
  883. $bits += 4;
  884. $words = (int)(($bits + 7) / 8);
  885. $padding = new qrcode_string();
  886. $padding->data = array();
  887. $ret = $padding->appendNum($words * 8 - $bits + 4, 0);
  888. if($ret < 0) return $ret;
  889. $padlen = $maxwords - $words;
  890. if($padlen > 0){
  891. $padbuf = array();
  892. for($i=0; $i<$padlen; ++$i){
  893. $padbuf[$i] = ($i&1)?0x11:0xec;
  894. }
  895. $ret = $padding->appendBytes($padlen, $padbuf);
  896. if($ret < 0) return $ret;
  897. }
  898. $ret = $bstream->append_size($padding);
  899. return $ret;
  900. }//END appendPaddingBit
  901. //get 得到数据长度
  902. private function getDataLength($version, $level){
  903. return qrcode_table::$capacity[$version][1] - qrcode_table::$capacity[$version][3][$level];
  904. }//END getDataLength
  905. //appendNum 追加数
  906. private function appendNum($bits, $num){
  907. if ($bits == 0) return 0;
  908. $bstream = new qrcode_string();
  909. $bstream->data = array();
  910. $b = $this->newFromNum($bits, $num);
  911. if(is_null($b)) return -1;
  912. $ret = $this->append_size($b);
  913. unset($b);
  914. return $ret;
  915. }//END appendNum
  916. //new 新数
  917. private function newFromNum($bits, $num){
  918. $bstream = new qrcode_string();
  919. $bstream->data = array();
  920. $bstream->allocate($bits);
  921. $mask = 1 << ($bits - 1);
  922. for($i=0; $i<$bits; ++$i){
  923. if($num & $mask){
  924. $bstream->data[$i] = 1;
  925. } else {
  926. $bstream->data[$i] = 0;
  927. }
  928. $mask = $mask >> 1;
  929. }
  930. return $bstream;
  931. }//END newFromNum
  932. //allocate 分配
  933. private function allocate($setLength){
  934. $this ->data = array_fill(0, $setLength, 0);
  935. return 0;
  936. }//END allocate
  937. //append 追加字节
  938. private function appendBytes($size, $data){
  939. if ($size == 0) return 0;
  940. $bstream = new qrcode_string();
  941. $bstream->data = array();
  942. $b = $bstream->newFromBytes($size, $data);
  943. if(is_null($b)) return -1;
  944. $ret = $this->append_size($b);
  945. unset($b);
  946. return $ret;
  947. }//END appendBytes
  948. //new 新的字节
  949. private function newFromBytes($size, $data){
  950. $bstream = new qrcode_string();
  951. $bstream->data = array();
  952. $bstream->allocate($size * 8);
  953. $p=0;
  954. for($i=0; $i<$size; ++$i) {
  955. $mask = 0x80;
  956. for($j=0; $j<8; ++$j) {
  957. if($data[$i] & $mask) {
  958. $bstream->data[$p] = 1;
  959. } else {
  960. $bstream->data[$p] = 0;
  961. }
  962. ++$p;
  963. $mask = $mask >> 1;
  964. }
  965. }
  966. return $bstream;
  967. }//END newFromBytes
  968. //set 设置版本
  969. private function setVersion($version){
  970. $this->version = $version;
  971. }//END setVersion
  972. //convert 转换数据
  973. private function convertData(){
  974. $ver = $this->estimateVersion();
  975. if($ver > $this->version) $this->setVersion($ver);
  976. for(;;){//to 死循环 break 条件停止
  977. $bits = $this->createBitStream();
  978. if($bits < 0) return -1;
  979. $ver = $this->getMinimumVersion((int)(($bits + 7) / 8), $this->level);
  980. if($ver < 0){
  981. //ERR::newerr( __LINE__ ,'ERROR: WRONG VERSION');
  982. return -1;
  983. } else if($ver > $this->version){
  984. $this->setVersion($ver);
  985. } else {
  986. break;
  987. }
  988. }
  989. return 0;
  990. }//END convertData
  991. //estimate 估计版本
  992. private function estimateVersion(){
  993. $version = 0;
  994. $prev = 0;
  995. do {
  996. $prev = $version;
  997. $bits = $this->estimateBitStreamSize($prev);
  998. $version = $this->getMinimumVersion((int)(($bits + 7) / 8), $this->level);
  999. if ($version < 0){
  1000. return -1;
  1001. }
  1002. } while ($version > $prev);
  1003. return $version;
  1004. }//END estimateVersion
  1005. //create 创建流
  1006. private function createBitStream(){
  1007. $total = 0;
  1008. foreach($this->items as $item){
  1009. $bits = $item->encodeBitStream($this->version);
  1010. if($bits < 0) return -1;
  1011. $total += $bits;
  1012. }
  1013. return $total;
  1014. }//END createBitStream
  1015. //encodeBitStream 编码比特流
  1016. private function encodeBitStream($version){
  1017. try {
  1018. unset($this->bstream);
  1019. $words = $this->maximumWords($this->mode, $version);
  1020. if($this->size > $words){
  1021. $st1 = $st2 = new qrcode_string();
  1022. $st1->data = $st2->data = array();
  1023. $st1->assignment($this->mode, $words, $this->data);
  1024. $st2->assignment($this->mode, $this->size - $words, array_slice($this->data, $words));
  1025. $st1->encodeBitStream($version);
  1026. $st2->encodeBitStream($version);
  1027. $this->bstream = new qrcode_string();
  1028. $this->bstream->data = array();
  1029. $this->append_size($st1->bstream);
  1030. $this->append_size($st2->bstream);
  1031. unset($st1);
  1032. unset($st2);
  1033. } else {
  1034. $ret = 0;
  1035. switch($this->mode){
  1036. case 0: $ret = $this->encodeModeNum($version); break;
  1037. case 1: $ret = $this->encodeModeAn($version); break;
  1038. case 2: $ret = $this->encodeMode8($version); break;
  1039. //-------------------------- case 3: $ret = $this->encodeModeKanji($version);break; ----------
  1040. case 4: $ret = $this->encodeModeStructure(); break;
  1041. default: break;
  1042. }
  1043. if($ret < 0) return -1;
  1044. }
  1045. return $this->size();
  1046. } catch (\Exception $e){
  1047. return -1;
  1048. }
  1049. }//END encodeBitStream
  1050. //maximum 最大词
  1051. private function maximumWords($mode, $version){
  1052. if($mode == 4) return 3;
  1053. if($version <= 9){
  1054. $l = 0;
  1055. } else if($version <= 26){
  1056. $l = 1;
  1057. } else {
  1058. $l = 2;
  1059. }
  1060. $bits = qrcode_table::$lengthTableBits[$mode][$l];
  1061. $words = (1 << $bits) - 1;
  1062. if($mode == 3) $words *= 2; // the number of bytes is required
  1063. return $words;
  1064. }//END maximumWords
  1065. //encode 数编码模式
  1066. private function encodeModeNum($version){
  1067. try {
  1068. $words = (int)($this->size / 3);
  1069. $bstream = new qrcode_string();
  1070. $bstream->data = array();
  1071. $val = 0x1;
  1072. $bstream->appendNum(4, $val);
  1073. $bstream->appendNum($this->lengthIndicator(0, $version), $this->size);
  1074. for($i=0; $i<$words; ++$i){
  1075. $val = (ord($this->data[$i*3 ]) - ord('0')) * 100;
  1076. $val += (ord($this->data[$i*3+1]) - ord('0')) * 10;
  1077. $val += (ord($this->data[$i*3+2]) - ord('0'));
  1078. $bstream->appendNum(10, $val);
  1079. }
  1080. if($this->size - $words * 3 == 1){
  1081. $val = ord($this->data[$words*3]) - ord('0');
  1082. $bstream->appendNum(4, $val);
  1083. } else if($this->size - $words * 3 == 2){
  1084. $val = (ord($this->data[$words*3 ]) - ord('0')) * 10;
  1085. $val += (ord($this->data[$words*3+1]) - ord('0'));
  1086. $bstream->appendNum(7, $val);
  1087. }
  1088. $this->bstream = $bstream;
  1089. return 0;
  1090. } catch (\Exception $e){
  1091. return -1;
  1092. }
  1093. }//END encodeModeNum
  1094. //encode 编码单方式
  1095. private function encodeModeAn($version){
  1096. try {
  1097. $words = (int)($this->size / 2);
  1098. $bstream = new qrcode_string();
  1099. $bstream->data = array();
  1100. $bstream->appendNum(4, 0x02);
  1101. $bstream->appendNum($this->lengthIndicator(1, $version), $this->size);
  1102. for($i=0; $i<$words; ++$i) {
  1103. $val = (int)$this->lookAnTable(ord($this->data[$i*2 ])) * 45;
  1104. $val += (int)$this->lookAnTable(ord($this->data[$i*2+1]));
  1105. $bstream->appendNum(11, $val);
  1106. }
  1107. if($this->size & 1) {
  1108. $val = $this->lookAnTable(ord($this->data[$words * 2]));
  1109. $bstream->appendNum(6, $val);
  1110. }
  1111. $this->bstream = $bstream;
  1112. return 0;
  1113. } catch (\Exception $e) {
  1114. return -1;
  1115. }
  1116. }//END encodeModeAn
  1117. //encode 编码8模式
  1118. private function encodeMode8($version){
  1119. try {
  1120. $bstream = new qrcode_string();
  1121. $bstream->data = array();
  1122. $bstream->appendNum(4, 0x4);
  1123. $bstream->appendNum($this->lengthIndicator(2, $version), $this->size);
  1124. for($i=0; $i<$this->size; ++$i) {
  1125. $bstream->appendNum(8, ord($this->data[$i]));
  1126. }
  1127. $this->bstream = $bstream;
  1128. return 0;
  1129. } catch (\Exception $e) {
  1130. return -1;
  1131. }
  1132. }//END encodeMode8
  1133. //encode 编码结构模式
  1134. private function encodeModeStructure(){
  1135. try {
  1136. $bstream = new qrcode_string();
  1137. $bstream->data = array();
  1138. $bstream->appendNum(4, 0x03);
  1139. $bstream->appendNum(4, ord($this->data[1]) - 1);
  1140. $bstream->appendNum(4, ord($this->data[0]) - 1);
  1141. $bstream->appendNum(8, ord($this->data[2]));
  1142. $this->bstream = $bstream;
  1143. return 0;
  1144. } catch (\Exception $e) {
  1145. return -1;
  1146. }
  1147. }// END encodeModeStructure
  1148. //get 获得最小版本
  1149. private function getMinimumVersion($size, $level){
  1150. for($i=1; $i<= 40; ++$i){
  1151. $words = qrcode_table::$capacity[$i][1] - qrcode_table::$capacity[$i][3][$level];
  1152. if($words >= $size) return $i;
  1153. }
  1154. return -1;
  1155. }//END getMinimumVersion
  1156. //estimate 估计比特流大小
  1157. private function estimateBitStreamSize($version){
  1158. $bits = 0;
  1159. foreach($this->items as $item){
  1160. $bits += $item->estimateBitStreamSizeOfEntry($version);
  1161. }
  1162. return $bits;
  1163. }//END estimateBitStreamSize
  1164. //estimate 输入估计比特流大小
  1165. private function estimateBitStreamSizeOfEntry($version){
  1166. $bits = 0;
  1167. if($version == 0) $version = 1;
  1168. switch($this->mode){
  1169. case 0: $bits = $this->estimateBitsModeNum($this->size); break;
  1170. case 1: $bits = $this->estimateBitsModeAn($this->size); break;
  1171. case 2: $bits = $this->estimateBitsMode8($this->size); break;
  1172. // --------------- case 3: $bits = $this->estimateBitsModeKanji($this->size); break;
  1173. case 4: return 20;
  1174. default: return 0;
  1175. }
  1176. $l = $this->lengthIndicator($this->mode, $version);
  1177. $m = 1 << $l;
  1178. $num = (int)(($this->size + $m - 1) / $m);
  1179. $bits += $num * (4 + $l);
  1180. return $bits;
  1181. }//END estimateBitStreamSizeOfEntry
  1182. //----------------------------------------NO: 第二分支 字符串系列 to------------------------------------------
  1183. //split 拆分字符串
  1184. private function splitString(){
  1185. while (strlen($this->dataStr) > 0){
  1186. if($this->dataStr == '') return 0;
  1187. $mode = $this->identifyMode(0);
  1188. switch ($mode){
  1189. case 0: $length = $this->eatNum(); break;
  1190. case 1: $length = $this->eatAn(); break;
  1191. //---------------- case 3: $length = $this->eatKanji(); break;
  1192. default: $length = $this->eat8(); break;
  1193. }
  1194. if($length == 0) return 0;
  1195. if($length < 0) return -1;
  1196. $this->dataStr = substr($this->dataStr, $length);
  1197. }
  1198. $items = $this->items;
  1199. unset($this->dataStr);//delete
  1200. return $items;
  1201. }//END splitString
  1202. //Num 数字
  1203. private function eatNum(){
  1204. $ln = $this->lengthIndicator(0, $this->version);
  1205. $p = 0;
  1206. while($this->isdigitat($this->dataStr, $p)){
  1207. ++$p;
  1208. }
  1209. $run = $p;
  1210. $mode = $this->identifyMode($p);
  1211. if($mode == 2){
  1212. $dif = $this->estimateBitsModeNum($run) + 4 + $ln
  1213. + $this->estimateBitsMode8(1) // + 4 + l8
  1214. - $this->estimateBitsMode8($run + 1); // - 4 - l8
  1215. if($dif > 0) return $this->eat8();
  1216. }
  1217. if($mode == 1){
  1218. $dif = $this->estimateBitsModeNum($run) + 4 + $ln
  1219. + $this->estimateBitsModeAn(1) // + 4 + la
  1220. - $this->estimateBitsModeAn($run + 1);// - 4 - la
  1221. if($dif > 0) return $this->eatAn();
  1222. }
  1223. $ret = $this->append(0, $run, str_split($this->dataStr));
  1224. if($ret < 0) return -1;
  1225. return $run;
  1226. }//END eatNum
  1227. //An 单个
  1228. private function eatAn(){
  1229. $la = $this->lengthIndicator(1, $this->version);
  1230. $ln = $this->lengthIndicator(0, $this->version);
  1231. $p = 0;
  1232. while($this->isalnumat($this->dataStr, $p)){
  1233. if($this->isdigitat($this->dataStr, $p)){
  1234. $q = $p;
  1235. while($this->isdigitat($this->dataStr, $q)){
  1236. ++$q;
  1237. }
  1238. $dif = $this->estimateBitsModeAn($p) // + 4 + la
  1239. + $this->estimateBitsModeNum($q - $p) + 4 + $ln
  1240. - $this->estimateBitsModeAn($q); // - 4 - la
  1241. if($dif < 0){
  1242. break;
  1243. } else {
  1244. $p = $q;
  1245. }
  1246. } else {
  1247. ++$p;
  1248. }
  1249. }
  1250. $run = $p;
  1251. if(!$this->isalnumat($this->dataStr, $p)){
  1252. $dif = $this->estimateBitsModeAn($run) + 4 + $la
  1253. + $this->estimateBitsMode8(1) // + 4 + l8
  1254. - $this->estimateBitsMode8($run + 1); // - 4 - l8
  1255. if($dif > 0) return $this->eat8();
  1256. }
  1257. $ret = $this->append(1, $run, str_split($this->dataStr));
  1258. if($ret < 0) return -1;
  1259. return $run;
  1260. }//END eatAn
  1261. /*----------------------------------
  1262. //Kanji 汉字
  1263. function eatKanji(){
  1264. $p = 0;
  1265. while($this->identifyMode($p) == 3){
  1266. $p += 2;
  1267. }
  1268. $ret = $this->append(3, $p, str_split($this->dataStr));
  1269. if($ret < 0) return -1;
  1270. return $run;
  1271. }//END eatKanji
  1272. ------------------------------ */
  1273. //eat8
  1274. private function eat8(){
  1275. $la = $this->lengthIndicator(1, $this->version);
  1276. $ln = $this->lengthIndicator(0, $this->version);
  1277. $p = 1;
  1278. $dataStrLen = strlen($this->dataStr);
  1279. while($p < $dataStrLen){
  1280. $mode = $this->identifyMode($p);
  1281. if($mode == 3) break; //xxxxxxx
  1282. if($mode == 0){
  1283. $q = $p;
  1284. while($this->isdigitat($this->dataStr, $q)){
  1285. ++$q;
  1286. }
  1287. $dif = $this->estimateBitsMode8($p) // + 4 + l8
  1288. + $this->estimateBitsModeNum($q - $p) + 4 + $ln
  1289. - $this->estimateBitsMode8($q); // - 4 - l8
  1290. if($dif < 0){
  1291. break;
  1292. } else {
  1293. $p = $q;
  1294. }
  1295. } else if($mode == 1){
  1296. $q = $p;
  1297. while($this->isalnumat($this->dataStr, $q)){
  1298. ++$q;
  1299. }
  1300. $dif = $this->estimateBitsMode8($p) // + 4 + l8
  1301. + $this->estimateBitsModeAn($q - $p) + 4 + $la
  1302. - $this->estimateBitsMode8($q); // - 4 - l8
  1303. if($dif < 0){
  1304. break;
  1305. } else {
  1306. $p = $q;
  1307. }
  1308. } else {
  1309. ++$p;
  1310. }
  1311. }
  1312. $run = $p;
  1313. $ret = $this->append(2, $run, str_split($this->dataStr));
  1314. if($ret < 0) return -1;
  1315. return $run;
  1316. }//END eat8
  1317. //length 长度指示
  1318. private function lengthIndicator($mode, $version){
  1319. if ($mode == 4) return 0;
  1320. if ($version <= 9){
  1321. $l = 0;
  1322. } else if ($version <= 26){
  1323. $l = 1;
  1324. } else {
  1325. $l = 2;
  1326. }
  1327. return qrcode_table::$lengthTableBits[$mode][$l];
  1328. }//END lengthIndicator
  1329. //estimate 估算位数 数字
  1330. private function estimateBitsModeNum($size){
  1331. $w = (int)$size / 3;
  1332. $bits = $w * 10;
  1333. switch($size - $w * 3){
  1334. case 1: $bits += 4; break;
  1335. case 2: $bits += 7; break;
  1336. default: break;
  1337. }
  1338. return $bits;
  1339. }//END estimateBitsModeNum
  1340. //estimate 估算位数 模式8
  1341. private function estimateBitsMode8($size){
  1342. return $size * 8;
  1343. }//END estimateBitsMode8
  1344. /*-------------------------
  1345. //estimate 估计比特模式汉字
  1346. function estimateBitsModeKanji($size){
  1347. return (int)(($size / 2) * 13);
  1348. }//END estimate
  1349. -------------------------------------------*/
  1350. //estimate 估算位数 单个
  1351. private function estimateBitsModeAn($size){
  1352. $w = (int)($size / 2);
  1353. $bits = $w * 11;
  1354. if($size & 1) $bits += 6;
  1355. return $bits;
  1356. }//END estimateBitsModeAn
  1357. //----------------------------------------NO: 第一分支 模式系列 to------------------------------------------
  1358. //identify 匹配ID模式
  1359. private function identifyMode($pos){
  1360. if($pos >= strlen($this->dataStr)) return -1;
  1361. $c = $this->dataStr[$pos];
  1362. if($this->isdigitat($this->dataStr, $pos)){
  1363. return 0;
  1364. }else if($this->isalnumat($this->dataStr, $pos)){
  1365. return 1;
  1366. }
  1367. /*----------------------------------------------
  1368. else if($this->modeHint == 3){ // modeHint =0/2 不可能是3
  1369. if($pos+1 < strlen($this->dataStr)){
  1370. $d = $this->dataStr[$pos+1];
  1371. $word = (ord($c) << 8) | ord($d);
  1372. if(($word >= 0x8140 && $word <= 0x9ffc) || ($word >= 0xe040 && $word <= 0xebbf)){
  1373. return 3;
  1374. }
  1375. }
  1376. }
  1377. ---------------------------------- */
  1378. return 2;
  1379. }//END identifyMode
  1380. //isdigitat 是否数字
  1381. private function isdigitat($str, $pos){
  1382. if ($pos >= strlen($str)) return false;
  1383. return ((ord($str[$pos]) >= ord('0'))&&(ord($str[$pos]) <= ord('9')));
  1384. }//END isdigitat
  1385. //isalnumat 是否
  1386. private function isalnumat($str, $pos){
  1387. if ($pos >= strlen($str)) return false;
  1388. return ($this->lookAnTable(ord($str[$pos])) >= 0);
  1389. }//END isalnumat
  1390. //Table 查询表
  1391. private function lookAnTable($c){
  1392. return (($c > 127) ? -1 : qrcode_table::$anTable[$c]);
  1393. }//END lookAnTable
  1394. }//END class qrcode_string
  1395. /*
  1396. * QR Code Table 表
  1397. * PHP QR Code is distributed under LGPL 3
  1398. * Copyright (C) 2010 Dominik Dzienia
  1399. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  1400. */
  1401. class qrcode_table{
  1402. //Info 格式信息
  1403. public static $formatInfo = array(
  1404. array(0x77c4, 0x72f3, 0x7daa, 0x789d, 0x662f, 0x6318, 0x6c41, 0x6976),
  1405. array(0x5412, 0x5125, 0x5e7c, 0x5b4b, 0x45f9, 0x40ce, 0x4f97, 0x4aa0),
  1406. array(0x355f, 0x3068, 0x3f31, 0x3a06, 0x24b4, 0x2183, 0x2eda, 0x2bed),
  1407. array(0x1689, 0x13be, 0x1ce7, 0x19d0, 0x0762, 0x0255, 0x0d0c, 0x083b)
  1408. );
  1409. //ECC表
  1410. public static $eccTable = array(
  1411. array(array( 0, 0), array( 0, 0), array( 0, 0), array( 0, 0)),
  1412. array(array( 1, 0), array( 1, 0), array( 1, 0), array( 1, 0)), // 1
  1413. array(array( 1, 0), array( 1, 0), array( 1, 0), array( 1, 0)),
  1414. array(array( 1, 0), array( 1, 0), array( 2, 0), array( 2, 0)),
  1415. array(array( 1, 0), array( 2, 0), array( 2, 0), array( 4, 0)),
  1416. array(array( 1, 0), array( 2, 0), array( 2, 2), array( 2, 2)), // 5
  1417. array(array( 2, 0), array( 4, 0), array( 4, 0), array( 4, 0)),
  1418. array(array( 2, 0), array( 4, 0), array( 2, 4), array( 4, 1)),
  1419. array(array( 2, 0), array( 2, 2), array( 4, 2), array( 4, 2)),
  1420. array(array( 2, 0), array( 3, 2), array( 4, 4), array( 4, 4)),
  1421. array(array( 2, 2), array( 4, 1), array( 6, 2), array( 6, 2)), //10
  1422. array(array( 4, 0), array( 1, 4), array( 4, 4), array( 3, 8)),
  1423. array(array( 2, 2), array( 6, 2), array( 4, 6), array( 7, 4)),
  1424. array(array( 4, 0), array( 8, 1), array( 8, 4), array(12, 4)),
  1425. array(array( 3, 1), array( 4, 5), array(11, 5), array(11, 5)),
  1426. array(array( 5, 1), array( 5, 5), array( 5, 7), array(11, 7)), //15
  1427. array(array( 5, 1), array( 7, 3), array(15, 2), array( 3, 13)),
  1428. array(array( 1, 5), array(10, 1), array( 1, 15), array( 2, 17)),
  1429. array(array( 5, 1), array( 9, 4), array(17, 1), array( 2, 19)),
  1430. array(array( 3, 4), array( 3, 11), array(17, 4), array( 9, 16)),
  1431. array(array( 3, 5), array( 3, 13), array(15, 5), array(15, 10)), //20
  1432. array(array( 4, 4), array(17, 0), array(17, 6), array(19, 6)),
  1433. array(array( 2, 7), array(17, 0), array( 7, 16), array(34, 0)),
  1434. array(array( 4, 5), array( 4, 14), array(11, 14), array(16, 14)),
  1435. array(array( 6, 4), array( 6, 14), array(11, 16), array(30, 2)),
  1436. array(array( 8, 4), array( 8, 13), array( 7, 22), array(22, 13)), //25
  1437. array(array(10, 2), array(19, 4), array(28, 6), array(33, 4)),
  1438. array(array( 8, 4), array(22, 3), array( 8, 26), array(12, 28)),
  1439. array(array( 3, 10), array( 3, 23), array( 4, 31), array(11, 31)),
  1440. array(array( 7, 7), array(21, 7), array( 1, 37), array(19, 26)),
  1441. array(array( 5, 10), array(19, 10), array(15, 25), array(23, 25)), //30
  1442. array(array(13, 3), array( 2, 29), array(42, 1), array(23, 28)),
  1443. array(array(17, 0), array(10, 23), array(10, 35), array(19, 35)),
  1444. array(array(17, 1), array(14, 21), array(29, 19), array(11, 46)),
  1445. array(array(13, 6), array(14, 23), array(44, 7), array(59, 1)),
  1446. array(array(12, 7), array(12, 26), array(39, 14), array(22, 41)), //35
  1447. array(array( 6, 14), array( 6, 34), array(46, 10), array( 2, 64)),
  1448. array(array(17, 4), array(29, 14), array(49, 10), array(24, 46)),
  1449. array(array( 4, 18), array(13, 32), array(48, 14), array(42, 32)),
  1450. array(array(20, 4), array(40, 7), array(43, 22), array(10, 67)),
  1451. array(array(19, 6), array(18, 31), array(34, 34), array(20, 61)),//40
  1452. );
  1453. //length 长度表位
  1454. public static $lengthTableBits = array(array(10, 12, 14), array( 9, 11, 13), array( 8, 16, 16), array( 8, 10, 12));
  1455. //grade 识别等级
  1456. public static $grade = array('L'=> 0, 'M'=> 1, 'Q'=> 2,'H'=> 3);
  1457. //an 单一表
  1458. public static $anTable = array(
  1459. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1460. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1461. 36, -1, -1, -1, 37, 38, -1, -1, -1, -1, 39, 40, -1, 41, 42, 43,
  1462. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 44, -1, -1, -1, -1, -1,
  1463. -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
  1464. 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1,
  1465. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1466. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
  1467. //versionPattern 版本模式表
  1468. public static $versionPattern = array(
  1469. 0x07c94, 0x085bc, 0x09a99, 0x0a4d3, 0x0bbf6, 0x0c762, 0x0d847, 0x0e60d,
  1470. 0x0f928, 0x10b78, 0x1145d, 0x12a17, 0x13532, 0x149a6, 0x15683, 0x168c9,
  1471. 0x177ec, 0x18ec4, 0x191e1, 0x1afab, 0x1b08e, 0x1cc1a, 0x1d33f, 0x1ed75,
  1472. 0x1f250, 0x209d5, 0x216f0, 0x228ba, 0x2379f, 0x24b0b, 0x2542e, 0x26a64,
  1473. 0x27541, 0x28c69);
  1474. //Pattern 排列方式表
  1475. public static $alignmentPattern = array(
  1476. array( 0, 0),
  1477. array( 0, 0), array(18, 0), array(22, 0), array(26, 0), array(30, 0), // 1- 5
  1478. array(34, 0), array(22, 38), array(24, 42), array(26, 46), array(28, 50), // 6-10
  1479. array(30, 54), array(32, 58), array(34, 62), array(26, 46), array(26, 48), //11-15
  1480. array(26, 50), array(30, 54), array(30, 56), array(30, 58), array(34, 62), //16-20
  1481. array(28, 50), array(26, 50), array(30, 54), array(28, 54), array(32, 58), //21-25
  1482. array(30, 58), array(34, 62), array(26, 50), array(30, 54), array(26, 52), //26-30
  1483. array(30, 56), array(34, 60), array(30, 58), array(34, 62), array(30, 54), //31-35
  1484. array(24, 50), array(28, 54), array(32, 58), array(26, 54), array(30, 58) );//35-40
  1485. //capacity 容量
  1486. public static $capacity = array(
  1487. array( 0, 0, 0, array( 0, 0, 0, 0)),
  1488. array( 21, 26, 0, array( 7, 10, 13, 17)), // 1
  1489. array( 25, 44, 7, array( 10, 16, 22, 28)),
  1490. array( 29, 70, 7, array( 15, 26, 36, 44)),
  1491. array( 33, 100, 7, array( 20, 36, 52, 64)),
  1492. array( 37, 134, 7, array( 26, 48, 72, 88)), // 5
  1493. array( 41, 172, 7, array( 36, 64, 96, 112)),
  1494. array( 45, 196, 0, array( 40, 72, 108, 130)),
  1495. array( 49, 242, 0, array( 48, 88, 132, 156)),
  1496. array( 53, 292, 0, array( 60, 110, 160, 192)),
  1497. array( 57, 346, 0, array( 72, 130, 192, 224)), //10
  1498. array( 61, 404, 0, array( 80, 150, 224, 264)),
  1499. array( 65, 466, 0, array( 96, 176, 260, 308)),
  1500. array( 69, 532, 0, array( 104, 198, 288, 352)),
  1501. array( 73, 581, 3, array( 120, 216, 320, 384)),
  1502. array( 77, 655, 3, array( 132, 240, 360, 432)), //15
  1503. array( 81, 733, 3, array( 144, 280, 408, 480)),
  1504. array( 85, 815, 3, array( 168, 308, 448, 532)),
  1505. array( 89, 901, 3, array( 180, 338, 504, 588)),
  1506. array( 93, 991, 3, array( 196, 364, 546, 650)),
  1507. array( 97, 1085, 3, array( 224, 416, 600, 700)), //20
  1508. array(101, 1156, 4, array( 224, 442, 644, 750)),
  1509. array(105, 1258, 4, array( 252, 476, 690, 816)),
  1510. array(109, 1364, 4, array( 270, 504, 750, 900)),
  1511. array(113, 1474, 4, array( 300, 560, 810, 960)),
  1512. array(117, 1588, 4, array( 312, 588, 870, 1050)), //25
  1513. array(121, 1706, 4, array( 336, 644, 952, 1110)),
  1514. array(125, 1828, 4, array( 360, 700, 1020, 1200)),
  1515. array(129, 1921, 3, array( 390, 728, 1050, 1260)),
  1516. array(133, 2051, 3, array( 420, 784, 1140, 1350)),
  1517. array(137, 2185, 3, array( 450, 812, 1200, 1440)), //30
  1518. array(141, 2323, 3, array( 480, 868, 1290, 1530)),
  1519. array(145, 2465, 3, array( 510, 924, 1350, 1620)),
  1520. array(149, 2611, 3, array( 540, 980, 1440, 1710)),
  1521. array(153, 2761, 3, array( 570, 1036, 1530, 1800)),
  1522. array(157, 2876, 0, array( 570, 1064, 1590, 1890)), //35
  1523. array(161, 3034, 0, array( 600, 1120, 1680, 1980)),
  1524. array(165, 3196, 0, array( 630, 1204, 1770, 2100)),
  1525. array(169, 3362, 0, array( 660, 1260, 1860, 2220)),
  1526. array(173, 3532, 0, array( 720, 1316, 1950, 2310)),
  1527. array(177, 3706, 0, array( 750, 1372, 2040, 2430)) );//40
  1528. }//END class qrcode_table
  1529. /*
  1530. * QR Code Pattern 40种模式
  1531. * PHP QR Code is distributed under LGPL 3
  1532. * Copyright (C) 2010 Dominik Dzienia
  1533. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  1534. */
  1535. class qrcode_pattern{
  1536. public $frames = array();
  1537. //get 获得宽度 public
  1538. function getWidth($version){
  1539. return qrcode_table::$capacity[$version][0];
  1540. }//END getWidth
  1541. //new 新框架 public
  1542. function newFrame($version){
  1543. if($version < 1 || $version > 40) return null;
  1544. if(!isset($this->frames[$version])) $this->frames[$version] = $this->createFrame($version);
  1545. if(is_null($this->frames[$version])) return null;
  1546. return $this->frames[$version];
  1547. }//END newFrame
  1548. //create 创建框架
  1549. private function createFrame($version){
  1550. $width = qrcode_table::$capacity[$version][0];
  1551. $frameLine = str_repeat ("\0", $width);
  1552. $frame = array_fill(0, $width, $frameLine);
  1553. // Finder pattern
  1554. $this->putFinderPattern($frame, 0, 0);
  1555. $this->putFinderPattern($frame, $width - 7, 0);
  1556. $this->putFinderPattern($frame, 0, $width - 7);
  1557. // Separator
  1558. $yOffset = $width - 7;
  1559. for($y=0; $y<7; ++$y){
  1560. $frame[$y][7] = "\xc0";
  1561. $frame[$y][$width - 8] = "\xc0";
  1562. $frame[$yOffset][7] = "\xc0";
  1563. ++$yOffset;
  1564. }
  1565. $setPattern = str_repeat("\xc0", 8);
  1566. $this->set($frame, 0, 7, $setPattern);
  1567. $this->set($frame, $width-8, 7, $setPattern);
  1568. $this->set($frame, 0, $width - 8, $setPattern);
  1569. // Format info
  1570. $setPattern = str_repeat("\x84", 9);
  1571. $this->set($frame, 0, 8, $setPattern);
  1572. $this->set($frame, $width - 8, 8, $setPattern, 8);
  1573. $yOffset = $width - 8;
  1574. for($y=0; $y<8; ++$y,++$yOffset){
  1575. $frame[$y][8] = "\x84";
  1576. $frame[$yOffset][8] = "\x84";
  1577. }
  1578. // Timing pattern
  1579. for($i=1; $i<$width-15; ++$i){
  1580. $frame[6][7+$i] = chr(0x90 | ($i & 1));
  1581. $frame[7+$i][6] = chr(0x90 | ($i & 1));
  1582. }
  1583. // Alignment pattern
  1584. $this->putAlignmentPattern($version, $frame, $width);
  1585. // Version information
  1586. if($version >= 7){
  1587. $vinf = $this->getVersionPattern($version);
  1588. $v = $vinf;
  1589. for($x=0; $x<6; ++$x){
  1590. for($y=0; $y<3; ++$y){
  1591. $frame[($width - 11)+$y][$x] = chr(0x88 | ($v & 1));
  1592. $v = $v >> 1;
  1593. }
  1594. }
  1595. $v = $vinf;
  1596. for($y=0; $y<6; ++$y){
  1597. for($x=0; $x<3; ++$x){
  1598. $frame[$y][$x+($width - 11)] = chr(0x88 | ($v & 1));
  1599. $v = $v >> 1;
  1600. }
  1601. }
  1602. }
  1603. // and a little bit...
  1604. $frame[$width - 8][8] = "\x81";
  1605. return $frame;
  1606. }//END createFrame
  1607. //get 获得版本模式
  1608. private function getVersionPattern($version){
  1609. if($version < 7 || $version > 40){
  1610. return 0;
  1611. }else{
  1612. return qrcode_table::$versionPattern[$version -7];
  1613. }
  1614. } //END getVersionPattern
  1615. //put 搜索模式
  1616. private function putFinderPattern(&$frame, $ox, $oy){
  1617. $finder = array(
  1618. "\xc1\xc1\xc1\xc1\xc1\xc1\xc1",
  1619. "\xc1\xc0\xc0\xc0\xc0\xc0\xc1",
  1620. "\xc1\xc0\xc1\xc1\xc1\xc0\xc1",
  1621. "\xc1\xc0\xc1\xc1\xc1\xc0\xc1",
  1622. "\xc1\xc0\xc1\xc1\xc1\xc0\xc1",
  1623. "\xc1\xc0\xc0\xc0\xc0\xc0\xc1",
  1624. "\xc1\xc1\xc1\xc1\xc1\xc1\xc1"
  1625. );
  1626. for($y=0; $y<7; ++$y){
  1627. $this->set($frame, $ox, $oy+$y, $finder[$y]);
  1628. }
  1629. } //END putFinderPattern
  1630. //put 排列方式
  1631. private function putAlignmentPattern($version, &$frame, $width){
  1632. if($version < 2) return;
  1633. $d = qrcode_table::$alignmentPattern[$version][1] - qrcode_table::$alignmentPattern[$version][0];
  1634. if($d < 0){
  1635. $w = 2;
  1636. } else {
  1637. $w = (int)(($width - qrcode_table::$alignmentPattern[$version][0]) / $d + 2);
  1638. }
  1639. if($w * $w - 3 == 1){
  1640. $x = qrcode_table::$alignmentPattern[$version][0];
  1641. $y = qrcode_table::$alignmentPattern[$version][0];
  1642. $this->putAlignmentMarker($frame, $x, $y);
  1643. return;
  1644. }
  1645. $cx = qrcode_table::$alignmentPattern[$version][0];
  1646. for($x=1; $x<$w - 1; ++$x){
  1647. $this->putAlignmentMarker($frame, 6, $cx);
  1648. $this->putAlignmentMarker($frame, $cx, 6);
  1649. $cx += $d;
  1650. }
  1651. $cy = qrcode_table::$alignmentPattern[$version][0];
  1652. for($y=0; $y<$w-1; ++$y){
  1653. $cx = qrcode_table::$alignmentPattern[$version][0];
  1654. for($x=0; $x<$w-1; ++$x){
  1655. $this->putAlignmentMarker($frame, $cx, $cy);
  1656. $cx += $d;
  1657. }
  1658. $cy += $d;
  1659. }
  1660. } //END putAlignmentPattern
  1661. //put 对准标记
  1662. private function putAlignmentMarker(array &$frame, $ox, $oy){
  1663. $finder = array(
  1664. "\xa1\xa1\xa1\xa1\xa1",
  1665. "\xa1\xa0\xa0\xa0\xa1",
  1666. "\xa1\xa0\xa1\xa0\xa1",
  1667. "\xa1\xa0\xa0\xa0\xa1",
  1668. "\xa1\xa1\xa1\xa1\xa1"
  1669. );
  1670. $yStart = $oy-2;
  1671. $xStart = $ox-2;
  1672. for($y=0; $y<5; ++$y){
  1673. $this->set($frame, $xStart, $yStart+$y, $finder[$y]);
  1674. }
  1675. } //END putAlignmentMarker
  1676. //set 填入数据
  1677. private function set(&$srctab, $x, $y, $repl, $replLen = false){
  1678. $srctab[$y] = substr_replace($srctab[$y], ($replLen !== false)?substr($repl,0,$replLen):$repl, $x, ($replLen !== false)?$replLen:strlen($repl));
  1679. } //END set
  1680. }//END class qrcode_pattern