Amat mudah untuk membina QRCode dalam PHP.
Caranya menggunakan API yang telah disediakan oleh Google.
Dua fail yang terlibat :
index.php
<?php
include "qrcode.php";
$url = (isset($_POST['url'])) ? trim($_POST['url']): 'https://www.belajarphp.com';
$size = (isset($_POST['size'])) ? trim($_POST['size']): '100';
$ex1 = new QRCODE($url,$size);
$ex1 = new QRCODE('https://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> </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='https://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