wordpressプラグイン改造メモ:Simple QR Code Generator Widget

WordPress

QRコードを表示するためのプラグイン Simple QR Code Generator Widget を改造。

  • URIが指定されていないときは、トップページではなく、現在表示しているページのURLを使う
  • Google Chart APIではなく、Pear Image_QRCode ライブラリを使う
  • イメージへのリンクではなく、Data URIを使う
  • 副作用として大きさの指定はできなくなる


パッチ

diff -u qrCode.php.old qrCode.php
--- qrCode.php.old	2017-08-17 08:05:49.102785149 +0900
+++ qrCode.php	2017-08-17 08:06:24.967806366 +0900
@@ -9,6 +9,8 @@
   Docs: http://code.google.com/apis/chart/infographics/docs/qr_codes.html
  */
 
+require_once '/usr/share/pear/Image/QRCode.php';
+
 function qrCodeGenerator(
   $width = '100', 
   $height = '100', 
@@ -17,20 +19,21 @@
     $title = get_the_title();
 
     if(empty($uri)){
-      $title = get_bloginfo('name');
-      $uri = get_bloginfo('url');
+      if(is_home()){
+        $title = get_bloginfo('name');
+        $uri = get_bloginfo('url');
+      }else{
+        $title = get_the_title(); #get_bloginfo('name');
+        $uri = get_the_permalink(); # get_bloginfo('url');
+      }
     }
-    
-    echo sprintf(
-      '<img src="//chart.apis.google.com/chart?cht=%1$s&chs=%2$dx%3$d&chl=%4$s&choe=%5$s" alt="%6$s" />', 
-      'qr', 
-      (int) $width, 
-      (int) $height, 
-      htmlspecialchars($uri), 
-      'UTF-8', 
-      htmlspecialchars($title)
-    );
-
+    $qrcode = new Image_QRCode();
+    $img = $qrcode->makeCode($uri,array('image_type'=>'png','error_correct'=>'H','output_type'=>'return'));
+    ob_start();
+	imagepng($img);
+    $base64=base64_encode(ob_get_contents());
+    ob_end_clean();
+    echo sprintf('<img src="data:image/png;base64,%s">',$base64);
 }
 
 function qrCode_widget($args) {

WordPress

Posted by ず@沖縄