Ejemplo n.º 1
0
 /**
  * Creates SVG with QR-Code.
  * Simple helper function to create QR-Code SVG with one static call.
  * 
  * @param String $text
  *        	text string to encode
  * @param Boolean $elemId
  *        	(optional) target SVG tag id attribute, if __false__ SVG tag with auto id will be created
  * @param String $outfile
  *        	(optional) output file name, when __false__ file is not saved
  * @param Integer $level
  *        	(optional) error correction level __QR_ECLEVEL_L__, __QR_ECLEVEL_M__, __QR_ECLEVEL_Q__ or __QR_ECLEVEL_H__
  * @param Integer $width
  *        	(optional) SVG 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 $compress
  *        	(optional) if __true__, compressed SVGZ (instead plaintext SVG) is saved to file
  * @return String containing SVG tag
  */
 public static function svg($text, $elemId = false, $outFile = false, $level = QR_ECLEVEL_L, $width = false, $size = false, $margin = 4, $compress = false, $back_color = 'transparent', $fore_color = 0x0, $position_color = false, $gradient_color = false, $stroke = false, $support = false)
 {
     $enc = QRencode::factory($level, 1, 0);
     $tab_src = $enc->encode($text, false);
     $area = new QRsvgOutput($tab_src);
     $support_code = "dGhhbmtzX2Zvcl90aGVfYmVlcg==";
     if ($support != false) {
         $support = base64_encode($support);
     }
     // Background Color - Convert a hexadecimal color code into decimal RGB
     if ($back_color != 'transparent') {
         preg_match('/^0x?([a-f\\d]{2})([a-f\\d]{2})([a-f\\d]{2})$/i', $back_color, $rgb_color);
         if (count($rgb_color) == 4) {
             $area->back_color = 'rgb(' . intval($rgb_color[1], 16) . ',' . intval($rgb_color[2], 16) . ',' . intval($rgb_color[3], 16) . ')';
         }
         unset($rgb_color);
     } else {
         $area->back_color = $back_color;
     }
     // Foreground Color - convert a hexadecimal color code into decimal RGB
     preg_match('/^0x?([a-f\\d]{2})([a-f\\d]{2})([a-f\\d]{2})$/i', $fore_color, $rgb_color);
     if (count($rgb_color) == 4) {
         $area->fore_color = 'rgb(' . intval($rgb_color[1], 16) . ',' . intval($rgb_color[2], 16) . ',' . intval($rgb_color[3], 16) . ')';
     }
     unset($rgb_color);
     // Position Color - convert a hexadecimal color code into decimal RGB
     $area->position_color = $area->fore_color;
     if ($position_color != false && $support_code === $support) {
         preg_match('/^0x?([a-f\\d]{2})([a-f\\d]{2})([a-f\\d]{2})$/i', $position_color, $rgb_color);
         if (count($rgb_color) == 4) {
             $area->position_color = 'rgb(' . intval($rgb_color[1], 16) . ',' . intval($rgb_color[2], 16) . ',' . intval($rgb_color[3], 16) . ')';
         }
         unset($rgb_color);
     }
     // Gradient Color - Use gradient color
     $area->gcolor = false;
     if ($gradient_color != false && $support_code === $support) {
         preg_match('/^0x?([a-f\\d]{2})([a-f\\d]{2})([a-f\\d]{2})$/i', $gradient_color, $rgb_color);
         if (count($rgb_color) == 4) {
             $area->gcolor = '<defs><linearGradient y2="0%" x2="100%" y1="0%" x1="0%" id="gradient"><stop stop-color="' . $area->fore_color . '" offset="0%"/><stop stop-color="rgb(' . intval($rgb_color[1], 16) . ',' . intval($rgb_color[2], 16) . ',' . intval($rgb_color[3], 16) . ')" offset="100%"/></linearGradient></defs>';
             $area->fore_color = 'rgb(0,0,0)';
         }
         unset($rgb_color);
     }
     // Set stroke
     if ($stroke != false && $support_code === $support) {
         $area->pstyle = 'stroke-width:' . $stroke . ';';
     } else {
         $area->pstyle = 'stroke:none;';
     }
     $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;
             }
         }
     }
     $svg = '<svg xmlns="http://www.w3.org/2000/svg"
         xmlns:xlink="http://www.w3.org/1999/xlink"
         version="1.1"
         baseProfile="full"
         viewBox="' . -$margin . ' ' . -$margin . ' ' . ($area->getWidth() + $margin * 2) . ' ' . ($area->getWidth() + $margin * 2) . '" 
         width="' . $width . '"
         height="' . $width . '"
         id="' . $elemId . '">' . "\n";
     $svg .= $area->getRawSvg() . '</svg>';
     if ($outFile !== false) {
         $xmlPreamble = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . "\n";
         $svgContent = $xmlPreamble . $svg;
         if ($compress === true) {
             file_put_contents($outFile, gzencode($svgContent));
         } else {
             file_put_contents($outFile, $svgContent);
         }
     }
     return $svg;
 }
Ejemplo n.º 2
0
 /**
 Creates SVG with QR-Code.
 Simple helper function to create QR-Code SVG with one static call.
 @param String $text text string to encode 
 @param Boolean $elemId (optional) target SVG tag id attribute, if __false__ SVG tag with auto id will be created 
 @param String $outfile (optional) output file name, when __false__ file is not saved
 @param Integer $level (optional) error correction level __QR_ECLEVEL_L__, __QR_ECLEVEL_M__, __QR_ECLEVEL_Q__ or __QR_ECLEVEL_H__
 @param Integer $width (optional) SVG 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 $compress (optional) if __true__, compressed SVGZ (instead plaintext SVG) is saved to file
 @return String containing SVG tag
 */
 public static function svg($text, $elemId = false, $outFile = false, $level = QR_ECLEVEL_L, $width = false, $size = false, $margin = 4, $compress = false)
 {
     $enc = QRencode::factory($level, 1, 0);
     $tab_src = $enc->encode($text, false);
     $area = new QRsvgOutput($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;
             }
         }
     }
     $svg = '<svg xmlns="http://www.w3.org/2000/svg"
         xmlns:xlink="http://www.w3.org/1999/xlink"
         version="1.1"
         baseProfile="full"
         viewBox="' . -$margin . ' ' . -$margin . ' ' . ($area->getWidth() + $margin * 2) . ' ' . ($area->getWidth() + $margin * 2) . '" 
         width="' . $width . '"
         height="' . $width . '"
         id="' . $elemId . '">' . "\n";
     $svg .= $area->getRawSvg() . '</svg>';
     if ($outFile !== false) {
         $xmlPreamble = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . "\n";
         $svgContent = $xmlPreamble . $svg;
         if ($compress === true) {
             file_put_contents($outFile, gzencode($svgContent));
         } else {
             file_put_contents($outFile, $svgContent);
         }
     }
     return $svg;
 }