file = WBClass::create('WBFile');
$this->config = WBClass::create('WBConfig');
$this->config->load('ute/qrcode/config', true);
}
/**
* Set QRCode Text
*
* @param string text for QR code
*/
public function setText($text)
{
$text = trim($text);
if ($text == $this->text) {
return;
}
$this->file->unlink();
$this->text = $text;
}
/**
* Render QRCode Image and Return File
*
* @return WBFile
*/
public function getImage()
{
if ($this->file->isFile()) {
return $this->file;
}
$renderer = strtolower($this->config->get('qrcode/renderer', 'command'));
$config = $this->config->get('qrcode/config/' . $renderer, array());
$this->file->tempnam('qrcode');
switch ($renderer) {
case 'webservice':
$this->renderFromWebService($config);
break;
default:
case 'command':
$this->renderFromCommand($config);
break;
}
return $this->file;
}
/**
* Use Command
*
* Use Command-Line-Tool to render QR-Code.
* @param array
*/
private function renderFromCommand($config)
{
$config = array_merge(array(
'size' => 800,
'blackonwhite' => 1
), $config);
$config['blackonwhite'] = intval($config['blackonwhite']);
$config['size'] = intval($config['size']);
$config['size'] = max(100, min(2000, $config['size']));
$cmd = array('qrcode');
if (1 > $config['blackonwhite']) {
$cmd[] = '-i';
}
$cmd[] = '-s ' . $config['size'];
$cmd[] = '%s >%s';
$cmd = implode(' ', $cmd);
$cmd = sprintf($cmd, $this->text, $this->file->realpath());
exec($cmd, $out, $ret);
if (0 < $ret) {
echo "FAILED " . $cmd . "
\n";
implode("
\n", $out);
}
}
/**
* Use API
*
* Use Web-service to render QR-Code.
* @param array
*/
private function renderFromWebService($config)
{
$config = array_merge(array(
'size' => 800,
'url' => 'https://api.qrserver.com/v1/create-qr-code/'
), $config);
$config['size'] = intval($config['size']);
$config['size'] = max(100, min(2000, $config['size']));
$text = urlencode($this->text);
$png = file_get_contents(sprintf('%s?size=%sx%s&data=%s', $config['url'], $config['size'], $config['size'] ,$text));
file_put_contents($this->file->realpath(), $png);
}
}