optional file saving through QROutputInterface::dump()

This commit is contained in:
codemasher
2018-09-19 11:04:23 +02:00
parent c13e006947
commit a5486e5482
4 changed files with 38 additions and 20 deletions
+20 -9
View File
@@ -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
+11 -7
View File
@@ -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;
+3 -1
View File
@@ -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);
}
+4 -3
View File
@@ -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);
}
/**