Bagaimana bina QR code dalam PHP

Bagaimana bina QR code dalam PHP

Amat mudah untuk membina QRCode dalam PHP.

qrcode_preview

Caranya menggunakan API yang telah disediakan oleh Google.

 

DEMO

Dua fail yang terlibat :

index.php

<?php
include "qrcode.php"; 
$url  = (isset($_POST['url'])) ? trim($_POST['url']): 'http://www.belajarphp.com';
$size = (isset($_POST['size'])) ? trim($_POST['size']): '100';
 
$ex1 = new QRCODE($url,$size); 
$ex1 = new QRCODE('http://belajarphp.com',100); 
$img1 = "<img src=".$ex1->generate().">";
?>
<form method="post" action="">
<table>
    <tr>
        <td>Ayat/URL</td>
        <td><input type="text" name="url" value="<?PHP echo $url; ?>"/></td>
    </tr>          
    <tr>
        <td>Saiz</td>
        <td><input type="text" name="size"  value="<?PHP echo $size; ?>"/> <i>min: 100 and max: 800</i></td>
    </tr>
    <tr>
        <td>&nbsp;</td>
        <td><input type="submit" name="generate" value="Bina QRCode" /></td>
    </tr>
</table>
<p><small>Powered By : www.BelajarPHP.com</small></p>
</form>
<table>
    <tr>
        <td><?php echo $img1; ?></td>
    </tr>          
</table>

 

Fail seterusnya

qrcode.php

 

<?php 
class QRCODE { 
 
    protected $size; 
    protected $data; 
    protected $encoding; 
    protected $errorCorrectionLevel; 
    protected $marginInRows; 
    protected $debug; 
 
    public function __construct($data='http://www.belajarphp.com',$size='300',$encoding='UTF-8',$errorCorrectionLevel='L',$marginInRows=4,$debug=false) { 
 
        $this->data=urlencode($data); 
        $this->size=($size>100 && $size<800)? $size : 300; 
        $this->encoding=($encoding == 'Shift_JIS' || $encoding == 'ISO-8859-1' || $encoding == 'UTF-8') ? $encoding : 'UTF-8'; 
        $this->errorCorrectionLevel=($errorCorrectionLevel == 'L' || $errorCorrectionLevel == 'M' || $errorCorrectionLevel == 'Q' || $errorCorrectionLevel == 'H') ?  $errorCorrectionLevel : 'L';
        $this->marginInRows=($marginInRows>0 && $marginInRows<10) ? $marginInRows:4; 
        $this->debug = ($debug==true)? true:false;     
    } 
 
    public function generate(){ 
 
        $QRLink = "https://chart.googleapis.com/chart?cht=qr&chs=".$this->size."x".$this->size.                 
                   "&chl=" . $this->data .  
                   "&choe=" . $this->encoding . 
                   "&chld=" . $this->errorCorrectionLevel . "|" . $this->marginInRows; 
        if ($this->debug) echo   $QRLink;          
        return $QRLink; 
    } 
}
?>

 

 

Leave a Reply

comment-avatar

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.