:octocat: allow returning the image resource

This commit is contained in:
codemasher
2020-11-16 14:09:50 +01:00
parent 2640d2f30c
commit e437956429
5 changed files with 49 additions and 12 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 ??= $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){
+8 -2
View File
@@ -47,7 +47,7 @@ class QRImage extends QROutputAbstract{
* The GD image resource
*
* @see imagecreatetruecolor()
* @var resource
* @var resource|\GdImage
*/
protected $image;
@@ -88,8 +88,10 @@ class QRImage extends QROutputAbstract{
/**
* @inheritDoc
*
* @return string|\GdImage
*/
public function dump(string $file = null):string{
public function dump(string $file = null){
$file ??= $this->options->cachefile;
$this->image = imagecreatetruecolor($this->length, $this->length);
@@ -111,6 +113,10 @@ class QRImage extends QROutputAbstract{
}
}
if($this->options->returnResource){
return $this->image;
}
$imageData = $this->dumpImage();
if($file !== null){
+16 -8
View File
@@ -29,6 +29,8 @@ use function extension_loaded, is_string;
*/
class QRImagick extends QROutputAbstract{
protected Imagick $imagick;
/**
* @inheritDoc
*/
@@ -62,19 +64,27 @@ class QRImagick extends QROutputAbstract{
/**
* @inheritDoc
*
* @return string|\Imagick
*/
public function dump(string $file = null):string{
public function dump(string $file = null){
$file ??= $this->options->cachefile;
$imagick = new Imagick;
$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);
@@ -86,7 +96,7 @@ class QRImagick extends QROutputAbstract{
/**
* Creates the QR image via ImagickDraw
*/
protected function drawImage(Imagick $imagick):string{
protected function drawImage():void{
$draw = new ImagickDraw;
foreach($this->matrix->matrix() as $y => $row){
@@ -103,9 +113,7 @@ class QRImagick extends QROutputAbstract{
}
}
$imagick->drawImage($draw);
return (string)$imagick;
$this->imagick->drawImage($draw);
}
}
+1
View File
@@ -38,6 +38,7 @@ use chillerlan\Settings\SettingsContainerAbstract;
* @property string $textLight
* @property string $markupDark
* @property string $markupLight
* @property bool $returnResource
* @property bool $imageBase64
* @property bool $imageTransparent
* @property array $imageTransparencyBG
+17 -1
View File
@@ -156,6 +156,22 @@ trait QROptionsTrait{
*/
protected string $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 (PHP < 8), GdImage
* - QRImagick: Imagick
* - QRFpdf: FPDF
*
* @see \chillerlan\QRCode\Output\QROutputInterface::dump()
*
* @var bool
*/
protected bool $returnResource = false;
/**
* toggle base64 or raw image data
*/
@@ -186,7 +202,7 @@ trait QROptionsTrait{
/**
* Imagick output format
*
* @see Imagick::setType()
* @see \Imagick::setType()
*/
protected string $imagickFormat = 'png';