Laman

Create Your Own QR Code With Logo Centered With PHP


I just want to know how to create QR Code, so I search to Google.com, first time I find online QR Code generate by Google API, Source code here(Github). I think always online is not good, so I search again then I find "phpqrcode", PHP QR Code is open source (LGPL) library for generating QR Code, 2-dimensional barcode. This is the web page.
http://phpqrcode.sourceforge.net/
The web page tell Some of library features includes:
  • Supports QR Code versions (size) 1-40
  • Numeric, Alphanumeric, 8-bit and Kanji encoding. (Kanji encoding was not fully tested, if you are japan-encoding enabled you can contribute by verifing it :) )
  • Implemented purely in PHP, no external dependencies except GD2
  • Exports to PNG, JPEG images, also exports as bit-table
  • TCPDF 2-D barcode API integration
  • Easy to configure
  • Data cache for calculation speed-up
  • Provided merge tool helps deploy library as a one big dependency-less file, simple to "include and do not wory"
  • Debug data dump, error logging, time benchmarking
  • API documentation
  • Detailed examples
  • 100% Open Source, LGPL Licensed
To minimal using just include qrlib.php
then write code
QRcode::png('some othertext 1234'); //creates code image and outputs it directly into browser

To adding image in the center of QR Code image I using php function imagecopyresampled
// === Adding image to qrcode
$QR = imagecreatefrompng($imgname);
if($logo !== FALSE){
    $logopng = imagecreatefrompng($logo);
    $QR_width = imagesx($QR);
    $QR_height = imagesy($QR);
    $logo_width = imagesx($logopng);
    $logo_height = imagesy($logopng);
   
    list($newwidth, $newheight) = getimagesize($logo);
    $out = imagecreatetruecolor($QR_width, $QR_width);
    imagecopyresampled($out, $QR, 0, 0, 0, 0, $QR_width, $QR_height, $QR_width, $QR_height);
    imagecopyresampled($out, $logopng, $QR_width/2.65, $QR_height/2.65, 0, 0, $QR_width/4, $QR_height/4, $newwidth, $newheight);
   
}
imagepng($out,$imgname);
imagedestroy($out);


Then, to chage color of qrcode image with php, use php function imagecolorat
// === Change image color
$im = imagecreatefrompng($imgname);
$r = 44;$g = 62;$b = 80;
for($x=0;$x<imagesx($im);++$x){
    for($y=0;$y<imagesy($im);++$y){
        $index     = imagecolorat($im, $x, $y);
        $c       = imagecolorsforindex($im, $index);
        if(($c['red'] < 100) && ($c['green'] < 100) && ($c['blue'] < 100)) { // dark colors
            // here we use the new color, but the original alpha channel
            $colorB = imagecolorallocatealpha($im, 0x12, 0x2E, 0x31, $c['alpha']);
            imagesetpixel($im, $x, $y, $colorB);
        }
    }
}
imagepng($im,$imgname);
imagedestroy($im);


I adding the function to convert png image to base64, qrcode image is saved as png file, just 3 line to convert png image to base64 code.
// === Convert Image to base64
$type = pathinfo($imgname, PATHINFO_EXTENSION);
$data = file_get_contents($imgname);
$imgbase64 = 'data:image/' . $type . ';base64,' . base64_encode($data);


There all done, to showing image use tag <img/>
 // === Show image
echo "<img src='$imgbase64' style='position:relative;display:block;width:240px;height:240px;margin:160px auto;'>";


Complete Project here(Github)

10 comments:

Silahkan