Strict Standards: Only variables should be passed by reference in C:\xampp\htdocs\PhpDoc\phpDocumentor\Smarty-2.6.0\libs\Smarty_Compiler.class.php on line 712

Strict Standards: Only variables should be passed by reference in C:\xampp\htdocs\PhpDoc\phpDocumentor\Smarty-2.6.0\libs\Smarty_Compiler.class.php on line 712

Strict Standards: Only variables should be passed by reference in C:\xampp\htdocs\PhpDoc\phpDocumentor\Smarty-2.6.0\libs\Smarty_Compiler.class.php on line 712

Strict Standards: Only variables should be passed by reference in C:\xampp\htdocs\PhpDoc\phpDocumentor\Smarty-2.6.0\libs\Smarty_Compiler.class.php on line 712

Strict Standards: Only variables should be passed by reference in C:\xampp\htdocs\PhpDoc\phpDocumentor\Smarty-2.6.0\libs\Smarty_Compiler.class.php on line 712

Strict Standards: Only variables should be passed by reference in C:\xampp\htdocs\PhpDoc\phpDocumentor\Smarty-2.6.0\libs\Smarty_Compiler.class.php on line 712

Strict Standards: Only variables should be passed by reference in C:\xampp\htdocs\PhpDoc\phpDocumentor\Smarty-2.6.0\libs\Smarty_Compiler.class.php on line 712

Strict Standards: Only variables should be passed by reference in C:\xampp\htdocs\PhpDoc\phpDocumentor\Smarty-2.6.0\libs\Smarty_Compiler.class.php on line 712
File Source for class.qrcode.php
phpDocumentor ycwp-qr-me
[ class tree: ycwp-qr-me ] [ index: ycwp-qr-me ] [ all elements ]

Source for file class.qrcode.php

Documentation is available at class.qrcode.php

  1. <?php
  2. /**
  3.  * QRCode generator. Uses Google Charts API to generate QR Codes.
  4.  *
  5.  * @author Nicola Mustone <mail@nicolamustone.it>
  6.  * @copyright Copyright (c) 2012 Nicola Mustone
  7.  * @version 1.0
  8.  * @package ycwp-qr-me
  9.  */
  10. class QRCode {
  11.     /**
  12.      * Base path for QR Code Google request.
  13.      *
  14.      * @access private
  15.      * @var string 
  16.      * @since 1.0
  17.      */
  18.     private $_GOOGLE_CHART_URL;
  19.     
  20.     /**
  21.      * QR Code image width.
  22.      *
  23.      * @access private
  24.      * @var int 
  25.      * @since 1.0
  26.      */
  27.     private $_width;
  28.     
  29.     /**
  30.      * QR Code image height.
  31.      *
  32.      * @access private
  33.      * @var int 
  34.      * @since 1.0
  35.      */
  36.     private $_height;
  37.     
  38.     /**
  39.      * QR COde image settings.
  40.      *
  41.      * @access private
  42.      * @var array 
  43.      * @since 1.0
  44.      */
  45.     private $_settings;
  46.     
  47.     /**
  48.      * Initialize properties.
  49.      */
  50.     public function __construct({
  51.         $this->_width = 200;
  52.         $this->_height = 200;
  53.         $this->_settings = array(
  54.             'cht' => 'qr',
  55.             'chs' => $this->_width . 'x' $this->_height,
  56.             'choe' => 'UTF-8',
  57.             'chld' => 'L',
  58.             'chl' => ' '
  59.         );
  60.         
  61.         ifin_array'https'stream_get_wrappers() ) ) {
  62.             $this->_GOOGLE_CHART_URL = 'https://chart.googleapis.com/chart?';
  63.         else {
  64.             $this->_GOOGLE_CHART_URL = 'http://chart.googleapis.com/chart?';
  65.         }
  66.     }
  67.     
  68.     /**
  69.      * Sets QR Code image size.
  70.      *
  71.      * @param int $w 
  72.      * @param int $h 
  73.      * @return void 
  74.      * @since 1.0
  75.      */
  76.     public function setSize($w$h{
  77.         $this->setWidth($w);
  78.         $this->setHeight($h);
  79.         
  80.         $size $this->_width . 'x' $this->_height;
  81.         $this->_setProperty('chs'$size);
  82.     }
  83.     
  84.     /**
  85.      * Sets QR Code image width.
  86.      *
  87.      * @param int $width 
  88.      * @return void 
  89.      * @since 1.0
  90.      */
  91.     public function setWidth($w{
  92.         $this->_width = (int) $w;    
  93.     }
  94.     
  95.     /**
  96.      * Sets QR Code image height.
  97.      *
  98.      * @param int $height 
  99.      * @return void 
  100.      * @since 1.0
  101.      */
  102.     public function setHeight($h{
  103.         $this->_height = (int) $h;    
  104.     }
  105.     
  106.     /**
  107.      * Sets QR Code image content charset.
  108.      *
  109.      * @param string $charset 
  110.      * @return void 
  111.      * @since 1.0
  112.      */
  113.     public function setCharSet($charset{
  114.         $this->_setProperty('choe'$charset);
  115.     }
  116.     
  117.     /**
  118.      * Sets QR Code image error correction level and margin.
  119.      *
  120.      * @param string $level 
  121.      * @param int $margin 
  122.      * @return void 
  123.      * @since 1.0
  124.      */
  125.     public function setErrorLevel($level$margin 0{
  126.         $this->_setProperty('chld'$level);
  127.         $this->_setProperty('chld'$margintrue);
  128.     }
  129.     
  130.     /**
  131.      * Sets QR Code image content.
  132.      *
  133.      * @param string $content 
  134.      * @return void 
  135.      * @since 1.0
  136.      */
  137.     public function setContent($content{
  138.         $this->_setProperty('chl'urlencode($content));
  139.     }
  140.     
  141.     /**
  142.      * Magic method to get properties value.
  143.      *
  144.      * @param string $name Property name
  145.      * @return mixed|boolFalse on failure
  146.      * @since 1.0
  147.      */
  148.     public function __get$name {
  149.         ifproperty_exists$this$name ) ) {
  150.             return $this->{$name};
  151.         }
  152.         
  153.         return false;
  154.     }
  155.     
  156.     /**
  157.      * Send a GET request to Google for a QR Code.
  158.      *
  159.      * @param array $params Additional params that will be added to the <img> tag.
  160.      * @return string 
  161.      */
  162.     public function QR_GET($params array()) {
  163.         $data $this->_makeURL();
  164.         $qr '<img src="' $data '"';
  165.             
  166.         if(!empty($params)) {
  167.             foreach($params as $key => $value{
  168.                 $qr .= ' ' $key '="' $value .'"';    
  169.             }
  170.         }
  171.         
  172.         $qr .= ' />';
  173.         
  174.         return $qr;
  175.     }
  176.     
  177.     /**
  178.      * Send a POST request to Google for a QR Code. Required if the content is larger than 2 KB.
  179.      *
  180.      * @param array $params Additional params that will be added to the <img> tag.
  181.      * @return string 
  182.      */
  183.     public function QR_POST$params array() ) {
  184.         $context stream_context_create
  185.             array'http' => 
  186.                 array(
  187.                     'method' => 'POST',
  188.                     'header' => 'Content-Type: image/png\r\n',
  189.                     'content' => http_build_query$this->_settings )
  190.                 )
  191.             )
  192.         );
  193.           
  194.         $data null;
  195.         $fp fopen$this->_makeURL()'r'false$context );
  196.         
  197.         while!feof$fp ) ) {
  198.             $data .= fread$fp1024 );
  199.         }
  200.           
  201.         $qr '<img src="data:image/png;base64,' base64_encode$data '" ';
  202.         
  203.         if!empty$params ) ) {
  204.             foreach$params as $key => $value {
  205.                 $qr .= ' ' $key '="' $value .'"';    
  206.             }
  207.         }
  208.         
  209.         $qr .= ' />';
  210.         
  211.         return $qr;
  212.     }
  213.     
  214.     /**
  215.      * Re-initialize the properties
  216.      *
  217.      * @return void 
  218.      * @since 1.0
  219.      */
  220.     public function reset({
  221.         $this->_width 200;
  222.         $this->_height 200;
  223.         $this->_settings array(
  224.             'cht' => 'qr',
  225.             'chs' => $this->_width 'x' $this->_height,
  226.             'choe' => 'UTF-8',
  227.             'chld' => 'L',
  228.             'chl' => ''
  229.         );    
  230.     }
  231.     
  232.     /**
  233.      * Set the specified value for a property.
  234.      *
  235.      * @param string $key Property name
  236.      * @param mixed $value 
  237.      * @param bool $append Append the value, otherwise it will override
  238.      * @param string $separator 
  239.      * @return bool 
  240.      * @since 1.0
  241.      */
  242.     protected function _setProperty($key$value$append false$separator '|'{
  243.         $params array_keys($this->_settings);
  244.         
  245.         if($key != 'cht'{
  246.             if(in_array($key$params)) {
  247.                 if(!$append{
  248.                     $this->_settings[$key$value;    
  249.                 else {
  250.                     $this->_settings[$key$this->_settings[$key$separator $value;
  251.                 }
  252.                 
  253.                 return true;
  254.             }
  255.         }
  256.         
  257.         return false;
  258.     }
  259.     
  260.     /**
  261.      * Make the URL for the request.
  262.      *
  263.      * @return string 
  264.      * @since 1.0
  265.      */
  266.     protected function _makeURL({
  267.         $url  $this->_GOOGLE_CHART_URL;
  268.         $url .= '&amp;cht=' $this->_settings['cht'];
  269.         $url .= '&amp;chs=' $this->_settings['chs'];
  270.         $url .= '&amp;chld=' $this->_settings['chld'];
  271.         $url .= '&amp;chl=' $this->_settings['chl'];
  272.         $url .= '&amp;choe=' $this->_settings['choe'];
  273.         
  274.         return $url;
  275.     }
  276. }
  277. ?>

Documentation generated on Tue, 21 Feb 2012 23:39:34 +0100 by phpDocumentor 1.4.3