Skip to content

Commit

Permalink
:octocat: allow returning the image resource
Browse files Browse the repository at this point in the history
  • Loading branch information
codemasher committed Nov 18, 2020
1 parent 5b75497 commit 3bd9eba
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 14 deletions.
8 changes: 7 additions & 1 deletion src/Output/QRFpdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ protected function setModuleValues():void{

/**
* @inheritDoc
*
* @return string|\FPDF
*/
public function dump(string $file = null):string{
public function dump(string $file = null){
$file = $file ?? $this->options->cachefile;

$fpdf = new FPDF('P', $this->options->fpdfMeasureUnit, [$this->length, $this->length]);
Expand All @@ -90,6 +92,10 @@ public function dump(string $file = null):string{

}

if($this->options->returnResource){
return $fpdf;
}

$pdfData = $fpdf->Output('S');

if($file !== null){
Expand Down
8 changes: 7 additions & 1 deletion src/Output/QRImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ protected function setModuleValues():void{

/**
* @inheritDoc
*
* @return string|resource
*/
public function dump(string $file = null):string{
public function dump(string $file = null){
$this->image = imagecreatetruecolor($this->length, $this->length);

// avoid: Indirect modification of overloaded property $imageTransparencyBG has no effect
Expand All @@ -86,6 +88,10 @@ public function dump(string $file = null):string{
}
}

if($this->options->returnResource){
return $this->image;
}

$imageData = $this->dumpImage($file);

if((bool)$this->options->imageBase64){
Expand Down
33 changes: 21 additions & 12 deletions src/Output/QRImagick.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
*/
class QRImagick extends QROutputAbstract{

/**
* @var \Imagick
*/
protected $imagick;

/**
* @inheritDoc
*/
Expand All @@ -45,19 +50,27 @@ protected function setModuleValues():void{

/**
* @inheritDoc
*
* @return string|\Imagick
*/
public function dump(string $file = null):string{
$file = $file ?? $this->options->cachefile;
$imagick = new Imagick;
public function dump(string $file = null){
$file = $file ?? $this->options->cachefile;
$this->imagick = new Imagick;

$imagick->newImage(
$this->imagick->newImage(
$this->length,
$this->length,
new ImagickPixel($this->options->imagickBG ?? 'transparent'),
$this->options->imagickFormat
);

$imageData = $this->drawImage($imagick);
$this->drawImage();

if($this->options->returnResource){
return $this->imagick;
}

$imageData = $this->imagick->getImageBlob();

if($file !== null){
$this->saveToFile($imageData, $file);
Expand All @@ -67,11 +80,9 @@ public function dump(string $file = null):string{
}

/**
* @param \Imagick $imagick
*
* @return string
* @return void
*/
protected function drawImage(Imagick $imagick):string{
protected function drawImage():void{
$draw = new ImagickDraw;

foreach($this->matrix->matrix() as $y => $row){
Expand All @@ -88,9 +99,7 @@ protected function drawImage(Imagick $imagick):string{
}
}

$imagick->drawImage($draw);

return (string)$imagick;
$this->imagick->drawImage($draw);
}

}
1 change: 1 addition & 0 deletions src/QROptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
* @property string $markupDark
* @property string $markupLight
*
* @property bool $returnResource
* @property bool $imageBase64
* @property bool $imageTransparent
* @property array $imageTransparencyBG
Expand Down
16 changes: 16 additions & 0 deletions src/QROptionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,22 @@ trait QROptionsTrait{
*/
protected $markupLight = '#fff';

/**
* Return the image resource instead of a render if applicable.
* This option overrides other output options, such as $cachefile and $imageBase64.
*
* Supported by the following modules:
*
* - QRImage: resource
* - QRImagick: Imagick
* - QRFpdf: FPDF
*
* @see \chillerlan\QRCode\Output\QROutputInterface::dump()
*
* @var bool
*/
protected $returnResource = false;

/**
* toggle base64 or raw image data
*
Expand Down
8 changes: 8 additions & 0 deletions tests/Output/QRFpdfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,12 @@ public function testRenderImage():void{
$this::assertSame($expected, $actual);
}

public function testOutputGetResource():void{
$this->options->returnResource = true;

$this->setOutputInterface();

$this::assertInstanceOf(FPDF::class, $this->outputInterface->dump());
}

}
8 changes: 8 additions & 0 deletions tests/Output/QRImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,12 @@ public function testSetModuleValues(){
$this->assertTrue(true); // tricking the code coverage
}

public function testOutputGetResource():void{
$this->options->returnResource = true;

$this->setOutputInterface();

$this::assertIsResource($this->outputInterface->dump());
}

}
9 changes: 9 additions & 0 deletions tests/Output/QRImagickTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace chillerlan\QRCodeTest\Output;

use Imagick;
use chillerlan\QRCode\{QRCode, Output\QRImagick};

class QRImagickTest extends QROutputTestAbstract{
Expand Down Expand Up @@ -52,4 +53,12 @@ public function testSetModuleValues(){
$this->assertTrue(true); // tricking the code coverage
}

public function testOutputGetResource():void{
$this->options->returnResource = true;

$this->setOutputInterface();

$this::assertInstanceOf(Imagick::class, $this->outputInterface->dump());
}

}

0 comments on commit 3bd9eba

Please sign in to comment.