Skip to content

Instantly share code, notes, and snippets.

@jakemcgraw
Created May 7, 2014 03:33
Show Gist options
  • Save jakemcgraw/9088105157734961560d to your computer and use it in GitHub Desktop.
Save jakemcgraw/9088105157734961560d to your computer and use it in GitHub Desktop.
<?php
/**
* BaconQrCode
*
* @link http://github.com/Bacon/BaconQrCode For the canonical source repository
* @copyright 2014 Jake McGraw
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
*/
namespace BaconQrCode\Renderer\Image\Decorator;
use BaconQrCode\Encoder\QrCode;
use BaconQrCode\Renderer\Image\RendererInterface;
/**
* Text decorator for QR code.
*/
class Text implements DecoratorInterface
{
const POSITION_TOP = 'top';
const POSITION_BOTTOM = 'bottom';
/**
* Text to render on top of QR code
*
* @var string
**/
protected $topText;
/**
* Text to render on bottom of QR code
*
* @var string
**/
protected $bottomText;
/**
* Set text to display at a given position
*
* @param string $text Text string to set
* @param string $position Where to position text on QR code
* @return Text
**/
public function setText($text, $position = self::POSITION_TOP)
{
switch($position) {
case self::POSITION_TOP:
$this->topText = $text;
break;
case self::POSITION_BOTTOM:
$this->bottomText = $text;
break;
}
return $this;
}
/**
* Get text at given position
*
* @param string $position Position of QR code text
* @return Text
**/
public function getText($position = self::POSITION_TOP)
{
switch($position) {
case self::POSITION_TOP:
return $this->topText;
case self::POSITION_BOTTOM:
return $this->bottomText;
}
return null;
}
/**
* preProcess(): defined by DecoratorInterface.
*
* @see DecoratorInterface::preProcess()
* @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 preProcess(
QrCode $qrCode,
RendererInterface $renderer,
$outputWidth,
$outputHeight,
$leftPadding,
$topPadding,
$multiple
) {
// do nothing
}
/**
* 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
) {
$fontHeight = 10;
$foregroundColor = $renderer->getForegroundColor()->getGray();
if (!empty($this->topText)) {
$newHeight = $outputHeight + $fontHeight;
$qrCodeWithText = imagecreatetruecolor($outputWidth, $newHeight);
imagecopyresized($qrCodeWithText, $renderer->image, 0, $fontHeight, 0, 0, $outputWidth, $newHeight, $outputWidth, $newHeight);
$white = imagecolorallocate($qrCodeWithText, 255, 255, 255);
imagefill($qrCodeWithText, 0, 0, $white);
imagestring($qrCodeWithText, 3, $leftPadding, 3, $this->topText, $foregroundColor);
$renderer->image = $qrCodeWithText;
$outputHeight = $newHeight;
}
if (!empty($this->bottomText)) {
if (strpos($this->bottomText, "\n") !== false) {
}
$newHeight = $outputHeight + $fontHeight + 3;
$qrCodeWithText = imagecreatetruecolor($outputWidth, $newHeight);
$white = imagecolorallocate($qrCodeWithText, 255, 255, 255);
imagefill($qrCodeWithText, 0, 0, $white);
imagecopyresized($qrCodeWithText, $renderer->image, 0, 0, 0, 0, $outputWidth, $outputHeight, $outputWidth, $outputHeight);
imagestring($qrCodeWithText, 3, $leftPadding, $outputHeight - $fontHeight, $this->bottomText, $foregroundColor);
$renderer->image = $qrCodeWithText;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment