From c73aa6657c5e1a69c76e63a9b8f70a00e76e67b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20R=C3=BCdinger?= Date: Tue, 16 Sep 2014 10:31:27 +0200 Subject: [PATCH 1/2] Add transparency feature for png files --- qrencode.php | 9 +++++---- qrimage.php | 14 ++++++++++---- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/qrencode.php b/qrencode.php index 249235c..206f184 100644 --- a/qrencode.php +++ b/qrencode.php @@ -280,9 +280,9 @@ public function encodeString($string, $version, $level, $hint, $casesensitive) } //---------------------------------------------------------------------- - public static function png($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4, $saveandprint=false, $back_color = 0xFFFFFF, $fore_color = 0x000000) + public static function png($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4, $saveandprint=false, $back_color = 0xFFFFFF, $fore_color = 0x000000, $back_transparent = false) { - $enc = QRencode::factory($level, $size, $margin, $back_color, $fore_color); + $enc = QRencode::factory($level, $size, $margin, $back_color, $fore_color, false, $back_transparent); return $enc->encodePNG($text, $outfile, $saveandprint=false); } @@ -424,7 +424,7 @@ class QRencode { public $hint = QR_MODE_8; //---------------------------------------------------------------------- - public static function factory($level = QR_ECLEVEL_L, $size = 3, $margin = 4, $back_color = 0xFFFFFF, $fore_color = 0x000000, $cmyk = false) + public static function factory($level = QR_ECLEVEL_L, $size = 3, $margin = 4, $back_color = 0xFFFFFF, $fore_color = 0x000000, $cmyk = false, $back_transparent = false) { $enc = new QRencode(); $enc->size = $size; @@ -432,6 +432,7 @@ public static function factory($level = QR_ECLEVEL_L, $size = 3, $margin = 4, $b $enc->fore_color = $fore_color; $enc->back_color = $back_color; $enc->cmyk = $cmyk; + $enc->back_transparent = $back_transparent; switch ($level.'') { case '0': @@ -510,7 +511,7 @@ public function encodePNG($intext, $outfile = false,$saveandprint=false) $maxSize = (int)(QR_PNG_MAXIMUM_SIZE / (count($tab)+2*$this->margin)); - QRimage::png($tab, $outfile, min(max(1, $this->size), $maxSize), $this->margin,$saveandprint, $this->back_color, $this->fore_color); + QRimage::png($tab, $outfile, min(max(1, $this->size), $maxSize), $this->margin,$saveandprint, $this->back_color, $this->fore_color, $this->back_transparent); } catch (Exception $e) { diff --git a/qrimage.php b/qrimage.php index b62d557..da0b920 100644 --- a/qrimage.php +++ b/qrimage.php @@ -27,9 +27,9 @@ class QRimage { //---------------------------------------------------------------------- - public static function png($frame, $filename = false, $pixelPerPoint = 4, $outerFrame = 4,$saveandprint=FALSE, $back_color, $fore_color) + public static function png($frame, $filename = false, $pixelPerPoint = 4, $outerFrame = 4,$saveandprint=FALSE, $back_color, $fore_color, $back_transparent) { - $image = self::image($frame, $pixelPerPoint, $outerFrame, $back_color, $fore_color); + $image = self::image($frame, $pixelPerPoint, $outerFrame, $back_color, $fore_color, $back_transparent); if ($filename === false) { Header("Content-type: image/png"); @@ -63,7 +63,7 @@ public static function jpg($frame, $filename = false, $pixelPerPoint = 8, $outer } //---------------------------------------------------------------------- - private static function image($frame, $pixelPerPoint = 4, $outerFrame = 4, $back_color = 0xFFFFFF, $fore_color = 0x000000) + private static function image($frame, $pixelPerPoint = 4, $outerFrame = 4, $back_color = 0xFFFFFF, $fore_color = 0x000000, $back_transparent = false) { $h = count($frame); $w = strlen($frame[0]); @@ -100,8 +100,14 @@ private static function image($frame, $pixelPerPoint = 4, $outerFrame = 4, $back $target_image =ImageCreate($imgW * $pixelPerPoint, $imgH * $pixelPerPoint); ImageCopyResized($target_image, $base_image, 0, 0, 0, 0, $imgW * $pixelPerPoint, $imgH * $pixelPerPoint, $imgW, $imgH); + + if ($back_transparent == true) { + $col[2] = imagecolorexact($target_image, $r2, $b2, $g2); + ImageColorTransparent($target_image, $col[2]); + } + ImageDestroy($base_image); return $target_image; } - } \ No newline at end of file + } From 06684a27e6b87edb1b47d8bbdf3e7bddd5002aa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20R=C3=BCdinger?= Date: Tue, 16 Sep 2014 10:33:31 +0200 Subject: [PATCH 2/2] Add transparency example to demo page * add transparency checkbox * add background color so we can see if transparency works --- index.php | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/index.php b/index.php index 9e14b7e..aea5193 100644 --- a/index.php +++ b/index.php @@ -1,4 +1,4 @@ -PHP QR Code
"; + echo "

PHP QR Code


"; //set it to writable location, a place for temp generated PNG files $PNG_TEMP_DIR = dirname(__FILE__).DIRECTORY_SEPARATOR.'temp'.DIRECTORY_SEPARATOR; @@ -34,7 +34,7 @@ //ofcourse we need rights to create temp dir if (!file_exists($PNG_TEMP_DIR)) - mkdir($PNG_TEMP_DIR); + mkdir($PNG_TEMP_DIR); $filename = $PNG_TEMP_DIR.'test.png'; @@ -49,27 +49,33 @@ if (isset($_REQUEST['size'])) $matrixPointSize = min(max((int)$_REQUEST['size'], 1), 10); - + $backTransparent = false; + if (isset($_REQUEST['back_transparent'])) + $backTransparent = (bool) $_REQUEST['back_transparent']; + + if (isset($_REQUEST['data'])) { //it's very important! if (trim($_REQUEST['data']) == '') die('data cannot be empty! back'); - + // user data - $filename = $PNG_TEMP_DIR.'test'.md5($_REQUEST['data'].'|'.$errorCorrectionLevel.'|'.$matrixPointSize).'.png'; - QRcode::png($_REQUEST['data'], $filename, $errorCorrectionLevel, $matrixPointSize, 2); - + $filename = $PNG_TEMP_DIR.'test'.md5($_REQUEST['data'].'|'.$errorCorrectionLevel.'|'.$matrixPointSize).'.png'; + QRcode::png($_REQUEST['data'], $filename, $errorCorrectionLevel, $matrixPointSize, 2, false, 0xFFFFFF, 0x000000, $backTransparent); + } else { - //default data - echo 'You can provide data in GET parameter: like that
'; - QRcode::png('PHP QR Code :)', $filename, $errorCorrectionLevel, $matrixPointSize, 2); - - } + //default data + echo 'You can provide data in GET parameter: like that
'; + QRcode::png('PHP QR Code :)', $filename, $errorCorrectionLevel, $matrixPointSize, 2, false, 0xFFFFFF, 0x000000, $backTransparent); - //display generated file - echo '
'; + } + + //display generated file + echo '
+ +

'; //config form echo '
@@ -86,9 +92,10 @@ echo ''; echo '  + Transparent

'; - // benchmark - QRtools::timeBenchmark(); - - \ No newline at end of file + // benchmark + QRtools::timeBenchmark(); + +