public static function createQRCode($file_path) { // get server info $protocol = isset($_SERVER['HTTPS']) && strcasecmp('off', $_SERVER['HTTPS']) !== 0; $hostname = $_SERVER['SERVER_NAME']; $port = $_SERVER['SERVER_PORT']; // Create folder $date = date('Ymd'); $upload_path = '/upload/qrcode/' . $date . '/'; $dir = public_path() . $upload_path; if (!file_exists($dir)) { mkdir($dir, 0777, true); } // QR Code $qrCode = new QrCode(); $qrCode->setText($protocol . $hostname . ':' . $port . $file_path); $image = $qrCode->get(); $fileName = md5(date('YmdHis')) . '.png'; // Save QR code to image $qrCode->render($dir . $fileName); // return $upload_path . $fileName; }
<?php include "vendor/autoload.php"; header('Content-Type: image/jpeg'); use Endroid\QrCode\QrCode; $qrCode = new QrCode(); $qrCode->setText("Life is too short to be generating QR codes"); $qrCode->setSize(300); $qrCode->setPadding(10); $qrCode->render(null, 'jpg');
<?php /** * Implement QRCode generator * * */ use Endroid\QrCode\QrCode; header('Content-Type: image/png'); if (!hasCache($file)) { $obj = file_get_contents($file); $qrCode = new QrCode(); $qrCode->setText($obj)->setSize(300)->setPadding(10)->setErrorCorrection('high')->setForegroundColor(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0))->setBackgroundColor(array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 0))->setLabelFontSize(10)->setImageType(QrCode::IMAGE_TYPE_PNG); ob_start(); // output image $qrCode->render(); $content = ob_get_contents(); ob_end_clean(); generateCache($file, $content); } outputCache($file);
public function render($outputFile) { $qrCode = new QrCode(); $qrCode->setText($this->getUrl()); $qrCode->setSize($this->configuration->getQrHeight()); $qrCode->setPadding($this->configuration->getQrPadding()); $qrCode->render($outputFile); }
/** * @depends testGeneratesUrl */ public function testRenders() { $this->storage->expects($this->once())->method('getSessionNonce')->will($this->returnValue('storednut')); $this->config->expects($this->any())->method('getDomain')->will($this->returnValue('example.com')); $this->config->expects($this->any())->method('getAuthenticationPath')->will($this->returnValue('/sqrl')); $this->config->expects($this->any())->method('getSecure')->will($this->returnValue(true)); $this->config->expects($this->any())->method('getQrHeight')->will($this->returnValue(30)); $this->config->expects($this->any())->method('getQrPadding')->will($this->returnValue(1)); $obj = new SQRLGenerate($this->config, $this->storage); $expected = new QrCode(); $expected->setText('sqrl://example.com/sqrl?nut=storednut'); $expected->setSize(30); $expected->setPadding(1); $expected->render(dirname(__FILE__) . '/expected.png'); $obj->render(dirname(__FILE__) . '/test.png'); $this->assertEquals(file_get_contents(dirname(__FILE__) . '/expected.png'), file_get_contents(dirname(__FILE__) . '/test.png')); unlink(dirname(__FILE__) . '/expected.png'); unlink(dirname(__FILE__) . '/test.png'); }
public function qrAction() { // 生成二维码 $username = urlencode('账号:') . '*****@*****.**'; $secretKey = 'DPI45HCE'; $url = "otpauth://totp/{$username}?secret={$secretKey}&issuer=" . urlencode('XXTIME.COM'); $qrCode = new QrCode(); $qrCode->setText($url)->setSize(200)->setPadding(10)->setErrorCorrection('low')->setForegroundColor(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0))->setBackgroundColor(array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 0))->setImageType(QrCode::IMAGE_TYPE_PNG); header('Content-Type: ' . $qrCode->getContentType()); $qrCode->render(); exit; // 验证 $totp = new PHPGangsta_GoogleAuthenticator(); $secretKey = $totp->createSecret(32); $oneCode = $totp->getCode($secretKey); $checkResult = $totp->verifyCode($secretKey, $oneCode, 2); // 2 = 2*30sec clock tolerance if ($checkResult) { echo 'OK'; dd($secret, $oneCode); } else { echo 'FAILED'; } exit; }
public function qrAction() { $username = $this->session->get('username'); if (!$username) { exit('No Permission'); } // 二维码生成与验证 $otp = new PHPGangsta_GoogleAuthenticator(); if ($_POST) { // 验证 开启二次验证是否正确 $code = $this->request->get('code', 'int'); $secret_key = $this->session->get('secret_key'); $checkResult = $otp->verifyCode($secret_key, $code, 2); if (!$checkResult) { Utils::outputJSON(array('code' => 0, 'message' => 'Verify Success')); $user_id = $this->session->get('user_id'); $this->authModel->setOTPKey($user_id, $secret_key); } Utils::outputJSON(array('code' => 1, 'message' => 'Verify Failed')); } $secret_key = $otp->createSecret(32); $this->session->set('secret_key', $secret_key); $username = urlencode('账号:') . $username; $url = "otpauth://totp/{$username}?secret={$secret_key}&issuer=" . urlencode('XXTIME.COM'); $qrCode = new QrCode(); $qrCode->setText($url)->setSize(200)->setPadding(10)->setErrorCorrection('low')->setForegroundColor(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0))->setBackgroundColor(array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 0))->setImageType(QrCode::IMAGE_TYPE_PNG); header('Content-Type: ' . $qrCode->getContentType()); $qrCode->render(); exit; }
public function sendTicketMail(SendTicketMailCommand $command) { $qrCode = new QrCode(); $qrCode->setText(rtrim($command->schemeAndHost, '/') . $this->router->generate('bcrmweb_event_checkin', array('id' => $command->ticket->getId(), 'code' => $command->ticket->getCode()))); $qrCode->setSize(300); $qrCode->setPadding(10); $qrfile = tempnam(sys_get_temp_dir(), 'qrcode-') . '.png'; $qrCode->render($qrfile); $emailCommand = new SendTemplateMailCommand(); $emailCommand->email = $command->ticket->getEmail(); $emailCommand->template = 'Ticket'; $emailCommand->templateData = array('ticket' => $command->ticket, 'event' => $command->event, 'cancel_link' => rtrim($command->schemeAndHost, '/') . $this->router->generate('bcrmweb_event_cancel_ticket', array('id' => $command->ticket->getId(), 'code' => $command->ticket->getCode()))); $emailCommand->image = $qrfile; $emailCommand->format = 'text/html'; $this->commandBus->handle($emailCommand); $event = new TicketMailSentEvent(); $event->ticket = $command->ticket; $this->eventMessageBus->publish($event); }