public function renderQrCodeAction() { /** @var AccountInterface $account */ $account = $this->zourceAccount(); $oneTimePassword = new TOTP($account->getContact()->getFullName(), $this->tfaService->getSecret()); $oneTimePassword->setIssuer('Zource'); $oneTimePassword->setIssuerIncludedAsParameter(true); $oneTimePassword->setParameter('image', $this->url()->fromRoute('settings/security/tfa-image', [], ['force_canonical' => true])); $filePath = tempnam('data/tmp/', '2fa-qr-'); $renderer = new Png(); $renderer->setHeight(256); $renderer->setWidth(256); $writer = new Writer($renderer); $writer->writeFile($oneTimePassword->getProvisioningUri(true), $filePath); $response = new Stream(); $response->setCleanup(true); $response->setStream(fopen($filePath, 'rb')); $response->setStreamName($filePath); $headers = $response->getHeaders(); $headers->addHeaderLine('Content-Length', filesize($filePath)); $headers->addHeaderLine('Content-Type', 'image/png'); return $response; }
/** * Creates a margin around the QrCode * * @param int $margin The desired margin in pixels around the QrCode * @return $this */ public function margin($margin) { $this->writer->getRenderer()->setMargin($margin); return $this; }