From 9988c4726acc627c6f57a44644363909426174cb Mon Sep 17 00:00:00 2001 From: Mofasa Date: Sat, 8 Dec 2018 22:41:10 +0800 Subject: [PATCH 1/4] PHP, add toHtml and toSvg ```php echo $qrcode->toHtml(); echo $qrcode->toSvg(5); --- php/qrcode.php | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/php/qrcode.php b/php/qrcode.php index 234a38b..b2ca8f3 100644 --- a/php/qrcode.php +++ b/php/qrcode.php @@ -551,6 +551,67 @@ function printHTML($size = "2px") { print(""); } + + public function toHtml($size = "2px", $color = '#000', $background = null) { + + $class = uniqid('qr'); + $html = ""; + + $bg = $background === null ? '' : ' style="background-color:' . $background . '"'; + + for ($r = 0, $moduleCount = $this->getModuleCount(); $r < $moduleCount; $r++) { + + $html .= ''; + + for ($c = 0; $c < $moduleCount; $c++) { + $html .= 'isDark($r, $c) ? " style='background-color:$color'" : $bg) . '/>'; + } + + $html .= ''; + } + + return $html . '
'; + } + + public function toSvg($scale = 5, $color = '#000', $background = null, $opacity = 1) { + + $moduleCount = $this->getModuleCount(); + $length = $moduleCount * $scale; + + $bg = $background === null ? '' : ' style="background-color:' . $background . '"'; + + $svg = '' + . $bg . ''; + + for ($r = 0; $r < $moduleCount; $r++) { + $path = ''; + $count = 0; + $len = $r * $scale; + for ($c = 0; $c < $moduleCount; $c++) { + if($this->isDark($r, $c)) { + if($count == 0) { + $start = $c * $scale; + $path .= 'M' .$start. ' ' .$len; + } + $count++; + if ($c == $moduleCount - 1) { + $path .= ' h'.($count * $scale).' v'.$scale.' h-'.($count * $scale).'Z '; + } + } elseif ($count > 0) { + $path .= ' h'.($count * $scale).' v'.$scale.' h-'.($count * $scale).'Z '; + $count = 0; + } + } + $svg .= ''; + } + + return $svg . ''; + } } //--------------------------------------------------------------- From 3c8d61f042b8b60ac4f3ab683bdf98cdf63e60a6 Mon Sep 17 00:00:00 2001 From: Mofasa Date: Sun, 9 Dec 2018 15:07:04 +0800 Subject: [PATCH 2/4] add css class to svg --- php/qrcode.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/php/qrcode.php b/php/qrcode.php index b2ca8f3..b0eeedc 100644 --- a/php/qrcode.php +++ b/php/qrcode.php @@ -577,20 +577,21 @@ public function toHtml($size = "2px", $color = '#000', $background = null) { public function toSvg($scale = 5, $color = '#000', $background = null, $opacity = 1) { + $class = uniqid('qr'); $moduleCount = $this->getModuleCount(); - $length = $moduleCount * $scale; + $length = $moduleCount * $scale; $bg = $background === null ? '' : ' style="background-color:' . $background . '"'; - $svg = '' - . $bg . ''; + $svg = '' + . $bg . ''; for ($r = 0; $r < $moduleCount; $r++) { - $path = ''; - $count = 0; + $path = ''; + $count = 0; $len = $r * $scale; for ($c = 0; $c < $moduleCount; $c++) { if($this->isDark($r, $c)) { From 892ce6e151eec7157cd57521b96e3c45e32522bd Mon Sep 17 00:00:00 2001 From: Mofasa Date: Tue, 11 Dec 2018 22:19:01 +0800 Subject: [PATCH 3/4] pack svg d path --- php/qrcode.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/php/qrcode.php b/php/qrcode.php index b0eeedc..f75a5e6 100644 --- a/php/qrcode.php +++ b/php/qrcode.php @@ -601,14 +601,14 @@ public function toSvg($scale = 5, $color = '#000', $background = null, $opacity } $count++; if ($c == $moduleCount - 1) { - $path .= ' h'.($count * $scale).' v'.$scale.' h-'.($count * $scale).'Z '; + $path .= 'h'.($count * $scale).'v'.$scale.'h-'.($count * $scale).'Z'; } } elseif ($count > 0) { - $path .= ' h'.($count * $scale).' v'.$scale.' h-'.($count * $scale).'Z '; + $path .= 'h'.($count * $scale).'v'.$scale.'h-'.($count * $scale).'Z'; $count = 0; } } - $svg .= ''; + $svg .= ''; } return $svg . ''; From 3305573000d9f03e54b8f01167f8140b0c084031 Mon Sep 17 00:00:00 2001 From: Mofasa Date: Tue, 11 Dec 2018 22:24:50 +0800 Subject: [PATCH 4/4] slim --- php/qrcode.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/php/qrcode.php b/php/qrcode.php index f75a5e6..1e75416 100644 --- a/php/qrcode.php +++ b/php/qrcode.php @@ -585,8 +585,8 @@ public function toSvg($scale = 5, $color = '#000', $background = null, $opacity $svg = '' - . $bg . ''; for ($r = 0; $r < $moduleCount; $r++) {