Anda pasti sering melihat QR Code produk barang-barang yang dijual di berbagai toko modern, baik online maupun offline. QR Code singkatan dari Quick Response Code yang merupakan perkembangan dari barcode karena semakin kompleksnya data yang bisa dimuat. Jika Anda pernah menggunakan whatsapp lewat web, tentu Anda pernah melihat QR Code ini.
Dengan PHP, kita bisa juga membuat kode tersebut. Cara paling mudah adalah menggunakan library Phpqrcode. Dengan library ini, kita tidak perlu lagi menginstal plugin atau ekstensi tertentu untuk PHP. Cukup gunakan file PHP yang berisi kelas-kelas untuk membuat QR Code tersebut.
Langkah pertama, download di github library tersebut.
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Demo QR Code</title> | |
</head> | |
<body> | |
<h1>PHP QR Code</h1> | |
<?php | |
//menentukan folder temporary untuk file gambar yang akan digenerate | |
$PNG_TEMP_DIR = dirname(__FILE__).DIRECTORY_SEPARATOR.'temp'.DIRECTORY_SEPARATOR; | |
//html PNG location prefix | |
$PNG_WEB_DIR = 'temp/'; | |
include "phpqrcode-master/qrlib.php"; | |
//jika folder belum ada, buat folder | |
if (!file_exists($PNG_TEMP_DIR)) | |
mkdir($PNG_TEMP_DIR); | |
$filename = $PNG_TEMP_DIR.'result.png'; | |
//memproses input form | |
//sanitasi dulu input dari user. Penting ini supaya tidak di-hack!!! | |
$errorCorrectionLevel = 'L'; | |
if (isset($_REQUEST['level']) && in_array($_REQUEST['level'], array('L','M','Q','H'))) | |
$errorCorrectionLevel = $_REQUEST['level']; | |
$matrixPointSize = 4; | |
if (isset($_REQUEST['size'])) | |
$matrixPointSize = min(max((int)$_REQUEST['size'], 1), 10); | |
if (isset($_REQUEST['data'])) { | |
//penting! | |
if (trim($_REQUEST['data']) == '') | |
die('data cannot be empty! <a href="?">back</a>'); | |
// user data | |
$filename = $PNG_TEMP_DIR.'test'.md5($_REQUEST['data'].'|'.$errorCorrectionLevel.'|'.$matrixPointSize).'.png'; | |
QRcode::png($_REQUEST['data'], $filename, $errorCorrectionLevel, $matrixPointSize, 2); | |
} else { | |
//default data | |
echo 'You can provide data in GET parameter: <a href="?data=like_that">like that</a><hr/>'; | |
QRcode::png('PHP QR Code :)', $filename, $errorCorrectionLevel, $matrixPointSize, 2); | |
} | |
//menampilkan file yang sudah dihasilkan | |
echo '<img src="'.$PNG_WEB_DIR.basename($filename).'" /><hr/>'; | |
//config form | |
echo '<form action="" method="post"> | |
Data: <textarea name="data">"'.(isset($_REQUEST['data'])?htmlspecialchars($_REQUEST['data']):'PHP QR Code :)').'"</textarea> | |
ECC: <select name="level"> | |
<option value="L"'.(($errorCorrectionLevel=='L')?' selected':'').'>L - smallest</option> | |
<option value="M"'.(($errorCorrectionLevel=='M')?' selected':'').'>M</option> | |
<option value="Q"'.(($errorCorrectionLevel=='Q')?' selected':'').'>Q</option> | |
<option value="H"'.(($errorCorrectionLevel=='H')?' selected':'').'>H - best</option> | |
</select> | |
Size: <select name="size">'; | |
for($i=1;$i<=10;$i++) | |
echo '<option value="'.$i.'"'.(($matrixPointSize==$i)?' selected':'').'>'.$i.'</option>'; | |
echo '</select> | |
<input type="submit" value="GENERATE"></form><hr/>'; | |
// benchmark | |
QRtools::timeBenchmark(); | |
?> | |
</body> | |
</html> |
Hasilnya seperti berikut, saya mencoba teks huruf cina juga:
Tidak ada komentar:
Posting Komentar