Ejemplo n.º 1
0
 /**
  * render(): defined by RendererInterface.
  *
  * @see    RendererInterface::render()
  * @param  QrCode $qrCode
  * @return string
  */
 public function render(QrCode $qrCode)
 {
     $input = $qrCode->getMatrix();
     $inputWidth = $input->getWidth();
     $inputHeight = $input->getHeight();
     $qrWidth = $inputWidth + ($this->getMargin() << 1);
     $qrHeight = $inputHeight + ($this->getMargin() << 1);
     $outputWidth = max($this->getWidth(), $qrWidth);
     $outputHeight = max($this->getHeight(), $qrHeight);
     $multiple = (int) min($outputWidth / $qrWidth, $outputHeight / $qrHeight);
     if ($this->shouldRoundDimensions()) {
         $outputWidth -= $outputWidth % $multiple;
         $outputHeight -= $outputHeight % $multiple;
     }
     // Padding includes both the quiet zone and the extra white pixels to
     // accommodate the requested dimensions. For example, if input is 25x25
     // the QR will be 33x33 including the quiet zone. If the requested size
     // is 200x160, the multiple will be 4, for a QR of 132x132. These will
     // handle all the padding from 100x100 (the actual QR) up to 200x160.
     $leftPadding = (int) (($outputWidth - $inputWidth * $multiple) / 2);
     $topPadding = (int) (($outputHeight - $inputHeight * $multiple) / 2);
     // Store calculated parameters
     $this->finalWidth = $outputWidth;
     $this->finalHeight = $outputHeight;
     $this->blockSize = $multiple;
     $this->init();
     $this->addColor('background', $this->getBackgroundColor());
     $this->addColor('foreground', $this->getForegroundColor());
     $this->drawBackground('background');
     foreach ($this->decorators as $decorator) {
         $decorator->preProcess($qrCode, $this, $outputWidth, $outputHeight, $leftPadding, $topPadding, $multiple);
     }
     for ($inputY = 0, $outputY = $topPadding; $inputY < $inputHeight; $inputY++, $outputY += $multiple) {
         for ($inputX = 0, $outputX = $leftPadding; $inputX < $inputWidth; $inputX++, $outputX += $multiple) {
             if ($input->get($inputX, $inputY) === 1) {
                 $this->drawBlock($outputX, $outputY, 'foreground');
             }
         }
     }
     foreach ($this->decorators as $decorator) {
         $decorator->postProcess($qrCode, $this, $outputWidth, $outputHeight, $leftPadding, $topPadding, $multiple);
     }
     return $this->getByteStream();
 }
Ejemplo n.º 2
0
 /**
  * postProcess(): defined by DecoratorInterface.
  *
  * @see    DecoratorInterface::postProcess()
  *
  * @param  QrCode            $qrCode
  * @param  RendererInterface $renderer
  * @param  integer           $outputWidth
  * @param  integer           $outputHeight
  * @param  integer           $leftPadding
  * @param  integer           $topPadding
  * @param  integer           $multiple
  * @return void
  */
 public function postProcess(QrCode $qrCode, RendererInterface $renderer, $outputWidth, $outputHeight, $leftPadding, $topPadding, $multiple)
 {
     $matrix = $qrCode->getMatrix();
     $positions = array(array(0, 0), array($matrix->getWidth() - 7, 0), array(0, $matrix->getHeight() - 7));
     $renderer->addColor('finder-outer', $this->getOuterColor());
     $renderer->addColor('finder-inner', $this->getInnerColor());
     foreach (self::$outerPositionDetectionPattern as $y => $row) {
         foreach ($row as $x => $isOuterSet) {
             $isInnerSet = self::$innerPositionDetectionPattern[$y][$x];
             if ($isOuterSet) {
                 foreach ($positions as $position) {
                     $renderer->drawBlock($leftPadding + $x * $multiple + $position[0] * $multiple, $topPadding + $y * $multiple + $position[1] * $multiple, 'finder-outer');
                 }
             }
             if ($isInnerSet) {
                 foreach ($positions as $position) {
                     $renderer->drawBlock($leftPadding + $x * $multiple + $position[0] * $multiple, $topPadding + $y * $multiple + $position[1] * $multiple, 'finder-inner');
                 }
             }
         }
     }
 }
Ejemplo n.º 3
0
 /**
  * render(): defined by RendererInterface.
  *
  * @see    RendererInterface::render()
  * @param  QrCode $qrCode
  * @return string
  */
 public function render(QrCode $qrCode)
 {
     $result = '';
     $matrix = $qrCode->getMatrix();
     $width = $matrix->getWidth();
     // Top margin
     for ($x = 0; $x < $this->margin; $x++) {
         $result .= str_repeat($this->emptyBlock, $width + 2 * $this->margin) . "\n";
     }
     // Body
     $array = $matrix->getArray();
     foreach ($array as $row) {
         $result .= str_repeat($this->emptyBlock, $this->margin);
         // left margin
         foreach ($row as $byte) {
             $result .= $byte ? $this->fullBlock : $this->emptyBlock;
         }
         $result .= str_repeat($this->emptyBlock, $this->margin);
         // right margin
         $result .= "\n";
     }
     // Bottom margin
     for ($x = 0; $x < $this->margin; $x++) {
         $result .= str_repeat($this->emptyBlock, $width + 2 * $this->margin) . "\n";
     }
     return $result;
 }