What's new

Closed QR Code PHP Class Tut

Status
Not open for further replies.

claudioxcore

Addict
Joined
Jul 9, 2015
Posts
33
Reaction
5
Points
75
Mga bro simple tut lang for generating qr codes using PHP.

First, open a new php file and name it to qrcode.php, then paste mo tong code sa baba.
PHP:
final class BarcodeQR {
    /**
     * Chart API URL
     */
    const API_CHART_URL = "http://chart.apis.google.com/chart";

    /**
     * Code data
     *
     * @var string $_data
     */
    private $_data;

    /**
     * Bookmark code
     *
     * @param string $title
     * @param string $url
     */
    public function bookmark($title = null, $url = null) {
        $this->_data = "MEBKM:TITLE:{$title};URL:{$url};;";
    }

    /**
    *PRODUCT code
    *
    * @param string $name
    * @param string $id
    * @param string $price
    */

    public function product($name = null, $id = null, $price = null) {
        $this->_data = "Prodcut Name:". $name. "ID:". $id. "Price:". $price;
    }

    /**
    *USER code
    *
    *@param string $fname
    *@param string $lname
    */

    public function user($fname = null, $lname = null) {
        $this->_data = "Firstname". $fname. "Lastname:". $lname;
    }


    /**
     * MECARD code
     *
     * @param string $name
     * @param string $address
     * @param string $phone
     * @param string $email
     */

    public function contact($name = null, $address = null, $phone = null, $email = null) {
        $this->_data = "MECARD:N:{$name};ADR:{$address};TEL:{$phone};EMAIL:{$email};;";
    }

    /**
     * Create code with GIF, JPG, etc.
     *
     * @param string $type
     * @param string $size
     * @param string $content
     */
    public function content($type = null, $size = null, $content = null) {
        $this->_data = "CNTS:TYPE:{$type};LNG:{$size};BODY:{$content};;";
    }

    /**
     * Generate QR code image
     *
     * @param int $size
     * @param string $filename
     * @return bool
     */
    public function draw($size = 150, $filename = null) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, self::API_CHART_URL);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "chs={$size}x{$size}&cht=qr&chl=" . urlencode($this->_data));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        $img = curl_exec($ch);
        curl_close($ch);

        if($img) {
            if($filename) {
                if(!preg_match("#\.png$#i", $filename)) {
                    $filename .= ".png";
                }
               
                return file_put_contents($filename, $img);
            } else {
                header("Content-type: image/png");
                print $img;
                return true;
            }
        }

        return false;
    }

    /**
     * Email address code
     *
     * @param string $email
     * @param string $subject
     * @param string $message
     */
    public function email($email = null, $subject = null, $message = null) {
        $this->_data = "MATMSG:TO:{$email};SUB:{$subject};BODY:{$message};;";
        var_dump($this->_data);
    }

    /**
     * Geo location code
     *
     * @param string $lat
     * @param string $lon
     * @param string $height
     */
    public function geo($lat = null, $lon = null, $height = null) {
        $this->_data = "GEO:{$lat},{$lon},{$height}";
    }

    /**
     * Telephone number code
     *
     * @param string $phone
     */
    public function phone($phone = null) {
        $this->_data = "TEL:{$phone}";
    }

    /**
     * SMS code
     *
     * @param string $phone
     * @param string $text
     */
    public function sms($phone = null, $text = null) {
        $this->_data = "SMSTO:{$phone}:{$text}";
    }

    /**
     * Text code
     *
     * @param string $text
     */
    public function text($text = null) {
        $this->_data = $text;
    }

    /**
     * URL code
     *
     * @param string $url
     */
    public function url($url = null) {
        $this->_data = preg_match("#^https?\:\/\/#", $url) ? $url : "http://{$url}";
    }

    /**
     * Wifi code
     *
     * @param string $type
     * @param string $ssid
     * @param string $password
     */
    public function wifi($type = null, $ssid = null, $password = null) {
        $this->_data = "WIFI:T:{$type};S{$ssid};{$password};;";
    }
}
?>

Example Code: name it index.php:
PHP:
// include qrcode class
include "qrcode.php";

// set BarcodeQR object
$qr = new BarcodeQR();

// create URL QR code
$qr->url("www.fcmagallen.zz.mu");

// display new QR code image
$qr->draw();

Well, madali lang to initindihin. Run nyo lang yan code na yan automatic generate yan ng qr code in png format.

Method List:
  1. QR code with link. ex:
    PHP:
    $qr->url("www.fcmagallen.zz.mu");
    $qr->draw();
  2. QR code with bookmark. ex:
    PHP:
    $qr->bookmark("Title", "Address url");
    $qr->draw();
  3. QR code with text. ex:
    PHP:
    $qr->text("Some texts goes here..");
    $qr->draw();
  4. QR code with sms. ex:
    PHP:
    $qr->sms("123345678", "Text message goes here..");
    $qr->draw();
  5. QR code with phone number. ex:
    PHP:
    $qr->phone_number("123345678");
    $qr->draw();
  6. QR code with contact info. ex:
    PHP:
    $qr->contact_info("claudioxcore", "Stadiona street 15", "123345678", "user@example.com");
    $qr->draw();
  7. QR code with email. ex:
    PHP:
    $qr->email("user@example.com", "Email subject", "Email text, text, text");
    $qr->draw();
  8. QR code with geolocation. ex:
    PHP:
    $qr->geo(46.404723, -105.838165, 15);
    $qr->draw();
  9. QR code with wifi info. ex:
    PHP:
    $qr->wifi("WEP", "home_network", "123456");
    $qr-draw();
 
Dear claudioxcore,

Since 2 years have passed since the last reply in this thread, I am locking it to prevent necroposting. Feel free to start a new thread or contact any forum staff if you want this to be reopened.

Thread closed.
 
Status
Not open for further replies.

Similar threads

Back
Top