1
185

qrencode

qrencode
SYNOPSIS

qrencode($s, $size=1, $quality='M', $fg=false, $bg=false, $margin=0)

DESCRIPTION

PHP QR Code

CODE
  1. require_once 'validatecolor.php';
  2. require_once 'phpqrcode/qrlib.php';
  3.  
  4. function qrencode($s, $size=1, $quality='M', $fg=false, $bg=false, $margin=0) {
  5.     $ql = array('L' => QR_ECLEVEL_L, 'M' => QR_ECLEVEL_M, 'Q' => QR_ECLEVEL_Q, 'H' => QR_ECLEVEL_H);
  6.  
  7.     if (!$s or !is_numeric($size) or !is_numeric($margin) or !$quality or !array_key_exists($quality, $ql) or ($fg and !validate_color($fg)) or ($bg and !validate_color($bg))) {
  8.         return false;
  9.     }
  10.  
  11.     if ($size < 1) {
  12.         $size=1;
  13.     }
  14.  
  15.     if ($margin < 0) {
  16.         $margin=0;
  17.     }
  18.  
  19.     $q=$ql[$quality];
  20.  
  21.     $frame = @QRcode::text($s, false, $q, 1, 0);
  22.  
  23.     if (!$frame) {
  24.         return false;
  25.     }
  26.  
  27.     $h = count($frame);
  28.     $w = strlen($frame[0]);
  29.  
  30.     $img = imagecreatetruecolor($w+2*$margin, $h);
  31.     $qrimg = imagecreatetruecolor($w*$size+2*$margin, $h*$size+2*$margin);
  32.  
  33.     if ($bg) {
  34.         $rgb=str_split($bg[0] == '#' ? substr($bg, 1, 6) : $bg, 2);
  35.         $r=hexdec($rgb[0]);
  36.         $g=hexdec($rgb[1]);
  37.         $b=hexdec($rgb[2]);
  38.     }
  39.     else {
  40.         $r=$g=$b=255;
  41.     }
  42.  
  43.     $bg=imagecolorallocate($img, $r, $g, $b);
  44.     imagefill($img, 0, 0, $bg);
  45.  
  46.     $bg=imagecolorallocate($qrimg, $r, $g, $b);
  47.     imagefill($qrimg, 0, 0, $bg);
  48.  
  49.     if ($fg) {
  50.         $rgb=str_split($fg[0] == '#' ? substr($fg, 1, 6) : $fg, 2);
  51.         $r=hexdec($rgb[0]);
  52.         $g=hexdec($rgb[1]);
  53.         $b=hexdec($rgb[2]);
  54.     }
  55.     else {
  56.         $r=$g=$b=0;
  57.     }
  58.  
  59.     $fg=imagecolorallocate($img, $r, $g, $b);
  60.  
  61.     for ($y=0; $y < $h; $y++) {
  62.         for ($x=0; $x < $w; $x++) {
  63.             if ($frame[$y][$x] == '1') {
  64.                 imagesetpixel($img, $x, $y, $fg);
  65.             }
  66.         }
  67.     }
  68.  
  69.     imagecopyresized($qrimg, $img, $margin, $margin, 0, 0, $w*$size, $h*$size, $w, $h);
  70.  
  71.     imagedestroy($img);
  72.  
  73.     ob_start();
  74.     imagepng($qrimg);
  75.     $png=ob_get_clean();
  76.     imagedestroy($qrimg);
  77.  
  78.     return $png;
  79. }
SEE ALSO

qrdecode

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].