public function encodeMask(wechat_qrcode_QRinput $input, $mask) { if ($input->getVersion() < 0 || $input->getVersion() > QRSPEC_VERSION_MAX) { throw new Exception('wrong version'); } if ($input->getErrorCorrectionLevel() > QR_ECLEVEL_H) { throw new Exception('wrong level'); } $raw = new wechat_qrcode_QRrawcode($input); wechat_qrcode_QRtools::markTime('after_raw'); $version = $raw->version; $width = wechat_qrcode_QRspec::getWidth($version); $frame = wechat_qrcode_QRspec::newFrame($version); $filler = new wechat_qrcode_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; } } wechat_qrcode_QRtools::markTime('after_filler'); unset($raw); // remainder bits $j = wechat_qrcode_QRspec::getRemainder($version); for ($i = 0; $i < $j; $i++) { $addr = $filler->next(); $filler->setFrameAt($addr, 0x2); } $frame = $filler->frame; unset($filler); // masking $maskObj = new wechat_qrcode_QRmask(); if ($mask < 0) { if (QR_FIND_BEST_MASK) { $masked = $maskObj->mask($width, $frame, $input->getErrorCorrectionLevel()); } else { $masked = $maskObj->makeMask($width, $frame, intval(QR_DEFAULT_MASK) % 8, $input->getErrorCorrectionLevel()); } } else { $masked = $maskObj->makeMask($width, $frame, $mask, $input->getErrorCorrectionLevel()); } if ($masked == NULL) { return NULL; } wechat_qrcode_QRtools::markTime('after_mask'); $this->version = $version; $this->width = $width; $this->data = $masked; return $this; }
public static function buildCache() { wechat_qrcode_QRtools::markTime('before_build_cache'); $mask = new wechat_qrcode_QRmask(); for ($a = 1; $a <= QRSPEC_VERSION_MAX; $a++) { $frame = wechat_qrcode_QRspec::newFrame($a); if (QR_IMAGE) { $fileName = QR_CACHE_DIR . 'frame_' . $a . '.png'; wechat_qrcode_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); } } wechat_qrcode_QRtools::markTime('after_build_cache'); }
public function encodePNG($intext, $outfile = false, $saveandprint = false) { try { ob_start(); $tab = $this->encode($intext); $err = ob_get_contents(); ob_end_clean(); if ($err != '') { wechat_qrcode_QRtools::log($outfile, $err); } $maxSize = (int) (QR_PNG_MAXIMUM_SIZE / (count($tab) + 2 * $this->margin)); wechat_qrcode_QRimage::png($tab, $outfile, min(max(1, $this->size), $maxSize), $this->margin, $saveandprint); } catch (Exception $e) { wechat_qrcode_QRtools::log($outfile, $e->getMessage()); } }