Skip to content

Commit 3bd9eba

Browse files
committed
:octocat: allow returning the image resource
1 parent 5b75497 commit 3bd9eba

File tree

8 files changed

+77
-14
lines changed

8 files changed

+77
-14
lines changed

src/Output/QRFpdf.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,10 @@ protected function setModuleValues():void{
6565

6666
/**
6767
* @inheritDoc
68+
*
69+
* @return string|\FPDF
6870
*/
69-
public function dump(string $file = null):string{
71+
public function dump(string $file = null){
7072
$file = $file ?? $this->options->cachefile;
7173

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

9193
}
9294

95+
if($this->options->returnResource){
96+
return $fpdf;
97+
}
98+
9399
$pdfData = $fpdf->Output('S');
94100

95101
if($file !== null){

src/Output/QRImage.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,10 @@ protected function setModuleValues():void{
6565

6666
/**
6767
* @inheritDoc
68+
*
69+
* @return string|resource
6870
*/
69-
public function dump(string $file = null):string{
71+
public function dump(string $file = null){
7072
$this->image = imagecreatetruecolor($this->length, $this->length);
7173

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

91+
if($this->options->returnResource){
92+
return $this->image;
93+
}
94+
8995
$imageData = $this->dumpImage($file);
9096

9197
if((bool)$this->options->imageBase64){

src/Output/QRImagick.php

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
*/
2525
class QRImagick extends QROutputAbstract{
2626

27+
/**
28+
* @var \Imagick
29+
*/
30+
protected $imagick;
31+
2732
/**
2833
* @inheritDoc
2934
*/
@@ -45,19 +50,27 @@ protected function setModuleValues():void{
4550

4651
/**
4752
* @inheritDoc
53+
*
54+
* @return string|\Imagick
4855
*/
49-
public function dump(string $file = null):string{
50-
$file = $file ?? $this->options->cachefile;
51-
$imagick = new Imagick;
56+
public function dump(string $file = null){
57+
$file = $file ?? $this->options->cachefile;
58+
$this->imagick = new Imagick;
5259

53-
$imagick->newImage(
60+
$this->imagick->newImage(
5461
$this->length,
5562
$this->length,
5663
new ImagickPixel($this->options->imagickBG ?? 'transparent'),
5764
$this->options->imagickFormat
5865
);
5966

60-
$imageData = $this->drawImage($imagick);
67+
$this->drawImage();
68+
69+
if($this->options->returnResource){
70+
return $this->imagick;
71+
}
72+
73+
$imageData = $this->imagick->getImageBlob();
6174

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

6982
/**
70-
* @param \Imagick $imagick
71-
*
72-
* @return string
83+
* @return void
7384
*/
74-
protected function drawImage(Imagick $imagick):string{
85+
protected function drawImage():void{
7586
$draw = new ImagickDraw;
7687

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

91-
$imagick->drawImage($draw);
92-
93-
return (string)$imagick;
102+
$this->imagick->drawImage($draw);
94103
}
95104

96105
}

src/QROptions.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
* @property string $markupDark
4343
* @property string $markupLight
4444
*
45+
* @property bool $returnResource
4546
* @property bool $imageBase64
4647
* @property bool $imageTransparent
4748
* @property array $imageTransparencyBG

src/QROptionsTrait.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,22 @@ trait QROptionsTrait{
188188
*/
189189
protected $markupLight = '#fff';
190190

191+
/**
192+
* Return the image resource instead of a render if applicable.
193+
* This option overrides other output options, such as $cachefile and $imageBase64.
194+
*
195+
* Supported by the following modules:
196+
*
197+
* - QRImage: resource
198+
* - QRImagick: Imagick
199+
* - QRFpdf: FPDF
200+
*
201+
* @see \chillerlan\QRCode\Output\QROutputInterface::dump()
202+
*
203+
* @var bool
204+
*/
205+
protected $returnResource = false;
206+
191207
/**
192208
* toggle base64 or raw image data
193209
*

tests/Output/QRFpdfTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,12 @@ public function testRenderImage():void{
7171
$this::assertSame($expected, $actual);
7272
}
7373

74+
public function testOutputGetResource():void{
75+
$this->options->returnResource = true;
76+
77+
$this->setOutputInterface();
78+
79+
$this::assertInstanceOf(FPDF::class, $this->outputInterface->dump());
80+
}
81+
7482
}

tests/Output/QRImageTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,12 @@ public function testSetModuleValues(){
5858
$this->assertTrue(true); // tricking the code coverage
5959
}
6060

61+
public function testOutputGetResource():void{
62+
$this->options->returnResource = true;
63+
64+
$this->setOutputInterface();
65+
66+
$this::assertIsResource($this->outputInterface->dump());
67+
}
68+
6169
}

tests/Output/QRImagickTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
namespace chillerlan\QRCodeTest\Output;
1414

15+
use Imagick;
1516
use chillerlan\QRCode\{QRCode, Output\QRImagick};
1617

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

56+
public function testOutputGetResource():void{
57+
$this->options->returnResource = true;
58+
59+
$this->setOutputInterface();
60+
61+
$this::assertInstanceOf(Imagick::class, $this->outputInterface->dump());
62+
}
63+
5564
}

0 commit comments

Comments
 (0)