G-from-D
G-from-D

Reputation: 11

How can I generate QR Codes from a string in PHP?

I've been playing around trying to generate QR Codes from a string without any luck. I'm using CodeIgniter. I've tried 2 different packages from Packagist, bacon/bacon-qr-code, and endroid/qrcode. Below is the code in my controller for Bacon :

$renderer = new \BaconQrCode\Renderer\Image\Png();
        $renderer->setHeight(256);
        $renderer->setWidth(256);
        $writer = new \BaconQrCode\Writer($renderer);
        $writer->writeFile('Hello World!', 'qrcode.png');

When I run this code I get the error 'The phpass class file was not found'. So I then installed phpass through spark, and I still get the same error. Can anyone tell me what I'm doing wrong?

Upvotes: 1

Views: 4871

Answers (2)

Tpojka
Tpojka

Reputation: 7111

First one is working as well (probably second one too). You need to use it this way (at least):

APPPATH . 'libraries/Qrcode.php'

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

use \BaconQrCode\Renderer\Image\Png;
use \BaconQrCode\Writer;

class Qrcode
{
    public function test()
    {
        $renderer = new Png();

        $renderer->setHeight(256);
        $renderer->setWidth(256);
        $writer = new Writer($renderer);
        $writer->writeFile('Hello World!', 'qrcode.png');
        //var_dump($writer);
    }
}

APPPATH . 'controllers/Test.php'

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Test extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $this->qrcode();
    }

    public function qrcode()
    {
        $this->load->library('qrcode');

        $this->qrcode->test();
    }
}

And image will be generated in FCPATH . 'qrcode.png' file.

Upvotes: 1

Muhammad Aqib
Muhammad Aqib

Reputation: 21

you can generate QR Codes of string using Google QR Codes API.

https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl=Hello+world&choe=UTF-8
  • 300x300 is your image size.
  • chl - a url encoded string to convert it into qr code.

Upvotes: 0

Related Questions