From a5486e54827da62cc02f7e8336fe068a2997cc5e Mon Sep 17 00:00:00 2001 From: codemasher Date: Wed, 19 Sep 2018 11:04:23 +0200 Subject: [PATCH] :sparkles: optional file saving through QROutputInterface::dump() --- src/Output/QRImage.php | 29 ++++++++++++++++++++--------- src/Output/QROutputAbstract.php | 18 +++++++++++------- src/Output/QROutputInterface.php | 4 +++- src/QRCode.php | 7 ++++--- 4 files changed, 38 insertions(+), 20 deletions(-) diff --git a/src/Output/QRImage.php b/src/Output/QRImage.php index b238b9b18..51c89604b 100644 --- a/src/Output/QRImage.php +++ b/src/Output/QRImage.php @@ -71,10 +71,12 @@ class QRImage extends QROutputAbstract{ protected $background; /** + * @param string|null $file + * * @return string * @throws \chillerlan\QRCode\Output\QRCodeOutputException */ - public function dump():string{ + public function dump(string $file = null):string{ if($this->options->cachefile !== null && !is_writable(dirname($this->options->cachefile))){ throw new QRCodeOutputException('Could not write data to cache file: '.$this->options->cachefile); @@ -92,7 +94,7 @@ class QRImage extends QROutputAbstract{ } } - $imageData = $this->dumpImage(); + $imageData = $this->dumpImage($file); if((bool)$this->options->imageBase64){ $imageData = 'data:image/'.$this->options->outputType.';base64,'.base64_encode($imageData); @@ -135,10 +137,15 @@ class QRImage extends QROutputAbstract{ } /** + * @param string|null $file + * * @return string + * @throws \chillerlan\QRCode\Output\QRCodeOutputException */ - protected function dumpImage():string { + protected function dumpImage(string $file = null):string{ + $file = $file ?? $this->options->cachefile; + ob_start(); try{ @@ -156,16 +163,20 @@ class QRImage extends QROutputAbstract{ ob_end_clean(); + if($file !== null){ + $this->saveToFile($imageData, $file); + } + return $imageData; } /** * @return void */ - protected function png(){ + protected function png():void{ imagepng( $this->image, - $this->options->cachefile, + null, in_array($this->options->pngCompression, range(-1, 9), true) ? $this->options->pngCompression : -1 @@ -176,17 +187,17 @@ class QRImage extends QROutputAbstract{ * Jiff - like... JitHub! * @return void */ - protected function gif(){ - imagegif($this->image, $this->options->cachefile); + protected function gif():void{ + imagegif($this->image); } /** * @return void */ - protected function jpg(){ + protected function jpg():void{ imagejpeg( $this->image, - $this->options->cachefile, + null, in_array($this->options->jpegQuality, range(0, 100), true) ? $this->options->jpegQuality : 85 diff --git a/src/Output/QROutputAbstract.php b/src/Output/QROutputAbstract.php index 56881b1cd..57d803d07 100644 --- a/src/Output/QROutputAbstract.php +++ b/src/Output/QROutputAbstract.php @@ -68,27 +68,31 @@ abstract class QROutputAbstract implements QROutputInterface{ * @see file_put_contents() * * @param string $data + * @param string $file * * @return bool|int * @throws \chillerlan\QRCode\Output\QRCodeOutputException */ - protected function saveToFile(string $data) { + protected function saveToFile(string $data, string $file) { - if(!is_writable(dirname($this->options->cachefile))){ - throw new QRCodeOutputException('Could not write data to cache file: '.$this->options->cachefile); + if(!is_writable(dirname($file))){ + throw new QRCodeOutputException('Could not write data to cache file: '.$file); } - return file_put_contents($this->options->cachefile, $data); + return file_put_contents($file, $data); } /** + * @param string|null $file + * * @return string */ - public function dump(){ + public function dump(string $file = null){ $data = call_user_func([$this, $this->outputMode ?? $this->defaultMode]); + $file = $file ?? $this->options->cachefile; - if($this->options->cachefile !== null){ - $this->saveToFile($data); + if($file !== null){ + $this->saveToFile($data, $file); } return $data; diff --git a/src/Output/QROutputInterface.php b/src/Output/QROutputInterface.php index 21cef072a..8ddc738c3 100644 --- a/src/Output/QROutputInterface.php +++ b/src/Output/QROutputInterface.php @@ -18,8 +18,10 @@ namespace chillerlan\QRCode\Output; interface QROutputInterface{ /** + * @param string|null $file + * * @return mixed */ - public function dump(); + public function dump(string $file = null); } diff --git a/src/QRCode.php b/src/QRCode.php index db34d7906..8664f1eea 100755 --- a/src/QRCode.php +++ b/src/QRCode.php @@ -114,12 +114,13 @@ class QRCode{ /** * Renders a QR Code for the given $data and QROptions * - * @param string $data + * @param string $data + * @param string|null $file * * @return mixed */ - public function render(string $data){ - return $this->initOutputInterface($data)->dump(); + public function render(string $data, string $file = null){ + return $this->initOutputInterface($data)->dump($file); } /**