1<?php
2/*
3 * PHP QR Code encoder
4 *
5 * Main encoder classes.
6 *
7 * Based on libqrencode C library distributed under LGPL 2.1
8 * Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi <fukuchi@megaui.net>
9 *
10 * PHP QR Code is distributed under LGPL 3
11 * Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
12 *
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 3 of the License, or any later version.
17 *
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
22 *
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 */
27
28namespace PHPQRCode;
29
30use Exception;
31
32class QRencode {
33
34    public $casesensitive = true;
35    public $eightbit = false;
36
37    public $version = 0;
38    public $size = 3;
39    public $margin = 4;
40
41    public $structured = 0; // not supported yet
42
43    public $level = Constants::QR_ECLEVEL_L;
44    public $hint = Constants::QR_MODE_8;
45
46    //----------------------------------------------------------------------
47    public static function factory($level = Constants::QR_ECLEVEL_L, $size = 3, $margin = 4)
48    {
49        $enc = new QRencode();
50        $enc->size = $size;
51        $enc->margin = $margin;
52
53        switch ($level.'') {
54            case '0':
55            case '1':
56            case '2':
57            case '3':
58                    $enc->level = $level;
59                break;
60            case 'l':
61            case 'L':
62                    $enc->level = Constants::QR_ECLEVEL_L;
63                break;
64            case 'm':
65            case 'M':
66                    $enc->level = Constants::QR_ECLEVEL_M;
67                break;
68            case 'q':
69            case 'Q':
70                    $enc->level = Constants::QR_ECLEVEL_Q;
71                break;
72            case 'h':
73            case 'H':
74                    $enc->level = Constants::QR_ECLEVEL_H;
75                break;
76        }
77
78        return $enc;
79    }
80
81    //----------------------------------------------------------------------
82    public function encodeRAW($intext, $outfile = false)
83    {
84        $code = new QRcode();
85
86        if($this->eightbit) {
87            $code->encodeString8bit($intext, $this->version, $this->level);
88        } else {
89            $code->encodeString($intext, $this->version, $this->level, $this->hint, $this->casesensitive);
90        }
91
92        return $code->data;
93    }
94
95    //----------------------------------------------------------------------
96    public function encode($intext, $outfile = false)
97    {
98        $code = new QRcode();
99
100        if($this->eightbit) {
101            $code->encodeString8bit($intext, $this->version, $this->level);
102        } else {
103            $code->encodeString($intext, $this->version, $this->level, $this->hint, $this->casesensitive);
104        }
105
106        QRtools::markTime('after_encode');
107
108        if ($outfile!== false) {
109            file_put_contents($outfile, join("\n", QRtools::binarize($code->data)));
110        } else {
111            return QRtools::binarize($code->data);
112        }
113    }
114
115    //----------------------------------------------------------------------
116    public function encodePNG($intext, $outfile = false,$saveandprint=false)
117    {
118        try {
119            ob_start();
120            $tab = $this->encode($intext);
121            $err = ob_get_contents();
122            ob_end_clean();
123
124            if ($err != '')
125                QRtools::log($outfile, "ERROR: " . $err);
126
127            $maxSize = (int)(Constants::QR_PNG_MAXIMUM_SIZE / (count($tab)+2*$this->margin));
128
129            QRimage::png($tab, $outfile, min(max(1, $this->size), $maxSize), $this->margin,$saveandprint);
130        } catch (Exception $e) {
131            echo $e->getMessage();
132            die();
133
134            QRtools::log($outfile, $e->getMessage());
135        }
136    }
137}
138