<?php
include "phpqrcode.php";
$value = "http://huizhongda.taobao.com/";
$errorCorrectionLevel = 'L';
$matrixPointSize = ''; echo QRcode::png($value, false, $errorCorrectionLevel, $matrixPointSize); /*and: QRcode::png($c, 'png/'.$t.'.png');
echo '<img src="png/'.$t.'.png" /><br />'.$c; */
exit;
?>

上面代码另存为index.php

<?php

/*
* PHP QR Code encoder
*
* This file contains MERGED version of PHP QR Code library.
* It was auto-generated from full version for your convenience.
*
* This merged version was configured to not requre any external files,
* with disabled cache, error loging and weker but faster mask matching.
* If you need tune it up please use non-merged version.
*
* For full version, documentation, examples of use please visit:
*
* http://phpqrcode.sourceforge.net/
* https://sourceforge.net/projects/phpqrcode/
*
* PHP QR Code is distributed under LGPL 3
* Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/ /*
* Version: 1.1.4
* Build: 2010100721
*/ //---- qrconst.php ----------------------------- /*
* PHP QR Code encoder
*
* Common constants
*
* Based on libqrencode C library distributed under LGPL 2.1
* Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi <fukuchi@megaui.net>
*
* PHP QR Code is distributed under LGPL 3
* Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/ // Encoding modes define('QR_MODE_NUL', -);
define('QR_MODE_NUM', );
define('QR_MODE_AN', );
define('QR_MODE_8', );
define('QR_MODE_KANJI', );
define('QR_MODE_STRUCTURE', ); // Levels of error correction. define('QR_ECLEVEL_L', );
define('QR_ECLEVEL_M', );
define('QR_ECLEVEL_Q', );
define('QR_ECLEVEL_H', ); // Supported output formats define('QR_FORMAT_TEXT', );
define('QR_FORMAT_PNG', ); class qrstr {
public static function set(&$srctab, $x, $y, $repl, $replLen = false) {
$srctab[$y] = substr_replace($srctab[$y], ($replLen !== false)?substr($repl,,$replLen):$repl, $x, ($replLen !== false)?$replLen:strlen($repl));
}
} //---- merged_config.php ----------------------------- /*
* PHP QR Code encoder
*
* Config file, tuned-up for merged verion
*/ define('QR_CACHEABLE', false); // use cache - more disk reads but less CPU power, masks and format templates are stored there
define('QR_CACHE_DIR', false); // used when QR_CACHEABLE === true
define('QR_LOG_DIR', false); // default error logs dir define('QR_FIND_BEST_MASK', true); // if true, estimates best mask (spec. default, but extremally slow; set to false to significant performance boost but (propably) worst quality code
define('QR_FIND_FROM_RANDOM', ); // if false, checks all masks available, otherwise value tells count of masks need to be checked, mask id are got randomly
define('QR_DEFAULT_MASK', ); // when QR_FIND_BEST_MASK === false define('QR_PNG_MAXIMUM_SIZE', ); // maximum allowed png image width (in pixels), tune to make sure GD and PHP can handle such big images //---- qrtools.php ----------------------------- /*
* PHP QR Code encoder
*
* Toolset, handy and debug utilites.
*
* PHP QR Code is distributed under LGPL 3
* Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/ class QRtools { //----------------------------------------------------------------------
public static function binarize($frame)
{
$len = count($frame);
foreach ($frame as &$frameLine) { for($i=; $i<$len; $i++) {
$frameLine[$i] = (ord($frameLine[$i])&)?'':'';
}
} return $frame;
} //----------------------------------------------------------------------
public static function tcpdfBarcodeArray($code, $mode = 'QR,L', $tcPdfVersion = '4.5.037')
{
$barcode_array = array(); if (!is_array($mode))
$mode = explode(',', $mode); $eccLevel = 'L'; if (count($mode) > ) {
$eccLevel = $mode[];
} $qrTab = QRcode::text($code, false, $eccLevel);
$size = count($qrTab); $barcode_array['num_rows'] = $size;
$barcode_array['num_cols'] = $size;
$barcode_array['bcode'] = array(); foreach ($qrTab as $line) {
$arrAdd = array();
foreach(str_split($line) as $char)
$arrAdd[] = ($char=='')?:;
$barcode_array['bcode'][] = $arrAdd;
} return $barcode_array;
} //----------------------------------------------------------------------
public static function clearCache()
{
self::$frames = array();
} //----------------------------------------------------------------------
public static function buildCache()
{
QRtools::markTime('before_build_cache'); $mask = new QRmask();
for ($a=; $a <= QRSPEC_VERSION_MAX; $a++) {
$frame = QRspec::newFrame($a);
if (QR_IMAGE) {
$fileName = QR_CACHE_DIR.'frame_'.$a.'.png';
QRimage::png(self::binarize($frame), $fileName, , );
} $width = count($frame);
$bitMask = array_fill(, $width, array_fill(, $width, ));
for ($maskNo=; $maskNo<; $maskNo++)
$mask->makeMaskNo($maskNo, $width, $frame, $bitMask, true);
} QRtools::markTime('after_build_cache');
} //----------------------------------------------------------------------
public static function log($outfile, $err)
{
if (QR_LOG_DIR !== false) {
if ($err != '') {
if ($outfile !== false) {
file_put_contents(QR_LOG_DIR.basename($outfile).'-errors.txt', date('Y-m-d H:i:s').': '.$err, FILE_APPEND);
} else {
file_put_contents(QR_LOG_DIR.'errors.txt', date('Y-m-d H:i:s').': '.$err, FILE_APPEND);
}
}
}
} //----------------------------------------------------------------------
public static function dumpMask($frame)
{
$width = count($frame);
for($y=;$y<$width;$y++) {
for($x=;$x<$width;$x++) {
echo ord($frame[$y][$x]).',';
}
}
} //----------------------------------------------------------------------
public static function markTime($markerId)
{
list($usec, $sec) = explode(" ", microtime());
$time = ((float)$usec + (float)$sec); if (!isset($GLOBALS['qr_time_bench']))
$GLOBALS['qr_time_bench'] = array(); $GLOBALS['qr_time_bench'][$markerId] = $time;
} //----------------------------------------------------------------------
public static function timeBenchmark()
{
self::markTime('finish'); $lastTime = ;
$startTime = ;
$p = ; echo '<table cellpadding="3" cellspacing="1">
<thead><tr style="border-bottom:1px solid silver"><td colspan="" style="text-align:center">BENCHMARK</td></tr></thead>
<tbody>'; foreach($GLOBALS['qr_time_bench'] as $markerId=>$thisTime) {
if ($p > ) {
echo '<tr><th style="text-align:right">till '.$markerId.': </th><td>'.number_format($thisTime-$lastTime, ).'s</td></tr>';
} else {
$startTime = $thisTime;
} $p++;
$lastTime = $thisTime;
} echo '</tbody><tfoot>
<tr style="border-top:2px solid black"><th style="text-align:right">TOTAL: </th><td>'.number_format($lastTime-$startTime, 6).'s</td></tr>
</tfoot>
</table>';
} } //########################################################################## QRtools::markTime('start'); //---- qrspec.php ----------------------------- /*
* PHP QR Code encoder
*
* QR Code specifications
*
* Based on libqrencode C library distributed under LGPL 2.1
* Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi <fukuchi@megaui.net>
*
* PHP QR Code is distributed under LGPL 3
* Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
*
* The following data / specifications are taken from
* "Two dimensional symbol -- QR-code -- Basic Specification" (JIS X0510:2004)
* or
* "Automatic identification and data capture techniques --
* QR Code 2005 bar code symbology specification" (ISO/IEC 18004:2006)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/ define('QRSPEC_VERSION_MAX', );
define('QRSPEC_WIDTH_MAX', ); define('QRCAP_WIDTH', );
define('QRCAP_WORDS', );
define('QRCAP_REMINDER', );
define('QRCAP_EC', ); class QRspec { public static $capacity = array(
array( , , , array( , , , )),
array( , , , array( , , , )), // 1
array( , , , array( , , , )),
array( , , , array( , , , )),
array( , , , array( , , , )),
array( , , , array( , , , )), // 5
array( , , , array( , , , )),
array( , , , array( , , , )),
array( , , , array( , , , )),
array( , , , array( , , , )),
array( , , , array( , , , )), //10
array( , , , array( , , , )),
array( , , , array( , , , )),
array( , , , array( , , , )),
array( , , , array( , , , )),
array( , , , array( , , , )), //15
array( , , , array( , , , )),
array( , , , array( , , , )),
array( , , , array( , , , )),
array( , , , array( , , , )),
array( , , , array( , , , )), //20
array(, , , array( , , , )),
array(, , , array( , , , )),
array(, , , array( , , , )),
array(, , , array( , , , )),
array(, , , array( , , , )), //25
array(, , , array( , , , )),
array(, , , array( , , , )),
array(, , , array( , , , )),
array(, , , array( , , , )),
array(, , , array( , , , )), //30
array(, , , array( , , , )),
array(, , , array( , , , )),
array(, , , array( , , , )),
array(, , , array( , , , )),
array(, , , array( , , , )), //35
array(, , , array( , , , )),
array(, , , array( , , , )),
array(, , , array( , , , )),
array(, , , array( , , , )),
array(, , , array( , , , )) //40
); //----------------------------------------------------------------------
public static function getDataLength($version, $level)
{
return self::$capacity[$version][QRCAP_WORDS] - self::$capacity[$version][QRCAP_EC][$level];
} //----------------------------------------------------------------------
public static function getECCLength($version, $level)
{
return self::$capacity[$version][QRCAP_EC][$level];
} //----------------------------------------------------------------------
public static function getWidth($version)
{
return self::$capacity[$version][QRCAP_WIDTH];
} //----------------------------------------------------------------------
public static function getRemainder($version)
{
return self::$capacity[$version][QRCAP_REMINDER];
} //----------------------------------------------------------------------
public static function getMinimumVersion($size, $level)
{ for($i=; $i<= QRSPEC_VERSION_MAX; $i++) {
$words = self::$capacity[$i][QRCAP_WORDS] - self::$capacity[$i][QRCAP_EC][$level];
if($words >= $size)
return $i;
} return -;
} //###################################################################### public static $lengthTableBits = array(
array(, , ),
array( , , ),
array( , , ),
array( , , )
); //----------------------------------------------------------------------
public static function lengthIndicator($mode, $version)
{
if ($mode == QR_MODE_STRUCTURE)
return ; if ($version <= ) {
$l = ;
} else if ($version <= ) {
$l = ;
} else {
$l = ;
} return self::$lengthTableBits[$mode][$l];
} //----------------------------------------------------------------------
public static function maximumWords($mode, $version)
{
if($mode == QR_MODE_STRUCTURE)
return ; if($version <= ) {
$l = ;
} else if($version <= ) {
$l = ;
} else {
$l = ;
} $bits = self::$lengthTableBits[$mode][$l];
$words = ( << $bits) - ; if($mode == QR_MODE_KANJI) {
$words *= ; // the number of bytes is required
} return $words;
} // Error correction code -----------------------------------------------
// Table of the error correction code (Reed-Solomon block)
// See Table 12-16 (pp.30-36), JIS X0510:2004. public static $eccTable = array(
array(array( , ), array( , ), array( , ), array( , )),
array(array( , ), array( , ), array( , ), array( , )), // 1
array(array( , ), array( , ), array( , ), array( , )),
array(array( , ), array( , ), array( , ), array( , )),
array(array( , ), array( , ), array( , ), array( , )),
array(array( , ), array( , ), array( , ), array( , )), // 5
array(array( , ), array( , ), array( , ), array( , )),
array(array( , ), array( , ), array( , ), array( , )),
array(array( , ), array( , ), array( , ), array( , )),
array(array( , ), array( , ), array( , ), array( , )),
array(array( , ), array( , ), array( , ), array( , )), //10
array(array( , ), array( , ), array( , ), array( , )),
array(array( , ), array( , ), array( , ), array( , )),
array(array( , ), array( , ), array( , ), array(, )),
array(array( , ), array( , ), array(, ), array(, )),
array(array( , ), array( , ), array( , ), array(, )), //15
array(array( , ), array( , ), array(, ), array( , )),
array(array( , ), array(, ), array( , ), array( , )),
array(array( , ), array( , ), array(, ), array( , )),
array(array( , ), array( , ), array(, ), array( , )),
array(array( , ), array( , ), array(, ), array(, )), //20
array(array( , ), array(, ), array(, ), array(, )),
array(array( , ), array(, ), array( , ), array(, )),
array(array( , ), array( , ), array(, ), array(, )),
array(array( , ), array( , ), array(, ), array(, )),
array(array( , ), array( , ), array( , ), array(, )), //25
array(array(, ), array(, ), array(, ), array(, )),
array(array( , ), array(, ), array( , ), array(, )),
array(array( , ), array( , ), array( , ), array(, )),
array(array( , ), array(, ), array( , ), array(, )),
array(array( , ), array(, ), array(, ), array(, )), //30
array(array(, ), array( , ), array(, ), array(, )),
array(array(, ), array(, ), array(, ), array(, )),
array(array(, ), array(, ), array(, ), array(, )),
array(array(, ), array(, ), array(, ), array(, )),
array(array(, ), array(, ), array(, ), array(, )), //35
array(array( , ), array( , ), array(, ), array( , )),
array(array(, ), array(, ), array(, ), array(, )),
array(array( , ), array(, ), array(, ), array(, )),
array(array(, ), array(, ), array(, ), array(, )),
array(array(, ), array(, ), array(, ), array(, )),//40
); //----------------------------------------------------------------------
// CACHEABLE!!! public static function getEccSpec($version, $level, array &$spec)
{
if (count($spec) < ) {
$spec = array(,,,,);
} $b1 = self::$eccTable[$version][$level][];
$b2 = self::$eccTable[$version][$level][];
$data = self::getDataLength($version, $level);
$ecc = self::getECCLength($version, $level); if($b2 == ) {
$spec[] = $b1;
$spec[] = (int)($data / $b1);
$spec[] = (int)($ecc / $b1);
$spec[] = ;
$spec[] = ;
} else {
$spec[] = $b1;
$spec[] = (int)($data / ($b1 + $b2));
$spec[] = (int)($ecc / ($b1 + $b2));
$spec[] = $b2;
$spec[] = $spec[] + ;
}
} // Alignment pattern --------------------------------------------------- // Positions of alignment patterns.
// This array includes only the second and the third position of the
// alignment patterns. Rest of them can be calculated from the distance
// between them. // See Table 1 in Appendix E (pp.71) of JIS X0510:2004. public static $alignmentPattern = array(
array( , ),
array( , ), array(, ), array(, ), array(, ), array(, ), // 1- 5
array(, ), array(, ), array(, ), array(, ), array(, ), // 6-10
array(, ), array(, ), array(, ), array(, ), array(, ), //11-15
array(, ), array(, ), array(, ), array(, ), array(, ), //16-20
array(, ), array(, ), array(, ), array(, ), array(, ), //21-25
array(, ), array(, ), array(, ), array(, ), array(, ), //26-30
array(, ), array(, ), array(, ), array(, ), array(, ), //31-35
array(, ), array(, ), array(, ), array(, ), array(, ), //35-40
); /** --------------------------------------------------------------------
* Put an alignment marker.
* @param frame
* @param width
* @param ox,oy center coordinate of the pattern
*/
public static function putAlignmentMarker(array &$frame, $ox, $oy)
{
$finder = array(
"\xa1\xa1\xa1\xa1\xa1",
"\xa1\xa0\xa0\xa0\xa1",
"\xa1\xa0\xa1\xa0\xa1",
"\xa1\xa0\xa0\xa0\xa1",
"\xa1\xa1\xa1\xa1\xa1"
); $yStart = $oy-;
$xStart = $ox-; for($y=; $y<; $y++) {
QRstr::set($frame, $xStart, $yStart+$y, $finder[$y]);
}
} //----------------------------------------------------------------------
public static function putAlignmentPattern($version, &$frame, $width)
{
if($version < )
return; $d = self::$alignmentPattern[$version][] - self::$alignmentPattern[$version][];
if($d < ) {
$w = ;
} else {
$w = (int)(($width - self::$alignmentPattern[$version][]) / $d + );
} if($w * $w - == ) {
$x = self::$alignmentPattern[$version][];
$y = self::$alignmentPattern[$version][];
self::putAlignmentMarker($frame, $x, $y);
return;
} $cx = self::$alignmentPattern[$version][];
for($x=; $x<$w - ; $x++) {
self::putAlignmentMarker($frame, , $cx);
self::putAlignmentMarker($frame, $cx, );
$cx += $d;
} $cy = self::$alignmentPattern[$version][];
for($y=; $y<$w-; $y++) {
$cx = self::$alignmentPattern[$version][];
for($x=; $x<$w-; $x++) {
self::putAlignmentMarker($frame, $cx, $cy);
$cx += $d;
}
$cy += $d;
}
} // Version information pattern ----------------------------------------- // Version information pattern (BCH coded).
// See Table 1 in Appendix D (pp.68) of JIS X0510:2004. // size: [QRSPEC_VERSION_MAX - 6] public static $versionPattern = array(
0x07c94, 0x085bc, 0x09a99, 0x0a4d3, 0x0bbf6, 0x0c762, 0x0d847, 0x0e60d,
0x0f928, 0x10b78, 0x1145d, 0x12a17, 0x13532, 0x149a6, 0x15683, 0x168c9,
0x177ec, 0x18ec4, 0x191e1, 0x1afab, 0x1b08e, 0x1cc1a, 0x1d33f, 0x1ed75,
0x1f250, 0x209d5, 0x216f0, 0x228ba, 0x2379f, 0x24b0b, 0x2542e, 0x26a64,
0x27541, 0x28c69
); //----------------------------------------------------------------------
public static function getVersionPattern($version)
{
if($version < || $version > QRSPEC_VERSION_MAX)
return ; return self::$versionPattern[$version -];
} // Format information --------------------------------------------------
// See calcFormatInfo in tests/test_qrspec.c (orginal qrencode c lib) public static $formatInfo = array(
array(0x77c4, 0x72f3, 0x7daa, 0x789d, 0x662f, 0x6318, 0x6c41, 0x6976),
array(0x5412, 0x5125, 0x5e7c, 0x5b4b, 0x45f9, 0x40ce, 0x4f97, 0x4aa0),
array(0x355f, 0x3068, 0x3f31, 0x3a06, 0x24b4, 0x2183, 0x2eda, 0x2bed),
array(0x1689, 0x13be, 0x1ce7, 0x19d0, 0x0762, 0x0255, 0x0d0c, 0x083b)
); public static function getFormatInfo($mask, $level)
{
if($mask < || $mask > )
return ; if($level < || $level > )
return ; return self::$formatInfo[$level][$mask];
} // Frame ---------------------------------------------------------------
// Cache of initial frames. public static $frames = array(); /** --------------------------------------------------------------------
* Put a finder pattern.
* @param frame
* @param width
* @param ox,oy upper-left coordinate of the pattern
*/
public static function putFinderPattern(&$frame, $ox, $oy)
{
$finder = array(
"\xc1\xc1\xc1\xc1\xc1\xc1\xc1",
"\xc1\xc0\xc0\xc0\xc0\xc0\xc1",
"\xc1\xc0\xc1\xc1\xc1\xc0\xc1",
"\xc1\xc0\xc1\xc1\xc1\xc0\xc1",
"\xc1\xc0\xc1\xc1\xc1\xc0\xc1",
"\xc1\xc0\xc0\xc0\xc0\xc0\xc1",
"\xc1\xc1\xc1\xc1\xc1\xc1\xc1"
); for($y=; $y<; $y++) {
QRstr::set($frame, $ox, $oy+$y, $finder[$y]);
}
} //----------------------------------------------------------------------
public static function createFrame($version)
{
$width = self::$capacity[$version][QRCAP_WIDTH];
$frameLine = str_repeat ("\0", $width);
$frame = array_fill(, $width, $frameLine); // Finder pattern
self::putFinderPattern($frame, , );
self::putFinderPattern($frame, $width - , );
self::putFinderPattern($frame, , $width - ); // Separator
$yOffset = $width - ; for($y=; $y<; $y++) {
$frame[$y][] = "\xc0";
$frame[$y][$width - ] = "\xc0";
$frame[$yOffset][] = "\xc0";
$yOffset++;
} $setPattern = str_repeat("\xc0", ); QRstr::set($frame, , , $setPattern);
QRstr::set($frame, $width-, , $setPattern);
QRstr::set($frame, , $width - , $setPattern); // Format info
$setPattern = str_repeat("\x84", );
QRstr::set($frame, , , $setPattern);
QRstr::set($frame, $width - , , $setPattern, ); $yOffset = $width - ; for($y=; $y<; $y++,$yOffset++) {
$frame[$y][] = "\x84";
$frame[$yOffset][] = "\x84";
} // Timing pattern for($i=; $i<$width-; $i++) {
$frame[][+$i] = chr(0x90 | ($i & ));
$frame[+$i][] = chr(0x90 | ($i & ));
} // Alignment pattern
self::putAlignmentPattern($version, $frame, $width); // Version information
if($version >= ) {
$vinf = self::getVersionPattern($version); $v = $vinf; for($x=; $x<; $x++) {
for($y=; $y<; $y++) {
$frame[($width - )+$y][$x] = chr(0x88 | ($v & ));
$v = $v >> ;
}
} $v = $vinf;
for($y=; $y<; $y++) {
for($x=; $x<; $x++) {
$frame[$y][$x+($width - )] = chr(0x88 | ($v & ));
$v = $v >> ;
}
}
} // and a little bit...
$frame[$width - ][] = "\x81"; return $frame;
} //----------------------------------------------------------------------
public static function debug($frame, $binary_mode = false)
{
if ($binary_mode) { foreach ($frame as &$frameLine) {
$frameLine = join('<span class="m">&nbsp;&nbsp;</span>', explode('', $frameLine));
$frameLine = join('██', explode('', $frameLine));
} ?>
<style>
.m { background-color: white; }
</style>
<?php
echo '<pre><tt><br/ ><br/ ><br/ >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
echo join("<br/ >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", $frame);
echo '</tt></pre><br/ ><br/ ><br/ ><br/ ><br/ ><br/ >'; } else { foreach ($frame as &$frameLine) {
$frameLine = join('<span class="m">&nbsp;</span>', explode("\xc0", $frameLine));
$frameLine = join('<span class="m">▒</span>', explode("\xc1", $frameLine));
$frameLine = join('<span class="p">&nbsp;</span>', explode("\xa0", $frameLine));
$frameLine = join('<span class="p">▒</span>', explode("\xa1", $frameLine));
$frameLine = join('<span class="s">◇</span>', explode("\x84", $frameLine)); //format 0
$frameLine = join('<span class="s">◆</span>', explode("\x85", $frameLine)); //format 1
$frameLine = join('<span class="x">☢</span>', explode("\x81", $frameLine)); //special bit
$frameLine = join('<span class="c">&nbsp;</span>', explode("\x90", $frameLine)); //clock 0
$frameLine = join('<span class="c">◷</span>', explode("\x91", $frameLine)); //clock 1
$frameLine = join('<span class="f">&nbsp;</span>', explode("\x88", $frameLine)); //version
$frameLine = join('<span class="f">▒</span>', explode("\x89", $frameLine)); //version
$frameLine = join('♦', explode("\x01", $frameLine));
$frameLine = join('⋅', explode("\0", $frameLine));
} ?>
<style>
.p { background-color: yellow; }
.m { background-color: #00FF00; }
.s { background-color: #FF0000; }
.c { background-color: aqua; }
.x { background-color: pink; }
.f { background-color: gold; }
</style>
<?php
echo "<pre><tt>";
echo join("<br/ >", $frame);
echo "</tt></pre>"; }
} //----------------------------------------------------------------------
public static function serial($frame)
{
return gzcompress(join("\n", $frame), );
} //----------------------------------------------------------------------
public static function unserial($code)
{
return explode("\n", gzuncompress($code));
} //----------------------------------------------------------------------
public static function newFrame($version)
{
if($version < || $version > QRSPEC_VERSION_MAX)
return null; if(!isset(self::$frames[$version])) { $fileName = QR_CACHE_DIR.'frame_'.$version.'.dat'; if (QR_CACHEABLE) {
if (file_exists($fileName)) {
self::$frames[$version] = self::unserial(file_get_contents($fileName));
} else {
self::$frames[$version] = self::createFrame($version);
file_put_contents($fileName, self::serial(self::$frames[$version]));
}
} else {
self::$frames[$version] = self::createFrame($version);
}
} if(is_null(self::$frames[$version]))
return null; return self::$frames[$version];
} //----------------------------------------------------------------------
public static function rsBlockNum($spec) { return $spec[] + $spec[]; }
public static function rsBlockNum1($spec) { return $spec[]; }
public static function rsDataCodes1($spec) { return $spec[]; }
public static function rsEccCodes1($spec) { return $spec[]; }
public static function rsBlockNum2($spec) { return $spec[]; }
public static function rsDataCodes2($spec) { return $spec[]; }
public static function rsEccCodes2($spec) { return $spec[]; }
public static function rsDataLength($spec) { return ($spec[] * $spec[]) + ($spec[] * $spec[]); }
public static function rsEccLength($spec) { return ($spec[] + $spec[]) * $spec[]; } } //---- qrimage.php ----------------------------- /*
* PHP QR Code encoder
*
* Image output of code using GD2
*
* PHP QR Code is distributed under LGPL 3
* Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/ define('QR_IMAGE', true); class QRimage { //----------------------------------------------------------------------
public static function png($frame, $filename = false, $pixelPerPoint = , $outerFrame = ,$saveandprint=FALSE)
{
$image = self::image($frame, $pixelPerPoint, $outerFrame); if ($filename === false) {
Header("Content-type: image/png");
ImagePng($image);
} else {
if($saveandprint===TRUE){
ImagePng($image, $filename);
header("Content-type: image/png");
ImagePng($image);
}else{
ImagePng($image, $filename);
}
} ImageDestroy($image);
} //----------------------------------------------------------------------
public static function jpg($frame, $filename = false, $pixelPerPoint = , $outerFrame = , $q = )
{
$image = self::image($frame, $pixelPerPoint, $outerFrame); if ($filename === false) {
Header("Content-type: image/jpeg");
ImageJpeg($image, null, $q);
} else {
ImageJpeg($image, $filename, $q);
} ImageDestroy($image);
} //----------------------------------------------------------------------
private static function image($frame, $pixelPerPoint = , $outerFrame = )
{
$h = count($frame);
$w = strlen($frame[]); $imgW = $w + *$outerFrame;
$imgH = $h + *$outerFrame; $base_image =ImageCreate($imgW, $imgH); $col[] = ImageColorAllocate($base_image,,,);
$col[] = ImageColorAllocate($base_image,,,); imagefill($base_image, , , $col[]); for($y=; $y<$h; $y++) {
for($x=; $x<$w; $x++) {
if ($frame[$y][$x] == '') {
ImageSetPixel($base_image,$x+$outerFrame,$y+$outerFrame,$col[]);
}
}
} $target_image =ImageCreate($imgW * $pixelPerPoint, $imgH * $pixelPerPoint);
ImageCopyResized($target_image, $base_image, , , , , $imgW * $pixelPerPoint, $imgH * $pixelPerPoint, $imgW, $imgH);
ImageDestroy($base_image); return $target_image;
}
} //---- qrinput.php ----------------------------- /*
* PHP QR Code encoder
*
* Input encoding class
*
* Based on libqrencode C library distributed under LGPL 2.1
* Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi <fukuchi@megaui.net>
*
* PHP QR Code is distributed under LGPL 3
* Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/ define('STRUCTURE_HEADER_BITS', );
define('MAX_STRUCTURED_SYMBOLS', ); class QRinputItem { public $mode;
public $size;
public $data;
public $bstream; public function __construct($mode, $size, $data, $bstream = null)
{
$setData = array_slice($data, , $size); if (count($setData) < $size) {
$setData = array_merge($setData, array_fill(,$size-count($setData),));
} if(!QRinput::check($mode, $size, $setData)) {
throw new Exception('Error m:'.$mode.',s:'.$size.',d:'.join(',',$setData));
return null;
} $this->mode = $mode;
$this->size = $size;
$this->data = $setData;
$this->bstream = $bstream;
} //----------------------------------------------------------------------
public function encodeModeNum($version)
{
try { $words = (int)($this->size / );
$bs = new QRbitstream(); $val = 0x1;
$bs->appendNum(, $val);
$bs->appendNum(QRspec::lengthIndicator(QR_MODE_NUM, $version), $this->size); for($i=; $i<$words; $i++) {
$val = (ord($this->data[$i* ]) - ord('')) * ;
$val += (ord($this->data[$i*+]) - ord('')) * ;
$val += (ord($this->data[$i*+]) - ord(''));
$bs->appendNum(, $val);
} if($this->size - $words * == ) {
$val = ord($this->data[$words*]) - ord('');
$bs->appendNum(, $val);
} else if($this->size - $words * == ) {
$val = (ord($this->data[$words* ]) - ord('')) * ;
$val += (ord($this->data[$words*+]) - ord(''));
$bs->appendNum(, $val);
} $this->bstream = $bs;
return ; } catch (Exception $e) {
return -;
}
} //----------------------------------------------------------------------
public function encodeModeAn($version)
{
try {
$words = (int)($this->size / );
$bs = new QRbitstream(); $bs->appendNum(, 0x02);
$bs->appendNum(QRspec::lengthIndicator(QR_MODE_AN, $version), $this->size); for($i=; $i<$words; $i++) {
$val = (int)QRinput::lookAnTable(ord($this->data[$i* ])) * ;
$val += (int)QRinput::lookAnTable(ord($this->data[$i*+])); $bs->appendNum(, $val);
} if($this->size & ) {
$val = QRinput::lookAnTable(ord($this->data[$words * ]));
$bs->appendNum(, $val);
} $this->bstream = $bs;
return ; } catch (Exception $e) {
return -;
}
} //----------------------------------------------------------------------
public function encodeMode8($version)
{
try {
$bs = new QRbitstream(); $bs->appendNum(, 0x4);
$bs->appendNum(QRspec::lengthIndicator(QR_MODE_8, $version), $this->size); for($i=; $i<$this->size; $i++) {
$bs->appendNum(, ord($this->data[$i]));
} $this->bstream = $bs;
return ; } catch (Exception $e) {
return -;
}
} //----------------------------------------------------------------------
public function encodeModeKanji($version)
{
try { $bs = new QRbitrtream(); $bs->appendNum(, 0x8);
$bs->appendNum(QRspec::lengthIndicator(QR_MODE_KANJI, $version), (int)($this->size / )); for($i=; $i<$this->size; $i+=) {
$val = (ord($this->data[$i]) << ) | ord($this->data[$i+]);
if($val <= 0x9ffc) {
$val -= 0x8140;
} else {
$val -= 0xc140;
} $h = ($val >> ) * 0xc0;
$val = ($val & 0xff) + $h; $bs->appendNum(, $val);
} $this->bstream = $bs;
return ; } catch (Exception $e) {
return -;
}
} //----------------------------------------------------------------------
public function encodeModeStructure()
{
try {
$bs = new QRbitstream(); $bs->appendNum(, 0x03);
$bs->appendNum(, ord($this->data[]) - );
$bs->appendNum(, ord($this->data[]) - );
$bs->appendNum(, ord($this->data[])); $this->bstream = $bs;
return ; } catch (Exception $e) {
return -;
}
} //----------------------------------------------------------------------
public function estimateBitStreamSizeOfEntry($version)
{
$bits = ; if($version == )
$version = ; switch($this->mode) {
case QR_MODE_NUM: $bits = QRinput::estimateBitsModeNum($this->size); break;
case QR_MODE_AN: $bits = QRinput::estimateBitsModeAn($this->size); break;
case QR_MODE_8: $bits = QRinput::estimateBitsMode8($this->size); break;
case QR_MODE_KANJI: $bits = QRinput::estimateBitsModeKanji($this->size);break;
case QR_MODE_STRUCTURE: return STRUCTURE_HEADER_BITS;
default:
return ;
} $l = QRspec::lengthIndicator($this->mode, $version);
$m = << $l;
$num = (int)(($this->size + $m - ) / $m); $bits += $num * ( + $l); return $bits;
} //----------------------------------------------------------------------
public function encodeBitStream($version)
{
try { unset($this->bstream);
$words = QRspec::maximumWords($this->mode, $version); if($this->size > $words) { $st1 = new QRinputItem($this->mode, $words, $this->data);
$st2 = new QRinputItem($this->mode, $this->size - $words, array_slice($this->data, $words)); $st1->encodeBitStream($version);
$st2->encodeBitStream($version); $this->bstream = new QRbitstream();
$this->bstream->append($st1->bstream);
$this->bstream->append($st2->bstream); unset($st1);
unset($st2); } else { $ret = ; switch($this->mode) {
case QR_MODE_NUM: $ret = $this->encodeModeNum($version); break;
case QR_MODE_AN: $ret = $this->encodeModeAn($version); break;
case QR_MODE_8: $ret = $this->encodeMode8($version); break;
case QR_MODE_KANJI: $ret = $this->encodeModeKanji($version);break;
case QR_MODE_STRUCTURE: $ret = $this->encodeModeStructure(); break; default:
break;
} if($ret < )
return -;
} return $this->bstream->size(); } catch (Exception $e) {
return -;
}
}
}; //########################################################################## class QRinput { public $items; private $version;
private $level; //----------------------------------------------------------------------
public function __construct($version = , $level = QR_ECLEVEL_L)
{
if ($version < || $version > QRSPEC_VERSION_MAX || $level > QR_ECLEVEL_H) {
throw new Exception('Invalid version no');
return NULL;
} $this->version = $version;
$this->level = $level;
} //----------------------------------------------------------------------
public function getVersion()
{
return $this->version;
} //----------------------------------------------------------------------
public function setVersion($version)
{
if($version < || $version > QRSPEC_VERSION_MAX) {
throw new Exception('Invalid version no');
return -;
} $this->version = $version; return ;
} //----------------------------------------------------------------------
public function getErrorCorrectionLevel()
{
return $this->level;
} //----------------------------------------------------------------------
public function setErrorCorrectionLevel($level)
{
if($level > QR_ECLEVEL_H) {
throw new Exception('Invalid ECLEVEL');
return -;
} $this->level = $level; return ;
} //----------------------------------------------------------------------
public function appendEntry(QRinputItem $entry)
{
$this->items[] = $entry;
} //----------------------------------------------------------------------
public function append($mode, $size, $data)
{
try {
$entry = new QRinputItem($mode, $size, $data);
$this->items[] = $entry;
return ;
} catch (Exception $e) {
return -;
}
} //---------------------------------------------------------------------- public function insertStructuredAppendHeader($size, $index, $parity)
{
if( $size > MAX_STRUCTURED_SYMBOLS ) {
throw new Exception('insertStructuredAppendHeader wrong size');
} if( $index <= || $index > MAX_STRUCTURED_SYMBOLS ) {
throw new Exception('insertStructuredAppendHeader wrong index');
} $buf = array($size, $index, $parity); try {
$entry = new QRinputItem(QR_MODE_STRUCTURE, , buf);
array_unshift($this->items, $entry);
return ;
} catch (Exception $e) {
return -;
}
} //----------------------------------------------------------------------
public function calcParity()
{
$parity = ; foreach($this->items as $item) {
if($item->mode != QR_MODE_STRUCTURE) {
for($i=$item->size-; $i>=; $i--) {
$parity ^= $item->data[$i];
}
}
} return $parity;
} //----------------------------------------------------------------------
public static function checkModeNum($size, $data)
{
for($i=; $i<$size; $i++) {
if((ord($data[$i]) < ord('')) || (ord($data[$i]) > ord(''))){
return false;
}
} return true;
} //----------------------------------------------------------------------
public static function estimateBitsModeNum($size)
{
$w = (int)$size / ;
$bits = $w * ; switch($size - $w * ) {
case :
$bits += ;
break;
case :
$bits += ;
break;
default:
break;
} return $bits;
} //----------------------------------------------------------------------
public static $anTable = array(
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
, -, -, -, , , -, -, -, -, , , -, , , ,
, , , , , , , , , , , -, -, -, -, -,
-, , , , , , , , , , , , , , , ,
, , , , , , , , , , , -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -
); //----------------------------------------------------------------------
public static function lookAnTable($c)
{
return (($c > )?-:self::$anTable[$c]);
} //----------------------------------------------------------------------
public static function checkModeAn($size, $data)
{
for($i=; $i<$size; $i++) {
if (self::lookAnTable(ord($data[$i])) == -) {
return false;
}
} return true;
} //----------------------------------------------------------------------
public static function estimateBitsModeAn($size)
{
$w = (int)($size / );
$bits = $w * ; if($size & ) {
$bits += ;
} return $bits;
} //----------------------------------------------------------------------
public static function estimateBitsMode8($size)
{
return $size * ;
} //----------------------------------------------------------------------
public function estimateBitsModeKanji($size)
{
return (int)(($size / ) * );
} //----------------------------------------------------------------------
public static function checkModeKanji($size, $data)
{
if($size & )
return false; for($i=; $i<$size; $i+=) {
$val = (ord($data[$i]) << ) | ord($data[$i+]);
if( $val < 0x8140
|| ($val > 0x9ffc && $val < 0xe040)
|| $val > 0xebbf) {
return false;
}
} return true;
} /***********************************************************************
* Validation
**********************************************************************/ public static function check($mode, $size, $data)
{
if($size <= )
return false; switch($mode) {
case QR_MODE_NUM: return self::checkModeNum($size, $data); break;
case QR_MODE_AN: return self::checkModeAn($size, $data); break;
case QR_MODE_KANJI: return self::checkModeKanji($size, $data); break;
case QR_MODE_8: return true; break;
case QR_MODE_STRUCTURE: return true; break; default:
break;
} return false;
} //----------------------------------------------------------------------
public function estimateBitStreamSize($version)
{
$bits = ; foreach($this->items as $item) {
$bits += $item->estimateBitStreamSizeOfEntry($version);
} return $bits;
} //----------------------------------------------------------------------
public function estimateVersion()
{
$version = ;
$prev = ;
do {
$prev = $version;
$bits = $this->estimateBitStreamSize($prev);
$version = QRspec::getMinimumVersion((int)(($bits + ) / ), $this->level);
if ($version < ) {
return -;
}
} while ($version > $prev); return $version;
} //----------------------------------------------------------------------
public static function lengthOfCode($mode, $version, $bits)
{
$payload = $bits - - QRspec::lengthIndicator($mode, $version);
switch($mode) {
case QR_MODE_NUM:
$chunks = (int)($payload / );
$remain = $payload - $chunks * ;
$size = $chunks * ;
if($remain >= ) {
$size += ;
} else if($remain >= ) {
$size += ;
}
break;
case QR_MODE_AN:
$chunks = (int)($payload / );
$remain = $payload - $chunks * ;
$size = $chunks * ;
if($remain >= )
$size++;
break;
case QR_MODE_8:
$size = (int)($payload / );
break;
case QR_MODE_KANJI:
$size = (int)(($payload / ) * );
break;
case QR_MODE_STRUCTURE:
$size = (int)($payload / );
break;
default:
$size = ;
break;
} $maxsize = QRspec::maximumWords($mode, $version);
if($size < ) $size = ;
if($size > $maxsize) $size = $maxsize; return $size;
} //----------------------------------------------------------------------
public function createBitStream()
{
$total = ; foreach($this->items as $item) {
$bits = $item->encodeBitStream($this->version); if($bits < )
return -; $total += $bits;
} return $total;
} //----------------------------------------------------------------------
public function convertData()
{
$ver = $this->estimateVersion();
if($ver > $this->getVersion()) {
$this->setVersion($ver);
} for(;;) {
$bits = $this->createBitStream(); if($bits < )
return -; $ver = QRspec::getMinimumVersion((int)(($bits + ) / ), $this->level);
if($ver < ) {
throw new Exception('WRONG VERSION');
return -;
} else if($ver > $this->getVersion()) {
$this->setVersion($ver);
} else {
break;
}
} return ;
} //----------------------------------------------------------------------
public function appendPaddingBit(&$bstream)
{
$bits = $bstream->size();
$maxwords = QRspec::getDataLength($this->version, $this->level);
$maxbits = $maxwords * ; if ($maxbits == $bits) {
return ;
} if ($maxbits - $bits < ) {
return $bstream->appendNum($maxbits - $bits, );
} $bits += ;
$words = (int)(($bits + ) / ); $padding = new QRbitstream();
$ret = $padding->appendNum($words * - $bits + , ); if($ret < )
return $ret; $padlen = $maxwords - $words; if($padlen > ) { $padbuf = array();
for($i=; $i<$padlen; $i++) {
$padbuf[$i] = ($i&)?0x11:0xec;
} $ret = $padding->appendBytes($padlen, $padbuf); if($ret < )
return $ret; } $ret = $bstream->append($padding); return $ret;
} //----------------------------------------------------------------------
public function mergeBitStream()
{
if($this->convertData() < ) {
return null;
} $bstream = new QRbitstream(); foreach($this->items as $item) {
$ret = $bstream->append($item->bstream);
if($ret < ) {
return null;
}
} return $bstream;
} //----------------------------------------------------------------------
public function getBitStream()
{ $bstream = $this->mergeBitStream(); if($bstream == null) {
return null;
} $ret = $this->appendPaddingBit($bstream);
if($ret < ) {
return null;
} return $bstream;
} //----------------------------------------------------------------------
public function getByteStream()
{
$bstream = $this->getBitStream();
if($bstream == null) {
return null;
} return $bstream->toByte();
}
} //---- qrbitstream.php ----------------------------- /*
* PHP QR Code encoder
*
* Bitstream class
*
* Based on libqrencode C library distributed under LGPL 2.1
* Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi <fukuchi@megaui.net>
*
* PHP QR Code is distributed under LGPL 3
* Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/ class QRbitstream { public $data = array(); //----------------------------------------------------------------------
public function size()
{
return count($this->data);
} //----------------------------------------------------------------------
public function allocate($setLength)
{
$this->data = array_fill(, $setLength, );
return ;
} //----------------------------------------------------------------------
public static function newFromNum($bits, $num)
{
$bstream = new QRbitstream();
$bstream->allocate($bits); $mask = << ($bits - );
for($i=; $i<$bits; $i++) {
if($num & $mask) {
$bstream->data[$i] = ;
} else {
$bstream->data[$i] = ;
}
$mask = $mask >> ;
} return $bstream;
} //----------------------------------------------------------------------
public static function newFromBytes($size, $data)
{
$bstream = new QRbitstream();
$bstream->allocate($size * );
$p=; for($i=; $i<$size; $i++) {
$mask = 0x80;
for($j=; $j<; $j++) {
if($data[$i] & $mask) {
$bstream->data[$p] = ;
} else {
$bstream->data[$p] = ;
}
$p++;
$mask = $mask >> ;
}
} return $bstream;
} //----------------------------------------------------------------------
public function append(QRbitstream $arg)
{
if (is_null($arg)) {
return -;
} if($arg->size() == ) {
return ;
} if($this->size() == ) {
$this->data = $arg->data;
return ;
} $this->data = array_values(array_merge($this->data, $arg->data)); return ;
} //----------------------------------------------------------------------
public function appendNum($bits, $num)
{
if ($bits == )
return ; $b = QRbitstream::newFromNum($bits, $num); if(is_null($b))
return -; $ret = $this->append($b);
unset($b); return $ret;
} //----------------------------------------------------------------------
public function appendBytes($size, $data)
{
if ($size == )
return ; $b = QRbitstream::newFromBytes($size, $data); if(is_null($b))
return -; $ret = $this->append($b);
unset($b); return $ret;
} //----------------------------------------------------------------------
public function toByte()
{ $size = $this->size(); if($size == ) {
return array();
} $data = array_fill(, (int)(($size + ) / ), );
$bytes = (int)($size / ); $p = ; for($i=; $i<$bytes; $i++) {
$v = ;
for($j=; $j<; $j++) {
$v = $v << ;
$v |= $this->data[$p];
$p++;
}
$data[$i] = $v;
} if($size & ) {
$v = ;
for($j=; $j<($size & ); $j++) {
$v = $v << ;
$v |= $this->data[$p];
$p++;
}
$data[$bytes] = $v;
} return $data;
} } //---- qrsplit.php ----------------------------- /*
* PHP QR Code encoder
*
* Input splitting classes
*
* Based on libqrencode C library distributed under LGPL 2.1
* Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi <fukuchi@megaui.net>
*
* PHP QR Code is distributed under LGPL 3
* Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
*
* The following data / specifications are taken from
* "Two dimensional symbol -- QR-code -- Basic Specification" (JIS X0510:2004)
* or
* "Automatic identification and data capture techniques --
* QR Code 2005 bar code symbology specification" (ISO/IEC 18004:2006)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class QRsplit { public $dataStr = '';
public $input;
public $modeHint; //----------------------------------------------------------------------
public function __construct($dataStr, $input, $modeHint)
{
$this->dataStr = $dataStr;
$this->input = $input;
$this->modeHint = $modeHint;
} //----------------------------------------------------------------------
public static function isdigitat($str, $pos)
{
if ($pos >= strlen($str))
return false; return ((ord($str[$pos]) >= ord(''))&&(ord($str[$pos]) <= ord('')));
} //----------------------------------------------------------------------
public static function isalnumat($str, $pos)
{
if ($pos >= strlen($str))
return false; return (QRinput::lookAnTable(ord($str[$pos])) >= );
} //----------------------------------------------------------------------
public function identifyMode($pos)
{
if ($pos >= strlen($this->dataStr))
return QR_MODE_NUL; $c = $this->dataStr[$pos]; if(self::isdigitat($this->dataStr, $pos)) {
return QR_MODE_NUM;
} else if(self::isalnumat($this->dataStr, $pos)) {
return QR_MODE_AN;
} else if($this->modeHint == QR_MODE_KANJI) { if ($pos+ < strlen($this->dataStr))
{
$d = $this->dataStr[$pos+];
$word = (ord($c) << ) | ord($d);
if(($word >= 0x8140 && $word <= 0x9ffc) || ($word >= 0xe040 && $word <= 0xebbf)) {
return QR_MODE_KANJI;
}
}
} return QR_MODE_8;
} //----------------------------------------------------------------------
public function eatNum()
{
$ln = QRspec::lengthIndicator(QR_MODE_NUM, $this->input->getVersion()); $p = ;
while(self::isdigitat($this->dataStr, $p)) {
$p++;
} $run = $p;
$mode = $this->identifyMode($p); if($mode == QR_MODE_8) {
$dif = QRinput::estimateBitsModeNum($run) + + $ln
+ QRinput::estimateBitsMode8() // + 4 + l8
- QRinput::estimateBitsMode8($run + ); // - 4 - l8
if($dif > ) {
return $this->eat8();
}
}
if($mode == QR_MODE_AN) {
$dif = QRinput::estimateBitsModeNum($run) + + $ln
+ QRinput::estimateBitsModeAn() // + 4 + la
- QRinput::estimateBitsModeAn($run + );// - 4 - la
if($dif > ) {
return $this->eatAn();
}
} $ret = $this->input->append(QR_MODE_NUM, $run, str_split($this->dataStr));
if($ret < )
return -; return $run;
} //----------------------------------------------------------------------
public function eatAn()
{
$la = QRspec::lengthIndicator(QR_MODE_AN, $this->input->getVersion());
$ln = QRspec::lengthIndicator(QR_MODE_NUM, $this->input->getVersion()); $p = ; while(self::isalnumat($this->dataStr, $p)) {
if(self::isdigitat($this->dataStr, $p)) {
$q = $p;
while(self::isdigitat($this->dataStr, $q)) {
$q++;
} $dif = QRinput::estimateBitsModeAn($p) // + 4 + la
+ QRinput::estimateBitsModeNum($q - $p) + + $ln
- QRinput::estimateBitsModeAn($q); // - 4 - la if($dif < ) {
break;
} else {
$p = $q;
}
} else {
$p++;
}
} $run = $p; if(!self::isalnumat($this->dataStr, $p)) {
$dif = QRinput::estimateBitsModeAn($run) + + $la
+ QRinput::estimateBitsMode8() // + 4 + l8
- QRinput::estimateBitsMode8($run + ); // - 4 - l8
if($dif > ) {
return $this->eat8();
}
} $ret = $this->input->append(QR_MODE_AN, $run, str_split($this->dataStr));
if($ret < )
return -; return $run;
} //----------------------------------------------------------------------
public function eatKanji()
{
$p = ; while($this->identifyMode($p) == QR_MODE_KANJI) {
$p += ;
} $ret = $this->input->append(QR_MODE_KANJI, $p, str_split($this->dataStr));
if($ret < )
return -; return $run;
} //----------------------------------------------------------------------
public function eat8()
{
$la = QRspec::lengthIndicator(QR_MODE_AN, $this->input->getVersion());
$ln = QRspec::lengthIndicator(QR_MODE_NUM, $this->input->getVersion()); $p = ;
$dataStrLen = strlen($this->dataStr); while($p < $dataStrLen) { $mode = $this->identifyMode($p);
if($mode == QR_MODE_KANJI) {
break;
}
if($mode == QR_MODE_NUM) {
$q = $p;
while(self::isdigitat($this->dataStr, $q)) {
$q++;
}
$dif = QRinput::estimateBitsMode8($p) // + 4 + l8
+ QRinput::estimateBitsModeNum($q - $p) + + $ln
- QRinput::estimateBitsMode8($q); // - 4 - l8
if($dif < ) {
break;
} else {
$p = $q;
}
} else if($mode == QR_MODE_AN) {
$q = $p;
while(self::isalnumat($this->dataStr, $q)) {
$q++;
}
$dif = QRinput::estimateBitsMode8($p) // + 4 + l8
+ QRinput::estimateBitsModeAn($q - $p) + + $la
- QRinput::estimateBitsMode8($q); // - 4 - l8
if($dif < ) {
break;
} else {
$p = $q;
}
} else {
$p++;
}
} $run = $p;
$ret = $this->input->append(QR_MODE_8, $run, str_split($this->dataStr)); if($ret < )
return -; return $run;
} //----------------------------------------------------------------------
public function splitString()
{
while (strlen($this->dataStr) > )
{
if($this->dataStr == '')
return ; $mode = $this->identifyMode(); switch ($mode) {
case QR_MODE_NUM: $length = $this->eatNum(); break;
case QR_MODE_AN: $length = $this->eatAn(); break;
case QR_MODE_KANJI:
if ($hint == QR_MODE_KANJI)
$length = $this->eatKanji();
else $length = $this->eat8();
break;
default: $length = $this->eat8(); break; } if($length == ) return ;
if($length < ) return -; $this->dataStr = substr($this->dataStr, $length);
}
} //----------------------------------------------------------------------
public function toUpper()
{
$stringLen = strlen($this->dataStr);
$p = ; while ($p<$stringLen) {
$mode = self::identifyMode(substr($this->dataStr, $p), $this->modeHint);
if($mode == QR_MODE_KANJI) {
$p += ;
} else {
if (ord($this->dataStr[$p]) >= ord('a') && ord($this->dataStr[$p]) <= ord('z')) {
$this->dataStr[$p] = chr(ord($this->dataStr[$p]) - );
}
$p++;
}
} return $this->dataStr;
} //----------------------------------------------------------------------
public static function splitStringToQRinput($string, QRinput $input, $modeHint, $casesensitive = true)
{
if(is_null($string) || $string == '\0' || $string == '') {
throw new Exception('empty string!!!');
} $split = new QRsplit($string, $input, $modeHint); if(!$casesensitive)
$split->toUpper(); return $split->splitString();
}
} //---- qrrscode.php ----------------------------- /*
* PHP QR Code encoder
*
* Reed-Solomon error correction support
*
* Copyright (C) 2002, 2003, 2004, 2006 Phil Karn, KA9Q
* (libfec is released under the GNU Lesser General Public License.)
*
* Based on libqrencode C library distributed under LGPL 2.1
* Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi <fukuchi@megaui.net>
*
* PHP QR Code is distributed under LGPL 3
* Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/ class QRrsItem { public $mm; // Bits per symbol
public $nn; // Symbols per block (= (1<<mm)-1)
public $alpha_to = array(); // log lookup table
public $index_of = array(); // Antilog lookup table
public $genpoly = array(); // Generator polynomial
public $nroots; // Number of generator roots = number of parity symbols
public $fcr; // First consecutive root, index form
public $prim; // Primitive element, index form
public $iprim; // prim-th root of 1, index form
public $pad; // Padding bytes in shortened block
public $gfpoly; //----------------------------------------------------------------------
public function modnn($x)
{
while ($x >= $this->nn) {
$x -= $this->nn;
$x = ($x >> $this->mm) + ($x & $this->nn);
} return $x;
} //----------------------------------------------------------------------
public static function init_rs_char($symsize, $gfpoly, $fcr, $prim, $nroots, $pad)
{
// Common code for intializing a Reed-Solomon control block (char or int symbols)
// Copyright 2004 Phil Karn, KA9Q
// May be used under the terms of the GNU Lesser General Public License (LGPL) $rs = null; // Check parameter ranges
if($symsize < || $symsize > ) return $rs;
if($fcr < || $fcr >= (<<$symsize)) return $rs;
if($prim <= || $prim >= (<<$symsize)) return $rs;
if($nroots < || $nroots >= (<<$symsize)) return $rs; // Can't have more roots than symbol values!
if($pad < || $pad >= ((<<$symsize) - - $nroots)) return $rs; // Too much padding $rs = new QRrsItem();
$rs->mm = $symsize;
$rs->nn = (<<$symsize)-;
$rs->pad = $pad; $rs->alpha_to = array_fill(, $rs->nn+, );
$rs->index_of = array_fill(, $rs->nn+, ); // PHP style macro replacement ;)
$NN =& $rs->nn;
$A0 =& $NN; // Generate Galois field lookup tables
$rs->index_of[] = $A0; // log(zero) = -inf
$rs->alpha_to[$A0] = ; // alpha**-inf = 0
$sr = ; for($i=; $i<$rs->nn; $i++) {
$rs->index_of[$sr] = $i;
$rs->alpha_to[$i] = $sr;
$sr <<= ;
if($sr & (<<$symsize)) {
$sr ^= $gfpoly;
}
$sr &= $rs->nn;
} if($sr != ){
// field generator polynomial is not primitive!
$rs = NULL;
return $rs;
} /* Form RS code generator polynomial from its roots */
$rs->genpoly = array_fill(, $nroots+, ); $rs->fcr = $fcr;
$rs->prim = $prim;
$rs->nroots = $nroots;
$rs->gfpoly = $gfpoly; /* Find prim-th root of 1, used in decoding */
for($iprim=;($iprim % $prim) != ;$iprim += $rs->nn)
; // intentional empty-body loop! $rs->iprim = (int)($iprim / $prim);
$rs->genpoly[] = ; for ($i = ,$root=$fcr*$prim; $i < $nroots; $i++, $root += $prim) {
$rs->genpoly[$i+] = ; // Multiply rs->genpoly[] by @**(root + x)
for ($j = $i; $j > ; $j--) {
if ($rs->genpoly[$j] != ) {
$rs->genpoly[$j] = $rs->genpoly[$j-] ^ $rs->alpha_to[$rs->modnn($rs->index_of[$rs->genpoly[$j]] + $root)];
} else {
$rs->genpoly[$j] = $rs->genpoly[$j-];
}
}
// rs->genpoly[0] can never be zero
$rs->genpoly[] = $rs->alpha_to[$rs->modnn($rs->index_of[$rs->genpoly[]] + $root)];
} // convert rs->genpoly[] to index form for quicker encoding
for ($i = ; $i <= $nroots; $i++)
$rs->genpoly[$i] = $rs->index_of[$rs->genpoly[$i]]; return $rs;
} //----------------------------------------------------------------------
public function encode_rs_char($data, &$parity)
{
$MM =& $this->mm;
$NN =& $this->nn;
$ALPHA_TO =& $this->alpha_to;
$INDEX_OF =& $this->index_of;
$GENPOLY =& $this->genpoly;
$NROOTS =& $this->nroots;
$FCR =& $this->fcr;
$PRIM =& $this->prim;
$IPRIM =& $this->iprim;
$PAD =& $this->pad;
$A0 =& $NN; $parity = array_fill(, $NROOTS, ); for($i=; $i< ($NN-$NROOTS-$PAD); $i++) { $feedback = $INDEX_OF[$data[$i] ^ $parity[]];
if($feedback != $A0) {
// feedback term is non-zero // This line is unnecessary when GENPOLY[NROOTS] is unity, as it must
// always be for the polynomials constructed by init_rs()
$feedback = $this->modnn($NN - $GENPOLY[$NROOTS] + $feedback); for($j=;$j<$NROOTS;$j++) {
$parity[$j] ^= $ALPHA_TO[$this->modnn($feedback + $GENPOLY[$NROOTS-$j])];
}
} // Shift
array_shift($parity);
if($feedback != $A0) {
array_push($parity, $ALPHA_TO[$this->modnn($feedback + $GENPOLY[])]);
} else {
array_push($parity, );
}
}
}
} //########################################################################## class QRrs { public static $items = array(); //----------------------------------------------------------------------
public static function init_rs($symsize, $gfpoly, $fcr, $prim, $nroots, $pad)
{
foreach(self::$items as $rs) {
if($rs->pad != $pad) continue;
if($rs->nroots != $nroots) continue;
if($rs->mm != $symsize) continue;
if($rs->gfpoly != $gfpoly) continue;
if($rs->fcr != $fcr) continue;
if($rs->prim != $prim) continue; return $rs;
} $rs = QRrsItem::init_rs_char($symsize, $gfpoly, $fcr, $prim, $nroots, $pad);
array_unshift(self::$items, $rs); return $rs;
}
} //---- qrmask.php ----------------------------- /*
* PHP QR Code encoder
*
* Masking
*
* Based on libqrencode C library distributed under LGPL 2.1
* Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi <fukuchi@megaui.net>
*
* PHP QR Code is distributed under LGPL 3
* Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/ define('N1', );
define('N2', );
define('N3', );
define('N4', ); class QRmask { public $runLength = array(); //----------------------------------------------------------------------
public function __construct()
{
$this->runLength = array_fill(, QRSPEC_WIDTH_MAX + , );
} //----------------------------------------------------------------------
public function writeFormatInformation($width, &$frame, $mask, $level)
{
$blacks = ;
$format = QRspec::getFormatInfo($mask, $level); for($i=; $i<; $i++) {
if($format & ) {
$blacks += ;
$v = 0x85;
} else {
$v = 0x84;
} $frame[][$width - - $i] = chr($v);
if($i < ) {
$frame[$i][] = chr($v);
} else {
$frame[$i + ][] = chr($v);
}
$format = $format >> ;
} for($i=; $i<; $i++) {
if($format & ) {
$blacks += ;
$v = 0x85;
} else {
$v = 0x84;
} $frame[$width - + $i][] = chr($v);
if($i == ) {
$frame[][] = chr($v);
} else {
$frame[][ - $i] = chr($v);
} $format = $format >> ;
} return $blacks;
} //----------------------------------------------------------------------
public function mask0($x, $y) { return ($x+$y)&; }
public function mask1($x, $y) { return ($y&); }
public function mask2($x, $y) { return ($x%); }
public function mask3($x, $y) { return ($x+$y)%; }
public function mask4($x, $y) { return (((int)($y/))+((int)($x/)))&; }
public function mask5($x, $y) { return (($x*$y)&)+($x*$y)%; }
public function mask6($x, $y) { return ((($x*$y)&)+($x*$y)%)&; }
public function mask7($x, $y) { return ((($x*$y)%)+(($x+$y)&))&; } //----------------------------------------------------------------------
private function generateMaskNo($maskNo, $width, $frame)
{
$bitMask = array_fill(, $width, array_fill(, $width, )); for($y=; $y<$width; $y++) {
for($x=; $x<$width; $x++) {
if(ord($frame[$y][$x]) & 0x80) {
$bitMask[$y][$x] = ;
} else {
$maskFunc = call_user_func(array($this, 'mask'.$maskNo), $x, $y);
$bitMask[$y][$x] = ($maskFunc == )?:;
} }
} return $bitMask;
} //----------------------------------------------------------------------
public static function serial($bitFrame)
{
$codeArr = array(); foreach ($bitFrame as $line)
$codeArr[] = join('', $line); return gzcompress(join("\n", $codeArr), );
} //----------------------------------------------------------------------
public static function unserial($code)
{
$codeArr = array(); $codeLines = explode("\n", gzuncompress($code));
foreach ($codeLines as $line)
$codeArr[] = str_split($line); return $codeArr;
} //----------------------------------------------------------------------
public function makeMaskNo($maskNo, $width, $s, &$d, $maskGenOnly = false)
{
$b = ;
$bitMask = array(); $fileName = QR_CACHE_DIR.'mask_'.$maskNo.DIRECTORY_SEPARATOR.'mask_'.$width.'_'.$maskNo.'.dat'; if (QR_CACHEABLE) {
if (file_exists($fileName)) {
$bitMask = self::unserial(file_get_contents($fileName));
} else {
$bitMask = $this->generateMaskNo($maskNo, $width, $s, $d);
if (!file_exists(QR_CACHE_DIR.'mask_'.$maskNo))
mkdir(QR_CACHE_DIR.'mask_'.$maskNo);
file_put_contents($fileName, self::serial($bitMask));
}
} else {
$bitMask = $this->generateMaskNo($maskNo, $width, $s, $d);
} if ($maskGenOnly)
return; $d = $s; for($y=; $y<$width; $y++) {
for($x=; $x<$width; $x++) {
if($bitMask[$y][$x] == ) {
$d[$y][$x] = chr(ord($s[$y][$x]) ^ (int)$bitMask[$y][$x]);
}
$b += (int)(ord($d[$y][$x]) & );
}
} return $b;
} //----------------------------------------------------------------------
public function makeMask($width, $frame, $maskNo, $level)
{
$masked = array_fill(, $width, str_repeat("\0", $width));
$this->makeMaskNo($maskNo, $width, $frame, $masked);
$this->writeFormatInformation($width, $masked, $maskNo, $level); return $masked;
} //----------------------------------------------------------------------
public function calcN1N3($length)
{
$demerit = ; for($i=; $i<$length; $i++) { if($this->runLength[$i] >= ) {
$demerit += (N1 + ($this->runLength[$i] - ));
}
if($i & ) {
if(($i >= ) && ($i < ($length-)) && ($this->runLength[$i] % == )) {
$fact = (int)($this->runLength[$i] / );
if(($this->runLength[$i-] == $fact) &&
($this->runLength[$i-] == $fact) &&
($this->runLength[$i+] == $fact) &&
($this->runLength[$i+] == $fact)) {
if(($this->runLength[$i-] < ) || ($this->runLength[$i-] >= ( * $fact))) {
$demerit += N3;
} else if((($i+) >= $length) || ($this->runLength[$i+] >= ( * $fact))) {
$demerit += N3;
}
}
}
}
}
return $demerit;
} //----------------------------------------------------------------------
public function evaluateSymbol($width, $frame)
{
$head = ;
$demerit = ; for($y=; $y<$width; $y++) {
$head = ;
$this->runLength[] = ; $frameY = $frame[$y]; if ($y>)
$frameYM = $frame[$y-]; for($x=; $x<$width; $x++) {
if(($x > ) && ($y > )) {
$b22 = ord($frameY[$x]) & ord($frameY[$x-]) & ord($frameYM[$x]) & ord($frameYM[$x-]);
$w22 = ord($frameY[$x]) | ord($frameY[$x-]) | ord($frameYM[$x]) | ord($frameYM[$x-]); if(($b22 | ($w22 ^ ))&) {
$demerit += N2;
}
}
if(($x == ) && (ord($frameY[$x]) & )) {
$this->runLength[] = -;
$head = ;
$this->runLength[$head] = ;
} else if($x > ) {
if((ord($frameY[$x]) ^ ord($frameY[$x-])) & ) {
$head++;
$this->runLength[$head] = ;
} else {
$this->runLength[$head]++;
}
}
} $demerit += $this->calcN1N3($head+);
} for($x=; $x<$width; $x++) {
$head = ;
$this->runLength[] = ; for($y=; $y<$width; $y++) {
if($y == && (ord($frame[$y][$x]) & )) {
$this->runLength[] = -;
$head = ;
$this->runLength[$head] = ;
} else if($y > ) {
if((ord($frame[$y][$x]) ^ ord($frame[$y-][$x])) & ) {
$head++;
$this->runLength[$head] = ;
} else {
$this->runLength[$head]++;
}
}
} $demerit += $this->calcN1N3($head+);
} return $demerit;
} //----------------------------------------------------------------------
public function mask($width, $frame, $level)
{
$minDemerit = PHP_INT_MAX;
$bestMaskNum = ;
$bestMask = array(); $checked_masks = array(,,,,,,,); if (QR_FIND_FROM_RANDOM !== false) { $howManuOut = -(QR_FIND_FROM_RANDOM % );
for ($i = ; $i < $howManuOut; $i++) {
$remPos = rand (, count($checked_masks)-);
unset($checked_masks[$remPos]);
$checked_masks = array_values($checked_masks);
} } $bestMask = $frame; foreach($checked_masks as $i) {
$mask = array_fill(, $width, str_repeat("\0", $width)); $demerit = ;
$blacks = ;
$blacks = $this->makeMaskNo($i, $width, $frame, $mask);
$blacks += $this->writeFormatInformation($width, $mask, $i, $level);
$blacks = (int)( * $blacks / ($width * $width));
$demerit = (int)((int)(abs($blacks - ) / ) * N4);
$demerit += $this->evaluateSymbol($width, $mask); if($demerit < $minDemerit) {
$minDemerit = $demerit;
$bestMask = $mask;
$bestMaskNum = $i;
}
} return $bestMask;
} //----------------------------------------------------------------------
} //---- qrencode.php ----------------------------- /*
* PHP QR Code encoder
*
* Main encoder classes.
*
* Based on libqrencode C library distributed under LGPL 2.1
* Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi <fukuchi@megaui.net>
*
* PHP QR Code is distributed under LGPL 3
* Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/ class QRrsblock {
public $dataLength;
public $data = array();
public $eccLength;
public $ecc = array(); public function __construct($dl, $data, $el, &$ecc, QRrsItem $rs)
{
$rs->encode_rs_char($data, $ecc); $this->dataLength = $dl;
$this->data = $data;
$this->eccLength = $el;
$this->ecc = $ecc;
}
}; //########################################################################## class QRrawcode {
public $version;
public $datacode = array();
public $ecccode = array();
public $blocks;
public $rsblocks = array(); //of RSblock
public $count;
public $dataLength;
public $eccLength;
public $b1; //----------------------------------------------------------------------
public function __construct(QRinput $input)
{
$spec = array(,,,,); $this->datacode = $input->getByteStream();
if(is_null($this->datacode)) {
throw new Exception('null imput string');
} QRspec::getEccSpec($input->getVersion(), $input->getErrorCorrectionLevel(), $spec); $this->version = $input->getVersion();
$this->b1 = QRspec::rsBlockNum1($spec);
$this->dataLength = QRspec::rsDataLength($spec);
$this->eccLength = QRspec::rsEccLength($spec);
$this->ecccode = array_fill(, $this->eccLength, );
$this->blocks = QRspec::rsBlockNum($spec); $ret = $this->init($spec);
if($ret < ) {
throw new Exception('block alloc error');
return null;
} $this->count = ;
} //----------------------------------------------------------------------
public function init(array $spec)
{
$dl = QRspec::rsDataCodes1($spec);
$el = QRspec::rsEccCodes1($spec);
$rs = QRrs::init_rs(, 0x11d, , , $el, - $dl - $el); $blockNo = ;
$dataPos = ;
$eccPos = ;
for($i=; $i<QRspec::rsBlockNum1($spec); $i++) {
$ecc = array_slice($this->ecccode,$eccPos);
$this->rsblocks[$blockNo] = new QRrsblock($dl, array_slice($this->datacode, $dataPos), $el, $ecc, $rs);
$this->ecccode = array_merge(array_slice($this->ecccode,, $eccPos), $ecc); $dataPos += $dl;
$eccPos += $el;
$blockNo++;
} if(QRspec::rsBlockNum2($spec) == )
return ; $dl = QRspec::rsDataCodes2($spec);
$el = QRspec::rsEccCodes2($spec);
$rs = QRrs::init_rs(, 0x11d, , , $el, - $dl - $el); if($rs == NULL) return -; for($i=; $i<QRspec::rsBlockNum2($spec); $i++) {
$ecc = array_slice($this->ecccode,$eccPos);
$this->rsblocks[$blockNo] = new QRrsblock($dl, array_slice($this->datacode, $dataPos), $el, $ecc, $rs);
$this->ecccode = array_merge(array_slice($this->ecccode,, $eccPos), $ecc); $dataPos += $dl;
$eccPos += $el;
$blockNo++;
} return ;
} //----------------------------------------------------------------------
public function getCode()
{
$ret; if($this->count < $this->dataLength) {
$row = $this->count % $this->blocks;
$col = $this->count / $this->blocks;
if($col >= $this->rsblocks[]->dataLength) {
$row += $this->b1;
}
$ret = $this->rsblocks[$row]->data[$col];
} else if($this->count < $this->dataLength + $this->eccLength) {
$row = ($this->count - $this->dataLength) % $this->blocks;
$col = ($this->count - $this->dataLength) / $this->blocks;
$ret = $this->rsblocks[$row]->ecc[$col];
} else {
return ;
}
$this->count++; return $ret;
}
} //########################################################################## class QRcode { public $version;
public $width;
public $data; //----------------------------------------------------------------------
public function encodeMask(QRinput $input, $mask)
{
if($input->getVersion() < || $input->getVersion() > QRSPEC_VERSION_MAX) {
throw new Exception('wrong version');
}
if($input->getErrorCorrectionLevel() > QR_ECLEVEL_H) {
throw new Exception('wrong level');
} $raw = new QRrawcode($input); QRtools::markTime('after_raw'); $version = $raw->version;
$width = QRspec::getWidth($version);
$frame = QRspec::newFrame($version); $filler = new FrameFiller($width, $frame);
if(is_null($filler)) {
return NULL;
} // inteleaved data and ecc codes
for($i=; $i<$raw->dataLength + $raw->eccLength; $i++) {
$code = $raw->getCode();
$bit = 0x80;
for($j=; $j<; $j++) {
$addr = $filler->next();
$filler->setFrameAt($addr, 0x02 | (($bit & $code) != ));
$bit = $bit >> ;
}
} QRtools::markTime('after_filler'); unset($raw); // remainder bits
$j = QRspec::getRemainder($version);
for($i=; $i<$j; $i++) {
$addr = $filler->next();
$filler->setFrameAt($addr, 0x02);
} $frame = $filler->frame;
unset($filler); // masking
$maskObj = new QRmask();
if($mask < ) { if (QR_FIND_BEST_MASK) {
$masked = $maskObj->mask($width, $frame, $input->getErrorCorrectionLevel());
} else {
$masked = $maskObj->makeMask($width, $frame, (intval(QR_DEFAULT_MASK) % ), $input->getErrorCorrectionLevel());
}
} else {
$masked = $maskObj->makeMask($width, $frame, $mask, $input->getErrorCorrectionLevel());
} if($masked == NULL) {
return NULL;
} QRtools::markTime('after_mask'); $this->version = $version;
$this->width = $width;
$this->data = $masked; return $this;
} //----------------------------------------------------------------------
public function encodeInput(QRinput $input)
{
return $this->encodeMask($input, -);
} //----------------------------------------------------------------------
public function encodeString8bit($string, $version, $level)
{
if(string == NULL) {
throw new Exception('empty string!');
return NULL;
} $input = new QRinput($version, $level);
if($input == NULL) return NULL; $ret = $input->append($input, QR_MODE_8, strlen($string), str_split($string));
if($ret < ) {
unset($input);
return NULL;
}
return $this->encodeInput($input);
} //----------------------------------------------------------------------
public function encodeString($string, $version, $level, $hint, $casesensitive)
{ if($hint != QR_MODE_8 && $hint != QR_MODE_KANJI) {
throw new Exception('bad hint');
return NULL;
} $input = new QRinput($version, $level);
if($input == NULL) return NULL; $ret = QRsplit::splitStringToQRinput($string, $input, $hint, $casesensitive);
if($ret < ) {
return NULL;
} return $this->encodeInput($input);
} //----------------------------------------------------------------------
public static function png($text, $outfile = false, $level = QR_ECLEVEL_L, $size = , $margin = , $saveandprint=false)
{
$enc = QRencode::factory($level, $size, $margin);
return $enc->encodePNG($text, $outfile, $saveandprint=false);
} //----------------------------------------------------------------------
public static function text($text, $outfile = false, $level = QR_ECLEVEL_L, $size = , $margin = )
{
$enc = QRencode::factory($level, $size, $margin);
return $enc->encode($text, $outfile);
} //----------------------------------------------------------------------
public static function raw($text, $outfile = false, $level = QR_ECLEVEL_L, $size = , $margin = )
{
$enc = QRencode::factory($level, $size, $margin);
return $enc->encodeRAW($text, $outfile);
}
} //########################################################################## class FrameFiller { public $width;
public $frame;
public $x;
public $y;
public $dir;
public $bit; //----------------------------------------------------------------------
public function __construct($width, &$frame)
{
$this->width = $width;
$this->frame = $frame;
$this->x = $width - ;
$this->y = $width - ;
$this->dir = -;
$this->bit = -;
} //----------------------------------------------------------------------
public function setFrameAt($at, $val)
{
$this->frame[$at['y']][$at['x']] = chr($val);
} //----------------------------------------------------------------------
public function getFrameAt($at)
{
return ord($this->frame[$at['y']][$at['x']]);
} //----------------------------------------------------------------------
public function next()
{
do { if($this->bit == -) {
$this->bit = ;
return array('x'=>$this->x, 'y'=>$this->y);
} $x = $this->x;
$y = $this->y;
$w = $this->width; if($this->bit == ) {
$x--;
$this->bit++;
} else {
$x++;
$y += $this->dir;
$this->bit--;
} if($this->dir < ) {
if($y < ) {
$y = ;
$x -= ;
$this->dir = ;
if($x == ) {
$x--;
$y = ;
}
}
} else {
if($y == $w) {
$y = $w - ;
$x -= ;
$this->dir = -;
if($x == ) {
$x--;
$y -= ;
}
}
}
if($x < || $y < ) return null; $this->x = $x;
$this->y = $y; } while(ord($this->frame[$y][$x]) & 0x80); return array('x'=>$x, 'y'=>$y);
} } ; //########################################################################## class QRencode { public $casesensitive = true;
public $eightbit = false; public $version = ;
public $size = ;
public $margin = ; public $structured = ; // not supported yet public $level = QR_ECLEVEL_L;
public $hint = QR_MODE_8; //----------------------------------------------------------------------
public static function factory($level = QR_ECLEVEL_L, $size = , $margin = )
{
$enc = new QRencode();
$enc->size = $size;
$enc->margin = $margin; switch ($level.'') {
case '':
case '':
case '':
case '':
$enc->level = $level;
break;
case 'l':
case 'L':
$enc->level = QR_ECLEVEL_L;
break;
case 'm':
case 'M':
$enc->level = QR_ECLEVEL_M;
break;
case 'q':
case 'Q':
$enc->level = QR_ECLEVEL_Q;
break;
case 'h':
case 'H':
$enc->level = QR_ECLEVEL_H;
break;
} return $enc;
} //----------------------------------------------------------------------
public function encodeRAW($intext, $outfile = false)
{
$code = new QRcode(); if($this->eightbit) {
$code->encodeString8bit($intext, $this->version, $this->level);
} else {
$code->encodeString($intext, $this->version, $this->level, $this->hint, $this->casesensitive);
} return $code->data;
} //----------------------------------------------------------------------
public function encode($intext, $outfile = false)
{
$code = new QRcode(); if($this->eightbit) {
$code->encodeString8bit($intext, $this->version, $this->level);
} else {
$code->encodeString($intext, $this->version, $this->level, $this->hint, $this->casesensitive);
} QRtools::markTime('after_encode'); if ($outfile!== false) {
file_put_contents($outfile, join("\n", QRtools::binarize($code->data)));
} else {
return QRtools::binarize($code->data);
}
} //----------------------------------------------------------------------
public function encodePNG($intext, $outfile = false,$saveandprint=false)
{
try { ob_start();
$tab = $this->encode($intext);
$err = ob_get_contents();
ob_end_clean(); if ($err != '')
QRtools::log($outfile, $err); $maxSize = (int)(QR_PNG_MAXIMUM_SIZE / (count($tab)+*$this->margin)); QRimage::png($tab, $outfile, min(max(, $this->size), $maxSize), $this->margin,$saveandprint); } catch (Exception $e) { QRtools::log($outfile, $e->getMessage()); }
}
} ?>

上面代码另存为:phpqrcode.php

最新文章

  1. [LeetCode] Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K
  2. java常见面试题及答案 11-20(JVM篇)
  3. [转]权限问题导致Nginx 403 Forbidden错误的解决方法
  4. 安装office2013时弹出microsoft setup bootstrapper已停止工作,接着就不能安装了
  5. C++STL 之排列
  6. linux 搭建pptpd vpn(转,备忘)
  7. [LeetCode]题解(python):068-Text Justification
  8. URL末尾处的斜杠“/”
  9. redis的持久化之AOF
  10. Oracle在.sql文件中创建存储过程
  11. MySQL主从1205报错【转】
  12. Codeforces 305E Playing with String 博弈
  13. SQLLITE HELPER
  14. jq通过对象获取其ID值
  15. Java集合实现类区别与联系
  16. .NET Core开发日志——从ASP.NET Core Module到KestrelServer
  17. 踩坑 net core
  18. APP分析----饿了么
  19. @Dataprovider 和 @Factory 的使用
  20. SpringMvc HttpMessageConverter之@ResponseBody

热门文章

  1. 修改phpadmin中的默认超时时间
  2. 从mysql全库备份中恢复指定库和指定表
  3. C#override与new修饰隐藏的区别(转载)
  4. java网络编程_IP地址
  5. 神经机器翻译(NMT)开源工具
  6. [Python3网络爬虫开发实战] 1.9.3-Scrapyd-Client的安装
  7. laravel框架应用和composer扩展包开发
  8. 79-Envelopes,包络指标.(2015.7.1)
  9. 洛谷 2187 小Z的笔记
  10. 集训第四周(高效算法设计)B题 (二分查找优化题)