Example #1
0
 public function init(array $spec)
 {
     $dl = QRspec::rsDataCodes1($spec);
     $el = QRspec::rsEccCodes1($spec);
     $rs = QRrs::initRs(8, 0x11d, 0, 1, $el, 255 - $dl - $el);
     $blockNo = 0;
     $dataPos = 0;
     $eccPos = 0;
     for ($i = 0; $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, 0, $eccPos), $ecc);
         $dataPos += $dl;
         $eccPos += $el;
         $blockNo++;
     }
     if (QRspec::rsBlockNum2($spec) == 0) {
         return 0;
     }
     $dl = QRspec::rsDataCodes2($spec);
     $el = QRspec::rsEccCodes2($spec);
     $rs = QRrs::initRs(8, 0x11d, 0, 1, $el, 255 - $dl - $el);
     if ($rs == null) {
         return -1;
     }
     for ($i = 0; $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, 0, $eccPos), $ecc);
         $dataPos += $dl;
         $eccPos += $el;
         $blockNo++;
     }
     return 0;
 }
Example #2
0
 public function encodeMask(QRinput $input, $mask)
 {
     if ($input->getVersion() < 0 || $input->getVersion() > Constants::QRSPEC_VERSION_MAX) {
         throw new Exception('wrong version');
     }
     if ($input->getErrorCorrectionLevel() > Constants::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 = 0; $i < $raw->dataLength + $raw->eccLength; $i++) {
         $code = $raw->getCode();
         $bit = 0x80;
         for ($j = 0; $j < 8; $j++) {
             $addr = $filler->next();
             $filler->setFrameAt($addr, 0x2 | ($bit & $code) != 0);
             $bit = $bit >> 1;
         }
     }
     QRtools::markTime('after_filler');
     unset($raw);
     // remainder bits
     $j = QRspec::getRemainder($version);
     for ($i = 0; $i < $j; $i++) {
         $addr = $filler->next();
         $filler->setFrameAt($addr, 0x2);
     }
     $frame = $filler->frame;
     unset($filler);
     // masking
     $maskObj = new QRmask();
     if ($mask < 0) {
         if (Constants::QR_FIND_BEST_MASK) {
             $masked = $maskObj->mask($width, $frame, $input->getErrorCorrectionLevel());
         } else {
             $masked = $maskObj->makeMask($width, $frame, intval(Constants::QR_DEFAULT_MASK) % 8, $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;
 }
Example #3
0
 public static function buildCache()
 {
     QRtools::markTime('before_build_cache');
     $mask = new QRmask();
     for ($a = 1; $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, 1, 0);
         }
         $width = count($frame);
         $bitMask = array_fill(0, $width, array_fill(0, $width, 0));
         for ($maskNo = 0; $maskNo < 8; $maskNo++) {
             $mask->makeMaskNo($maskNo, $width, $frame, $bitMask, true);
         }
     }
     QRtools::markTime('after_build_cache');
 }
Example #4
0
 public function writeFormatInformation($width, &$frame, $mask, $level)
 {
     $blacks = 0;
     $format = QRspec::getFormatInfo($mask, $level);
     for ($i = 0; $i < 8; $i++) {
         if ($format & 1) {
             $blacks += 2;
             $v = 0x85;
         } else {
             $v = 0x84;
         }
         $frame[8][$width - 1 - $i] = chr($v);
         if ($i < 6) {
             $frame[$i][8] = chr($v);
         } else {
             $frame[$i + 1][8] = chr($v);
         }
         $format = $format >> 1;
     }
     for ($i = 0; $i < 7; $i++) {
         if ($format & 1) {
             $blacks += 2;
             $v = 0x85;
         } else {
             $v = 0x84;
         }
         $frame[$width - 7 + $i][8] = chr($v);
         if ($i == 0) {
             $frame[8][7] = chr($v);
         } else {
             $frame[8][6 - $i] = chr($v);
         }
         $format = $format >> 1;
     }
     return $blacks;
 }
Example #5
0
 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 = 0;
             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 < 0) {
                 return -1;
             }
         }
         return $this->bstream->size();
     } catch (Exception $e) {
         return -1;
     }
 }
Example #6
0
 public function appendPaddingBit(&$bstream)
 {
     $bits = $bstream->size();
     $maxwords = QRspec::getDataLength($this->version, $this->level);
     $maxbits = $maxwords * 8;
     if ($maxbits == $bits) {
         return 0;
     }
     if ($maxbits - $bits < 5) {
         return $bstream->appendNum($maxbits - $bits, 0);
     }
     $bits += 4;
     $words = (int) (($bits + 7) / 8);
     $padding = new QRbitstream();
     $ret = $padding->appendNum($words * 8 - $bits + 4, 0);
     if ($ret < 0) {
         return $ret;
     }
     $padlen = $maxwords - $words;
     if ($padlen > 0) {
         $padbuf = array();
         for ($i = 0; $i < $padlen; $i++) {
             $padbuf[$i] = $i & 1 ? 0x11 : 0xec;
         }
         $ret = $padding->appendBytes($padlen, $padbuf);
         if ($ret < 0) {
             return $ret;
         }
     }
     $ret = $bstream->append($padding);
     return $ret;
 }
Example #7
0
 public function eat8()
 {
     $la = QRspec::lengthIndicator(Constants::QR_MODE_AN, $this->input->getVersion());
     $ln = QRspec::lengthIndicator(Constants::QR_MODE_NUM, $this->input->getVersion());
     $p = 1;
     $dataStrLen = strlen($this->dataStr);
     while ($p < $dataStrLen) {
         $mode = $this->identifyMode($p);
         if ($mode == Constants::QR_MODE_KANJI) {
             break;
         }
         if ($mode == Constants::QR_MODE_NUM) {
             $q = $p;
             while (self::isdigitat($this->dataStr, $q)) {
                 $q++;
             }
             $dif = QRinput::estimateBitsMode8($p) + QRinput::estimateBitsModeNum($q - $p) + 4 + $ln - QRinput::estimateBitsMode8($q);
             // - 4 - l8
             if ($dif < 0) {
                 break;
             } else {
                 $p = $q;
             }
         } else {
             if ($mode == Constants::QR_MODE_AN) {
                 $q = $p;
                 while (self::isalnumat($this->dataStr, $q)) {
                     $q++;
                 }
                 $dif = QRinput::estimateBitsMode8($p) + QRinput::estimateBitsModeAn($q - $p) + 4 + $la - QRinput::estimateBitsMode8($q);
                 // - 4 - l8
                 if ($dif < 0) {
                     break;
                 } else {
                     $p = $q;
                 }
             } else {
                 $p++;
             }
         }
     }
     $run = $p;
     $ret = $this->input->append(Constants::QR_MODE_8, $run, str_split($this->dataStr));
     if ($ret < 0) {
         return -1;
     }
     return $run;
 }
Example #8
0
 public static function qrtest()
 {
     // Include only this file, remaining required files will be included from it
     if (!class_exists("qrstr", FALSE)) {
         require_once PATH_PHPQRCODE . "/qrlib.php";
     }
     //write code into file, Error corection lecer is lowest, L (one form: L,M,Q,H)
     //each code square will be 4x4 pixels (4x zoom)
     //code will have 2 code squares white boundary around
     if (1) {
         // Error correction level L : About 7% or less errors can be corrected.
         // Error correction level M : About 15% or less errors can be corrected.
         // Error correction level Q : About 25% or less errors can be corrected.
         // Error correction level H : About 30% or less errors can be corrected.
         $text = "http://www.nordita.org/guest/";
         $filetype = "png";
         // "png","svg","eps","txt"
         if (!in_array($filetype, array("png", "svg", "eps", "txt"))) {
             return FALSE;
         }
         $level = "H";
         // "L" (default) ,"M", "Q", "H"
         $pixelsize = 16;
         // pixel size of each code square
         $margin = 4;
         // boundary width in number of code squares
         $saveandprint = FALSE;
         // TRUE = save to file and display in browser; FALSE = save to file only; redundant if $outfile=FALSE
         $back_color = 0xffffff;
         $fore_color = 0x0;
         $cmyk = TRUE;
         $filename = "qrcode";
         $outfile = PATH_PHPQRCACHE . $filename . "." . $filetype;
         // full file path or FALSE (only output on screen)
         //$outfile      = FALSE;
         switch ($filetype) {
             case "png":
                 QRcode::png($text, $outfile, $level, $pixelsize, $margin, $saveandprint, $back_color, $fore_color);
                 break;
             case "eps":
                 QRcode::eps($text, $outfile, $level, $pixelsize, $margin, $saveandprint, $back_color, $fore_color, $cmyk);
                 break;
             case "svg":
                 QRcode::svg($text, $outfile, $level, $pixelsize, $margin, $saveandprint, $back_color, $fore_color);
                 break;
             case "txt":
                 QRcode::text($text, $outfile, $level, $pixelsize, $margin);
                 // black = "1", white = "0"
                 break;
         }
         // end switch
     }
     //same as above but outputs file directly into browser (with appr. header etc.)
     //all other settings are default
     //WARNING! it should be FIRST and ONLY output generated by script, otherwise
     //rest of output will land inside PNG binary, breaking it for sure
     if (0) {
         QRcode::png("PHP QR Code :)");
     }
     //show benchmark
     if (0) {
         QRtools::timeBenchmark();
     }
     //rebuild cache
     if (0) {
         QRtools::buildCache();
     }
     //code generated in text mode - as a binary table
     //then displayed out as HTML using Unicode block building chars :)
     if (0) {
         $qr = new QRencode();
         $tab = $qr->encode('PHP QR Code :)');
         QRspec::debug($tab, true);
     }
 }
Example #9
0
 public function eat8()
 {
     $la = QRspec::lengthIndicator(QR_MODE_AN, $this->input->getVersion());
     $ln = QRspec::lengthIndicator(QR_MODE_NUM, $this->input->getVersion());
     $p = 1;
     //$dataStrLen = strlen($this->dataStr);
     $dataStrLen = mb_strlen($this->dataStr, 'CP1251');
     $_str = $this->dataStr;
     $_p = 0;
     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) + QRinput::estimateBitsModeNum($q - $p) + 4 + $ln - QRinput::estimateBitsMode8($q);
             // - 4 - l8
             if ($dif < 0) {
                 break;
             } else {
                 $p = $q;
             }
         } else {
             if ($mode == QR_MODE_AN) {
                 $q = $p;
                 while (self::isalnumat($this->dataStr, $q)) {
                     $q++;
                 }
                 $dif = QRinput::estimateBitsMode8($p) + QRinput::estimateBitsModeAn($q - $p) + 6 + $la - QRinput::estimateBitsMode8($q);
                 // - 4 - l8
                 if ($dif < 0) {
                     break;
                 } else {
                     $p = $q;
                 }
             } else {
                 if (BX_UTF === true) {
                     $value = ord($_str[$p]);
                     if ($value > 127) {
                         if ($value >= 192 && $value <= 223) {
                             $s = 1;
                         } elseif ($value >= 224 && $value <= 239) {
                             $s = 2;
                         } elseif ($value >= 240 && $value <= 247) {
                             $s = 3;
                         }
                     } else {
                         $s = 0;
                     }
                     $p = $p + $s;
                     $_p = $_p + $s;
                 }
                 $p++;
             }
         }
     }
     $run = $p;
     $ret = $this->input->append(QR_MODE_8, $run, str_split($this->dataStr));
     if ($ret < 0) {
         return -1;
     }
     return $run - $_p;
 }
<?php

include 'phpqrcode/qrlib.php';
//QRcode::png('some othertext 1234', 'out.png', 'H');
$qr = new QRencode();
$tab = $qr->encode('PHP QR Code :)');
QRspec::debug($tab, true);
<?php

include '../lib/full/qrlib.php';
// now the fun begins, we use code generating features of library
$codeContents = 'Let see what the code structure looks like with a little bit bigger code';
$version = 0;
// will be autodetected
$eccLevel = QR_ECLEVEL_H;
$encodingHint = QR_MODE_8;
$caseSensitive = true;
$code = new QRcode();
$code->encodeString($codeContents, $version, $eccLevel, $encodingHint, $caseSensitive);
QRspec::debug($code->data, false);