Ver código fonte

added QRImage* soon:x:

smiley 10 anos atrás
pai
commit
2681aa4ef5
3 arquivos alterados com 155 adições e 5 exclusões
  1. 9 5
      examples/image.php
  2. 127 0
      src/Output/QRImage.php
  3. 19 0
      src/Output/QRImageOptions.php

+ 9 - 5
examples/image.php

@@ -2,14 +2,18 @@
 
 require_once '../vendor/autoload.php';
 
-use codemasher\QRCode\QRCode;
-use codemasher\QRCode\QRConst;
+use chillerlan\QRCode\Output\QRImage;
+use chillerlan\QRCode\QRCode;
+use chillerlan\QRCode\QRConst;
+use chillerlan\QRCode\QROptions;
 
-// google authenticator
-$qr = new QRCode('otpauth://totp/test?secret=B3JX4VCVJDVNXNZ5&issuer=chillerlan.net', QRConst::ERROR_CORRECT_LEVEL_L);
+$qrOptions = new QROptions;
+$qrOptions->output = new QRImage();
+
+$qr =  new QRCode('https://www.youtube.com/watch?v=DLzxrzFCyOs&t=43s', $qrOptions);
 
 header('Content-type: image/png');
 
-$im = $qr->createImage(5, 5);
+$im = $qr->output();
 imagepng($im);
 imagedestroy($im);

+ 127 - 0
src/Output/QRImage.php

@@ -0,0 +1,127 @@
+<?php
+/**
+ * Class QRImage
+ *
+ * @filesource   QRImage.php
+ * @created      05.12.2015
+ * @package      chillerlan\QRCode\Output
+ * @author       Smiley <smiley@chillerlan.net>
+ * @copyright    2015 Smiley
+ * @license      MIT
+ */
+
+namespace chillerlan\QRCode\Output;
+use chillerlan\QRCode\Util;
+
+/**
+ * toBase64()
+ */
+class QRImage extends QROutputBase implements QROutputInterface{
+
+	/**
+	 * @var \chillerlan\QRCode\Output\QRImageOptions $outputOptions
+	 */
+	protected $options;
+
+	/**
+	 * @var \chillerlan\QRCode\Output\QRImageOptions $outputOptions
+	 */
+	public function __construct(QRImageOptions $outputOptions = null){
+		$this->options = $outputOptions;
+
+		if(!$this->options instanceof QRImageOptions){
+			$this->options = new QRImageOptions;
+		}
+
+	}
+
+
+
+	/**
+	 * added $fg (foreground), $bg (background), and $bgtrans (use transparent bg) parameters
+	 * also added some simple error checking on parameters
+	 * updated 2015.07.27 ~ DoktorJ
+	 *
+	 * @param int        $size
+	 * @param int        $margin
+	 * @param int        $fg
+	 * @param int        $bg
+	 * @param bool|false $bgtrans
+	 *
+	 * @return resource
+	 */
+	public function toImage($size = 2, $margin = 2, $fg = 0x000000, $bg = 0xFFFFFF, $bgtrans = false){
+
+		// size/margin EC
+		if(!is_numeric($size)){
+			$size = 2;
+		}
+		if(!is_numeric($margin)){
+			$margin = 2;
+		}
+		if($size < 1){
+			$size = 1;
+		}
+		if($margin < 0){
+			$margin = 0;
+		}
+
+		$image_size = $this->pixelCount * $size + $margin * 2;
+
+		$image = imagecreatetruecolor($image_size, $image_size);
+
+		// fg/bg EC
+		if($fg < 0 || $fg > 0xFFFFFF){
+			$fg = 0x0;
+		}
+		if($bg < 0 || $bg > 0xFFFFFF){
+			$bg = 0xFFFFFF;
+		}
+
+		// convert hexadecimal RGB to arrays for imagecolorallocate
+		$fgrgb = Util::hex2rgb($fg);
+		$bgrgb = Util::hex2rgb($bg);
+
+		// replace $black and $white with $fgc and $bgc
+		$fgc = imagecolorallocate($image, $fgrgb['r'], $fgrgb['g'], $fgrgb['b']);
+		$bgc = imagecolorallocate($image, $bgrgb['r'], $bgrgb['g'], $bgrgb['b']);
+		if($bgtrans){
+			imagecolortransparent($image, $bgc);
+		}
+
+		// update $white to $bgc
+		imagefilledrectangle($image, 0, 0, $image_size, $image_size, $bgc);
+
+		for($r = 0; $r < $this->pixelCount; $r++){
+			for($c = 0; $c < $this->pixelCount; $c++){
+				if($this->matrix[$r][$c]){
+
+					// update $black to $fgc
+					imagefilledrectangle($image,
+						$margin + $c * $size,
+						$margin + $r * $size,
+						$margin + ($c + 1) * $size - 1,
+						$margin + ($r + 1) * $size - 1,
+						$fgc);
+				}
+			}
+		}
+
+		return $image;
+	}
+
+	public function dump(){
+		return $this->toImage();
+	}
+
+	protected function toPNG(){
+
+	}
+
+	/**
+	 * Actually, it's pronounced "DJIFF". *hides*
+	 */
+	protected function toGIF(){
+
+	}
+}

+ 19 - 0
src/Output/QRImageOptions.php

@@ -0,0 +1,19 @@
+<?php
+/**
+ *
+ * @filesource   QRImageOptions.php
+ * @created      08.12.2015
+ * @package      chillerlan\QRCode\Output
+ * @author       Smiley <smiley@chillerlan.net>
+ * @copyright    2015 Smiley
+ * @license      MIT
+ */
+
+namespace chillerlan\QRCode\Output;
+
+/**
+ * Class QRImageOptions
+ */
+class QRImageOptions{
+
+}