:octocat: allow returning the image resource

This commit is contained in:
codemasher
2020-11-18 16:57:34 +01:00
parent 5b75497101
commit 3bd9eba64f
8 changed files with 77 additions and 14 deletions
+7 -1
View File
@@ -65,8 +65,10 @@ class QRFpdf extends QROutputAbstract{
/**
* @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]);
@@ -90,6 +92,10 @@ class QRFpdf extends QROutputAbstract{
}
if($this->options->returnResource){
return $fpdf;
}
$pdfData = $fpdf->Output('S');
if($file !== null){
+7 -1
View File
@@ -65,8 +65,10 @@ class QRImage extends QROutputAbstract{
/**
* @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
@@ -86,6 +88,10 @@ class QRImage extends QROutputAbstract{
}
}
if($this->options->returnResource){
return $this->image;
}
$imageData = $this->dumpImage($file);
if((bool)$this->options->imageBase64){
+21 -12
View File
@@ -24,6 +24,11 @@ use function is_string;
*/
class QRImagick extends QROutputAbstract{
/**
* @var \Imagick
*/
protected $imagick;
/**
* @inheritDoc
*/
@@ -45,19 +50,27 @@ class QRImagick extends QROutputAbstract{
/**
* @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);
@@ -67,11 +80,9 @@ class QRImagick extends QROutputAbstract{
}
/**
* @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){
@@ -88,9 +99,7 @@ class QRImagick extends QROutputAbstract{
}
}
$imagick->drawImage($draw);
return (string)$imagick;
$this->imagick->drawImage($draw);
}
}
+1
View File
@@ -42,6 +42,7 @@ use chillerlan\Settings\SettingsContainerAbstract;
* @property string $markupDark
* @property string $markupLight
*
* @property bool $returnResource
* @property bool $imageBase64
* @property bool $imageTransparent
* @property array $imageTransparencyBG
+16
View File
@@ -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
*
+8
View File
@@ -71,4 +71,12 @@ class QRFpdfTest extends QROutputTestAbstract{
$this::assertSame($expected, $actual);
}
public function testOutputGetResource():void{
$this->options->returnResource = true;
$this->setOutputInterface();
$this::assertInstanceOf(FPDF::class, $this->outputInterface->dump());
}
}
+8
View File
@@ -58,4 +58,12 @@ class QRImageTest extends QROutputTestAbstract{
$this->assertTrue(true); // tricking the code coverage
}
public function testOutputGetResource():void{
$this->options->returnResource = true;
$this->setOutputInterface();
$this::assertIsResource($this->outputInterface->dump());
}
}
+9
View File
@@ -12,6 +12,7 @@
namespace chillerlan\QRCodeTest\Output;
use Imagick;
use chillerlan\QRCode\{QRCode, Output\QRImagick};
class QRImagickTest extends QROutputTestAbstract{
@@ -52,4 +53,12 @@ class QRImagickTest extends QROutputTestAbstract{
$this->assertTrue(true); // tricking the code coverage
}
public function testOutputGetResource():void{
$this->options->returnResource = true;
$this->setOutputInterface();
$this::assertInstanceOf(Imagick::class, $this->outputInterface->dump());
}
}