Skip to content

Commit

Permalink
:octocat: allow base64 encoding for SVG and PDF output
Browse files Browse the repository at this point in the history
  • Loading branch information
codemasher committed Nov 18, 2020
1 parent 3bd9eba commit 6ecb09f
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/Output/QRFpdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ public function dump(string $file = null){
$this->saveToFile($pdfData, $file);
}

if($this->options->imageBase64){
$pdfData = sprintf('data:application/pdf;base64,%s', base64_encode($pdfData));
}

return $pdfData;
}

Expand Down
4 changes: 4 additions & 0 deletions src/Output/QRMarkup.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ protected function svg():string{
return '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">'.$this->options->eol.$svg;
}

if($this->options->imageBase64){
$svg = sprintf('data:image/svg+xml;base64,%s', base64_encode($svg));
}

return $svg;
}

Expand Down
3 changes: 2 additions & 1 deletion tests/Output/QRFpdfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public function testSetModuleValues():void{
public function testRenderImage():void{
$type = QRCode::OUTPUT_FPDF;

$this->options->outputType = $type;
$this->options->outputType = $type;
$this->options->imageBase64 = false;
$this->outputInterface->dump($this::cachefile.$type);

// substr() to avoid CreationDate
Expand Down
4 changes: 3 additions & 1 deletion tests/Output/QRMarkupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public function testMarkupOutputFile($type){
* @param $type
*/
public function testMarkupOutput($type){
$this->options->outputType = $type;
$this->options->imageBase64 = false;
$this->options->outputType = $type;
$this->setOutputInterface();

$expected = explode($this->options->eol, file_get_contents($this::cachefile.$type));
Expand All @@ -62,6 +63,7 @@ public function testMarkupOutput($type){

public function testSetModuleValues(){

$this->options->imageBase64 = false;
$this->options->moduleValues = [
// data
1024 => '#4A6000',
Expand Down
2 changes: 1 addition & 1 deletion tests/QRCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function typeDataProvider(){
'png' => [QRCode::OUTPUT_IMAGE_PNG, 'data:image/png;base64,'],
'gif' => [QRCode::OUTPUT_IMAGE_GIF, 'data:image/gif;base64,'],
'jpg' => [QRCode::OUTPUT_IMAGE_JPG, 'data:image/jpg;base64,'],
'svg' => [QRCode::OUTPUT_MARKUP_SVG, '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" class="qr-svg " style="width: 100%; height: auto;" viewBox="'],
'svg' => [QRCode::OUTPUT_MARKUP_SVG, 'data:image/svg+xml;base64,'],
'html' => [QRCode::OUTPUT_MARKUP_HTML, '<div><span style="background:'],
'text' => [QRCode::OUTPUT_STRING_TEXT, '⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕'.PHP_EOL],
'json' => [QRCode::OUTPUT_STRING_JSON, '[[18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18],'],
Expand Down

0 comments on commit 6ecb09f

Please sign in to comment.