Просмотр исходного кода

:sparkles: optional file saving through QROutputInterface::dump()

codemasher 7 лет назад
Родитель
Сommit
a5486e5482
4 измененных файлов с 38 добавлено и 20 удалено
  1. 20 9
      src/Output/QRImage.php
  2. 11 7
      src/Output/QROutputAbstract.php
  3. 3 1
      src/Output/QROutputInterface.php
  4. 4 3
      src/QRCode.php

+ 20 - 9
src/Output/QRImage.php

@@ -71,10 +71,12 @@ class QRImage extends QROutputAbstract{
 	protected $background;
 	protected $background;
 
 
 	/**
 	/**
+	 * @param string|null $file
+	 *
 	 * @return string
 	 * @return string
 	 * @throws \chillerlan\QRCode\Output\QRCodeOutputException
 	 * @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))){
 		if($this->options->cachefile !== null && !is_writable(dirname($this->options->cachefile))){
 			throw new QRCodeOutputException('Could not write data to cache file: '.$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){
 		if((bool)$this->options->imageBase64){
 			$imageData = 'data:image/'.$this->options->outputType.';base64,'.base64_encode($imageData);
 			$imageData = 'data:image/'.$this->options->outputType.';base64,'.base64_encode($imageData);
@@ -135,10 +137,15 @@ class QRImage extends QROutputAbstract{
 	}
 	}
 
 
 	/**
 	/**
+	 * @param string|null $file
+	 *
 	 * @return string
 	 * @return string
+
 	 * @throws \chillerlan\QRCode\Output\QRCodeOutputException
 	 * @throws \chillerlan\QRCode\Output\QRCodeOutputException
 	 */
 	 */
-	protected function dumpImage():string {
+	protected function dumpImage(string $file = null):string{
+		$file = $file ?? $this->options->cachefile;
+
 		ob_start();
 		ob_start();
 
 
 		try{
 		try{
@@ -156,16 +163,20 @@ class QRImage extends QROutputAbstract{
 
 
 		ob_end_clean();
 		ob_end_clean();
 
 
+		if($file !== null){
+			$this->saveToFile($imageData, $file);
+		}
+
 		return $imageData;
 		return $imageData;
 	}
 	}
 
 
 	/**
 	/**
 	 * @return void
 	 * @return void
 	 */
 	 */
-	protected function png(){
+	protected function png():void{
 		imagepng(
 		imagepng(
 			$this->image,
 			$this->image,
-			$this->options->cachefile,
+			null,
 			in_array($this->options->pngCompression, range(-1, 9), true)
 			in_array($this->options->pngCompression, range(-1, 9), true)
 				? $this->options->pngCompression
 				? $this->options->pngCompression
 				: -1
 				: -1
@@ -176,17 +187,17 @@ class QRImage extends QROutputAbstract{
 	 * Jiff - like... JitHub!
 	 * Jiff - like... JitHub!
 	 * @return void
 	 * @return void
 	 */
 	 */
-	protected function gif(){
-		imagegif($this->image, $this->options->cachefile);
+	protected function gif():void{
+		imagegif($this->image);
 	}
 	}
 
 
 	/**
 	/**
 	 * @return void
 	 * @return void
 	 */
 	 */
-	protected function jpg(){
+	protected function jpg():void{
 		imagejpeg(
 		imagejpeg(
 			$this->image,
 			$this->image,
-			$this->options->cachefile,
+			null,
 			in_array($this->options->jpegQuality, range(0, 100), true)
 			in_array($this->options->jpegQuality, range(0, 100), true)
 				? $this->options->jpegQuality
 				? $this->options->jpegQuality
 				: 85
 				: 85

+ 11 - 7
src/Output/QROutputAbstract.php

@@ -68,27 +68,31 @@ abstract class QROutputAbstract implements QROutputInterface{
 	 * @see file_put_contents()
 	 * @see file_put_contents()
 	 *
 	 *
 	 * @param string $data
 	 * @param string $data
+	 * @param string $file
 	 *
 	 *
 	 * @return bool|int
 	 * @return bool|int
 	 * @throws \chillerlan\QRCode\Output\QRCodeOutputException
 	 * @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
 	 * @return string
 	 */
 	 */
-	public function dump(){
+	public function dump(string $file = null){
 		$data = call_user_func([$this, $this->outputMode ?? $this->defaultMode]);
 		$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;
 		return $data;

+ 3 - 1
src/Output/QROutputInterface.php

@@ -18,8 +18,10 @@ namespace chillerlan\QRCode\Output;
 interface QROutputInterface{
 interface QROutputInterface{
 
 
 	/**
 	/**
+	 * @param string|null $file
+	 *
 	 * @return mixed
 	 * @return mixed
 	 */
 	 */
-	public function dump();
+	public function dump(string $file = null);
 
 
 }
 }

+ 4 - 3
src/QRCode.php

@@ -114,12 +114,13 @@ class QRCode{
 	/**
 	/**
 	 * Renders a QR Code for the given $data and QROptions
 	 * Renders a QR Code for the given $data and QROptions
 	 *
 	 *
-	 * @param string $data
+	 * @param string      $data
+	 * @param string|null $file
 	 *
 	 *
 	 * @return mixed
 	 * @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);
 	}
 	}
 
 
 	/**
 	/**