<?php include '../lib/full/qrlib.php'; include 'config.php'; // Saving SVG to file, demo with sourcecode preview $tempDir = EXAMPLE_TMP_SERVERPATH; $dataText = 'PHP QR Code :)'; $svgTagId = 'id-of-svg'; $saveToFile = '203_demo.svg'; // it is saved to file but also returned from function $svgCode = QRcode::svg($dataText, false, $tempDir . $saveToFile); $svgCodeFromFile = file_get_contents($tempDir . $saveToFile); // taken from: http://php.net/manual/en/function.highlight-string.php by: Dobromir Velev function xml_highlight($s) { $s = preg_replace("|<([^/?])(.*)\\s(.*)>|isU", "[1]<[2]\\1\\2[/2] [5]\\3[/5]>[/1]", $s); $s = preg_replace("|</(.*)>|isU", "[1]</[2]\\1[/2]>[/1]", $s); $s = preg_replace("|<\\?(.*)\\?>|isU", "[3]<?\\1?>[/3]", $s); $s = preg_replace("|\\=\"(.*)\"|isU", "[6]=[/6][4]\"\\1\"[/4]", $s); $s = htmlspecialchars($s); $s = str_replace("\t", " ", $s); $s = str_replace(" ", " ", $s); $replace = array(1 => '0000FF', 2 => '0000FF', 3 => '800000', 4 => '00AA00', 5 => 'FF0000', 6 => '0000FF'); foreach ($replace as $k => $v) { $s = preg_replace("|\\[" . $k . "\\](.*)\\[/" . $k . "\\]|isU", "<font color=\"#" . $v . "\">\\1</font>", $s); } return nl2br($s); } // tag output echo $svgCodeFromFile; echo '<br/>';
public static function qrtest() { // Include only this file, remaining required files will be included from it if (!class_exists("qrstr", FALSE)) { require_once PATH_PHPQRCODE . "/qrlib.php"; } //write code into file, Error corection lecer is lowest, L (one form: L,M,Q,H) //each code square will be 4x4 pixels (4x zoom) //code will have 2 code squares white boundary around if (1) { // Error correction level L : About 7% or less errors can be corrected. // Error correction level M : About 15% or less errors can be corrected. // Error correction level Q : About 25% or less errors can be corrected. // Error correction level H : About 30% or less errors can be corrected. $text = "http://www.nordita.org/guest/"; $filetype = "png"; // "png","svg","eps","txt" if (!in_array($filetype, array("png", "svg", "eps", "txt"))) { return FALSE; } $level = "H"; // "L" (default) ,"M", "Q", "H" $pixelsize = 16; // pixel size of each code square $margin = 4; // boundary width in number of code squares $saveandprint = FALSE; // TRUE = save to file and display in browser; FALSE = save to file only; redundant if $outfile=FALSE $back_color = 0xffffff; $fore_color = 0x0; $cmyk = TRUE; $filename = "qrcode"; $outfile = PATH_PHPQRCACHE . $filename . "." . $filetype; // full file path or FALSE (only output on screen) //$outfile = FALSE; switch ($filetype) { case "png": QRcode::png($text, $outfile, $level, $pixelsize, $margin, $saveandprint, $back_color, $fore_color); break; case "eps": QRcode::eps($text, $outfile, $level, $pixelsize, $margin, $saveandprint, $back_color, $fore_color, $cmyk); break; case "svg": QRcode::svg($text, $outfile, $level, $pixelsize, $margin, $saveandprint, $back_color, $fore_color); break; case "txt": QRcode::text($text, $outfile, $level, $pixelsize, $margin); // black = "1", white = "0" break; } // end switch } //same as above but outputs file directly into browser (with appr. header etc.) //all other settings are default //WARNING! it should be FIRST and ONLY output generated by script, otherwise //rest of output will land inside PNG binary, breaking it for sure if (0) { QRcode::png("PHP QR Code :)"); } //show benchmark if (0) { QRtools::timeBenchmark(); } //rebuild cache if (0) { QRtools::buildCache(); } //code generated in text mode - as a binary table //then displayed out as HTML using Unicode block building chars :) if (0) { $qr = new QRencode(); $tab = $qr->encode('PHP QR Code :)'); QRspec::debug($tab, true); } }
* modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 3 of the License, or any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ include "qrlib.php"; if (isset($_REQUEST['svg'])) { echo QRcode::svg("stuff", false, QR_ECLEVEL_H, 10, 1); die; } echo "<h1>PHP QR Code</h1><hr/>"; //set it to writable location, a place for temp generated PNG files $PNG_TEMP_DIR = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR; //html PNG location prefix $PNG_WEB_DIR = 'temp/'; //ofcourse we need rights to create temp dir if (!file_exists($PNG_TEMP_DIR)) { mkdir($PNG_TEMP_DIR); } $filename = $PNG_TEMP_DIR . 'test.png'; //processing form input //remember to sanitize user input in real-life solution !!! $errorCorrectionLevel = 'L';
public static function generateQR($uri, $data_path = null, $identifier_path = null) { /*** * Generate a QR code from a string * * @param string $uri * @param string $data_path The path to the write directory * @param string $identifier_path An optional subdirectory for paths; makes loops easier * @returns array with the main results in "svg" and "raw" keys, with a Google fallback in the "url" key ***/ try { require_once dirname(__FILE__) . '/../qr/qrlib.php'; if (!class_exists('Stronghash')) { require_once dirname(__FILE__) . '/../core/stronghash/php-stronghash.php'; } $salt = Stronghash::createSalt(); $persistent = !empty($data_path); if (!$persistent) { $tmp_dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR; if (!file_exists($tmp_dir)) { if (!mkdir($tmp_dir)) { # Could not write to temporary path throw new Exception("Could not write to '{$tmp_dir}'"); } } $web_dir = 'temp/'; $filename = $tmp_dir . sha1($salt) . '.png'; } else { # Persistent file $file = $sha1($salt . $uri); if (substr($data_path, -1) != '/') { $data_path .= DIRECTORY_SEPARATOR; } $full_path = empty($identifier_path) ? $data_path : $data_path . $identifier_path . DIRECTORY_SEPARATOR; if (!file_exists($full_path)) { if (!mkdir($full_path)) { # Could not write to the storage path throw new Exception("Could not write to '{$full_path}'"); } } $filename = $full_path . $file . '.png'; } $svg = QRcode::svg($uri, false, QR_ECLEVEL_H, 8, 1); if (function_exists('ImageCreate')) { QRcode::png($uri, $filename, QR_ECLEVEL_H, 8, 1); } $raw = base64_encode(file_get_contents($filename)); $raw = 'data:image/png;base64,' . $raw; if (!$persistent) { unlink($filename); } # As a final option, get a URL fallback # https://developers.google.com/chart/infographics/docs/qr_codes?csw=1 $url = 'https://chart.googleapis.com/chart?cht=qr&chs=500x500&chld=H&chl=' . $uri; return array('status' => true, 'uri' => $uri, 'svg' => $svg, 'raw' => $raw, 'url' => $url); } catch (Exception $e) { return array('status' => false, 'human_error' => 'Unable to generate QR code', 'error' => $e->getMessage(), 'uri' => $uri, 'identifier' => $identifier, 'persistent' => $persistent); } }
if (ISQ::$social['googleplus']) { echo '<span onclick="window.open(\'https://plus.google.com/share?url=' . $encoded_shorturl . '\',\'_blank\',\'width=550,height=380\')" class="button social-button googleplus" title="Share on LinkedIn">' . file_get_contents('public/images/googleplus.svg') . '</span>'; } ?> </div> <?php if (ISQ::$general['qr']) { ?> <!-- QR code --> <h2><?php yourls_e('QR code', 'isq_translation'); ?> </h2> <p><?php yourls_e('Share your link with external devices', 'isq_translation'); ?> </p> <?php // PHP QR Code is LGPL licensed include 'public/phpqrcode/qrlib.php'; echo QRcode::svg($shorturl, 'url-qr-code', false, QR_ECLEVEL_L, '600'); } ?> </div> <?php include 'footer.php';
<?php include '../lib/full/qrlib.php'; include 'config.php'; // Compressed SVGZ support $tempDir = EXAMPLE_TMP_SERVERPATH; $dataText = 'PHP QR Code :)'; $svgTagId = 'id-of-svg'; $saveToFile = '204_demo.svgz'; $width = false; // auto calculated $size = false; $margin = 4; $compress = true; // it is saved to file but also returned from function $svgCode = QRcode::svg($dataText, false, $tempDir . $saveToFile, QR_ECLEVEL_L, $width, $size, $margin, $compress); $svgCodeFromFile = file_get_contents($tempDir . $saveToFile); $sizeOrig = strlen($svgCode); $sizeGzip = strlen($svgCodeFromFile); $saved = ($sizeOrig + 1 - $sizeGzip) / ($sizeOrig + 1) * 100; echo '<b>Raw SVG size:</b> ' . $sizeOrig . ' B <br/>'; echo '<b>Compressed SVGZ size:</b> ' . $sizeGzip . ' B <br/>'; echo '<b>Saved:</b>: ' . number_format($saved, 3) . '%<br/>'; echo '<br/>Notice: your server need to be configured for SVGZ output - look into sfproxy.php<br/>'; echo '<iframe src="' . EXAMPLE_TMP_URLRELPATH . $saveToFile . '" style="width:98%;height:160px"></iframe>';
<?php include '../lib/full/qrlib.php'; // SVG file format support $svgCode = QRcode::svg('PHP QR Code :)'); echo $svgCode;
$shorturl = isset($return['shorturl']) ? $return['shorturl'] : ''; $message = isset($return['message']) ? $return['message'] : ''; $title = isset($return['title']) ? $return['title'] : ''; $status = isset($return['status']) ? $return['status'] : ''; // URL encoded links used in the social sharing buttons $encoded_shorturl = urlencode($shorturl); $encoded_title = urlencode($title); // QR code shenanigans if (ISQ::$general['qr']) { // PHP QR Code is LGPL licensed include 'public/phpqrcode/qrlib.php'; $qrContainerId = 'url-qr-code'; $saveToFile = false; $imageWidth = 600; // in pixels $qrCode = QRcode::svg($url, $qrContainerId, $saveToFile, QR_ECLEVEL_L, $imageWidth); } ?> <div class="content"> <!-- Error reporting --> <?php isset($error) ? $error : ''; ?> <!-- Default output --> <h2><?php yourls_e('Your short URL', 'isq_translation'); ?> </h2>
/** * @Route("/qrset/{qrset}/generate", name="generate_qr") * @Secure(roles="ROLE_USER") */ public function generateQr(QrSet $qrset) { if ($this->getRequest()->get('quantity') == null) { echo 'Quantity is required'; die; } $qrImages = array(); $toFlush = array($qrset); for ($i = $qrset->getQrcodes()->count(); $i < $qrset->getQrcodes()->count() + (int) $this->getRequest()->get('quantity'); $i++) { // Create the QR Entity $qrCode = new QrCode(); $qrCode->setQrset($qrset); $hash = hash_hmac('sha1', $qrset->getId() . '_' . $i, $qrset->getCourse()->getUser()->getPassword()); $qrCode->setCode($hash); $this->container->get('doctrine')->getManager()->persist($qrCode); $toFlush[] = $qrCode; // Generate QR image based on the created entity $qrImage = array(); $fileName = tempnam($this->container->getParameter("kernel.cache_dir"), 'qrimg'); $link = $this->container->get('router')->generate('redeem', array('hash' => $qrCode->getCode()), true); \QRcode::svg($link, $fileName); $qrImage['link'] = $qrCode->getCode(); $qrImage['svg'] = file_get_contents($fileName); $xml = simplexml_load_string($qrImage['svg']); $xml['width'] = 135; $xml['height'] = 135; $qrImage['svg'] = $xml->asXML(); $qrImages[] = $qrImage; } $this->container->get('doctrine')->getManager()->persist($qrset); $this->container->get('doctrine')->getManager()->flush($toFlush); $html = $this->container->get('templating')->render('PixelbonusSiteBundle:QR:qr_template.html.twig', array('qrImages' => $qrImages)); if ($this->getRequest()->get('html') == 'true') { return new Response($html); } else { $pdf = $this->container->get('knp_snappy.pdf')->getOutputFromHtml($html); return new Response($pdf, 200, array('Content-Type' => 'application/pdf', 'Content-Disposition' => sprintf('attachment;filename="%s.pdf"', 'qr'))); } }
<?php // include php class for QR code generation include './phpqrcode.php'; // get bitcoin address $bitcoinaddress = htmlspecialchars($_GET["bitcoinaddress"]); // output page echo '<html><head><title>Bitcoin</title></head><body>'; echo '<div style="text-align:center;"><h1>Bitcoin</h1></div>'; echo '<p style="text-align:center;"><a href="bitcoin:' . $bitcoinaddress . '">bitcoin:' . $bitcoinaddress . '</a></p>'; echo '<p style="text-align:center;">'; QRcode::svg($bitcoinaddress, false, 'h', 5); echo '</p>'; echo '<p style="text-align:center;">Information: <a href="https://www.bitcoin.org" target="_blank">bitcoin.org</a></p>'; echo '</body></html>';