Exemplo n.º 1
0
 /**
  * Creates Html+JS code to draw QR-Code with HTML5 Canvas.
  * Simple helper function to create QR-Code array with one static call.
  * 
  * @param String $text
  *        	text string to encode
  * @param String $elemId
  *        	(optional) target Canvas tag id attribute, if __false__ Canvas tag with auto id will be created
  * @param Integer $level
  *        	(optional) error correction level __QR_ECLEVEL_L__, __QR_ECLEVEL_M__, __QR_ECLEVEL_Q__ or __QR_ECLEVEL_H__
  * @param Integer $width
  *        	(optional) CANVAS element width (sam as height)
  * @param Integer $size
  *        	(optional) pixel size, multiplier for each 'virtual' pixel
  * @param Integer $margin
  *        	(optional) code margin (silent zone) in 'virtual' pixels
  * @param Boolean $autoInclude
  *        	(optional) if __true__, required qrcanvas.js lib will be included (only once)
  * @return String containing JavaScript creating the code, Canvas element (when $elemId is __false__) and script tag with required lib (when $autoInclude is __true__ and not yet included)
  */
 public static function canvas($text, $elemId = false, $level = QR_ECLEVEL_L, $width = false, $size = false, $margin = 4, $autoInclude = false)
 {
     $html = '';
     $extra = '';
     if ($autoInclude) {
         if (!self::$jscanvasincluded) {
             self::$jscanvasincluded = true;
             echo '<script type="text/javascript" src="qrcanvas.js"></script>';
         }
     }
     $enc = QRencode::factory($level, 1, 0);
     $tab_src = $enc->encode($text, false);
     $area = new QRcanvasOutput($tab_src);
     $area->detectGroups();
     $area->detectAreas();
     if ($elemId === false) {
         $elemId = 'qrcode-' . md5(mt_rand(1000, 1000000) . '.' . mt_rand(1000, 1000000) . '.' . mt_rand(1000, 1000000) . '.' . mt_rand(1000, 1000000));
         if ($width == false) {
             if ($size !== false && $size > 0) {
                 $width = ($area->getWidth() + 2 * $margin) * $size;
             } else {
                 $width = ($area->getWidth() + 2 * $margin) * 4;
             }
         }
         $html .= '<canvas id="' . $elemId . '" width="' . $width . '" height="' . $width . '">Your browser does not support CANVAS tag! Please upgrade to modern version of FireFox, Opera, Chrome or Safari/Webkit based browser</canvas>';
     }
     if ($width !== false) {
         $extra .= ', ' . $width . ', ' . $width;
     }
     if ($margin !== false) {
         $extra .= ', ' . $margin . ', ' . $margin;
     }
     $html .= '<script>if(eval("typeof "+\'QRdrawCode\'+"==\'function\'")){QRdrawCode(QRdecompactOps(\'' . $area->getCanvasOps() . '\')' . "\n" . ', \'' . $elemId . '\', ' . $area->getWidth() . ' ' . $extra . ');}else{alert(\'Please include qrcanvas.js!\');}</script>';
     return $html;
 }