Passed
Push — master ( 25c5f3...d91862 )
by Saepul
07:05 queued 02:03
created

qrvect::eps()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 13
nc 3
nop 8
dl 0
loc 16
rs 9.8333
c 1
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/*
3
 * PHP QR Code encoder
4
 *
5
 * Image output of code using GD2
6
 *
7
 * PHP QR Code is distributed under LGPL 3
8
 * Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
9
 *
10
 * This library is free software; you can redistribute it and/or
11
 * modify it under the terms of the GNU Lesser General Public
12
 * License as published by the Free Software Foundation; either
13
 * version 3 of the License, or any later version.
14
 *
15
 * This library is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18
 * Lesser General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Lesser General Public
21
 * License along with this library; if not, write to the Free Software
22
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23
 */
24
25
    define('QR_VECT', true);
26
27
    class qrvect
28
    {
29
        //----------------------------------------------------------------------
30
        public static function eps($frame, $filename = false, $pixelPerPoint = 4, $outerFrame = 4, $saveandprint = false, $back_color = 0xFFFFFF, $fore_color = 0x000000, $cmyk = false)
31
        {
32
            $vect = self::vectEPS($frame, $pixelPerPoint, $outerFrame, $back_color, $fore_color, $cmyk);
33
34
            if ($filename === false) {
35
                header('Content-Type: application/postscript');
36
                header('Content-Disposition: filename="qrcode.eps"');
37
                echo $vect;
38
            } else {
39
                if ($saveandprint === true) {
40
                    QRtools::save($vect, $filename);
41
                    header('Content-Type: application/postscript');
42
                    header('Content-Disposition: filename="qrcode.eps"');
43
                    echo $vect;
44
                } else {
45
                    QRtools::save($vect, $filename);
46
                }
47
            }
48
        }
49
50
        //----------------------------------------------------------------------
51
        private static function vectEPS($frame, $pixelPerPoint = 4, $outerFrame = 4, $back_color = 0xFFFFFF, $fore_color = 0x000000, $cmyk = false)
52
        {
53
            $h = count($frame);
54
            $w = strlen($frame[0]);
55
56
            $imgW = $w + 2 * $outerFrame;
57
            $imgH = $h + 2 * $outerFrame;
58
59
            if ($cmyk) {
60
                // convert color value into decimal eps format
61
                $c = round((($fore_color & 0xFF000000) >> 16) / 255, 5);
62
                $m = round((($fore_color & 0x00FF0000) >> 16) / 255, 5);
63
                $y = round((($fore_color & 0x0000FF00) >> 8) / 255, 5);
64
                $k = round(($fore_color & 0x000000FF) / 255, 5);
65
                $fore_color_string = $c.' '.$m.' '.$y.' '.$k.' setcmykcolor'."\n";
66
67
                // convert color value into decimal eps format
68
                $c = round((($back_color & 0xFF000000) >> 16) / 255, 5);
69
                $m = round((($back_color & 0x00FF0000) >> 16) / 255, 5);
70
                $y = round((($back_color & 0x0000FF00) >> 8) / 255, 5);
71
                $k = round(($back_color & 0x000000FF) / 255, 5);
72
                $back_color_string = $c.' '.$m.' '.$y.' '.$k.' setcmykcolor'."\n";
73
            } else {
74
                // convert a hexadecimal color code into decimal eps format (green = 0 1 0, blue = 0 0 1, ...)
75
                $r = round((($fore_color & 0xFF0000) >> 16) / 255, 5);
76
                $b = round((($fore_color & 0x00FF00) >> 8) / 255, 5);
77
                $g = round(($fore_color & 0x0000FF) / 255, 5);
78
                $fore_color_string = $r.' '.$b.' '.$g.' setrgbcolor'."\n";
79
80
                // convert a hexadecimal color code into decimal eps format (green = 0 1 0, blue = 0 0 1, ...)
81
                $r = round((($back_color & 0xFF0000) >> 16) / 255, 5);
82
                $b = round((($back_color & 0x00FF00) >> 8) / 255, 5);
83
                $g = round(($back_color & 0x0000FF) / 255, 5);
84
                $back_color_string = $r.' '.$b.' '.$g.' setrgbcolor'."\n";
85
            }
86
87
            $output =
88
            '%!PS-Adobe EPSF-3.0'."\n".
89
            '%%Creator: PHPQrcodeLib'."\n".
90
            '%%Title: QRcode'."\n".
91
            '%%CreationDate: '.date('Y-m-d')."\n".
92
            '%%DocumentData: Clean7Bit'."\n".
93
            '%%LanguageLevel: 2'."\n".
94
            '%%Pages: 1'."\n".
95
            '%%BoundingBox: 0 0 '.$imgW * $pixelPerPoint.' '.$imgH * $pixelPerPoint."\n";
96
97
            // set the scale
98
            $output .= $pixelPerPoint.' '.$pixelPerPoint.' scale'."\n";
99
            // position the center of the coordinate system
100
101
            $output .= $outerFrame.' '.$outerFrame.' translate'."\n";
102
103
            // redefine the 'rectfill' operator to shorten the syntax
104
            $output .= '/F { rectfill } def'."\n";
105
106
            // set the symbol color
107
            $output .= $back_color_string;
108
            $output .= '-'.$outerFrame.' -'.$outerFrame.' '.($w + 2 * $outerFrame).' '.($h + 2 * $outerFrame).' F'."\n";
109
110
            // set the symbol color
111
            $output .= $fore_color_string;
112
113
            // Convert the matrix into pixels
114
115
            for ($i = 0; $i < $h; $i++) {
116
                for ($j = 0; $j < $w; $j++) {
117
                    if ($frame[$i][$j] == '1') {
118
                        $y = $h - 1 - $i;
119
                        $x = $j;
120
                        $output .= $x.' '.$y.' 1 1 F'."\n";
121
                    }
122
                }
123
            }
124
125
            $output .= '%%EOF';
126
127
            return $output;
128
        }
129
130
        //----------------------------------------------------------------------
131
        public static function svg($frame, $filename, $pixelPerPoint, $outerFrame, $saveandprint, $back_color, $fore_color)
132
        {
133
            $vect = self::vectSVG($frame, $pixelPerPoint, $outerFrame, $back_color, $fore_color);
134
135
            if ($filename === false) {
136
                header('Content-Type: image/svg+xml');
137
                //header('Content-Disposition: attachment, filename="qrcode.svg"');
138
                echo $vect;
139
            } else {
140
                if ($saveandprint === true) {
141
                    QRtools::save($vect, $filename);
142
                    header('Content-Type: image/svg+xml');
143
                    //header('Content-Disposition: filename="'.$filename.'"');
144
                    echo $vect;
145
                } else {
146
                    QRtools::save($vect, $filename);
147
                }
148
            }
149
        }
150
151
        //----------------------------------------------------------------------
152
        private static function vectSVG($frame, $pixelPerPoint = 4, $outerFrame = 4, $back_color = 0xFFFFFF, $fore_color = 0x000000)
153
        {
154
            $h = count($frame);
155
            $w = strlen($frame[0]);
156
157
            $imgW = $w + 2 * $outerFrame;
158
            $imgH = $h + 2 * $outerFrame;
159
160
            $output =
0 ignored issues
show
Unused Code introduced by
The assignment to $output is dead and can be removed.
Loading history...
161
            '<?xml version="1.0" encoding="utf-8"?>'."\n".
162
            '<svg version="1.1" baseProfile="full"  width="'.$imgW * $pixelPerPoint.'" height="'.$imgH * $pixelPerPoint.'" viewBox="0 0 '.$imgW * $pixelPerPoint.' '.$imgH * $pixelPerPoint.'"
163
             xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ev="http://www.w3.org/2001/xml-events">'."\n".
164
            '<desc></desc>'."\n";
165
166
            $output =
167
            '<?xml version="1.0" encoding="utf-8"?>'."\n".
168
            '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">'."\n".
169
            '<svg xmlns="http://www.w3.org/2000/svg" xml:space="preserve" xmlns:xlink="http://www.w3.org/1999/xlink" width="'.$imgW * $pixelPerPoint.'" height="'.$imgH * $pixelPerPoint.'" viewBox="0 0 '.$imgW * $pixelPerPoint.' '.$imgH * $pixelPerPoint.'">'."\n".
170
            '<desc></desc>'."\n";
171
172
            if (!empty($back_color)) {
173
                $backgroundcolor = str_pad(dechex($back_color), 6, '0', STR_PAD_LEFT);
174
                $output .= '<rect width="'.$imgW * $pixelPerPoint.'" height="'.$imgH * $pixelPerPoint.'" fill="#'.$backgroundcolor.'" cx="0" cy="0" />'."\n";
175
            }
176
177
            $output .=
178
            '<defs>'."\n".
179
            '<rect id="p" width="'.$pixelPerPoint.'" height="'.$pixelPerPoint.'" />'."\n".
180
            '</defs>'."\n".
181
            '<g fill="#'.str_pad(dechex($fore_color), 6, '0', STR_PAD_LEFT).'">'."\n";
182
183
            // Convert the matrix into pixels
184
185
            for ($i = 0; $i < $h; $i++) {
186
                for ($j = 0; $j < $w; $j++) {
187
                    if ($frame[$i][$j] == '1') {
188
                        $y = ($i + $outerFrame) * $pixelPerPoint;
189
                        $x = ($j + $outerFrame) * $pixelPerPoint;
190
                        $output .= '<use x="'.$x.'" y="'.$y.'" xlink:href="#p" />'."\n";
191
                    }
192
                }
193
            }
194
            $output .=
195
            '</g>'."\n".
196
            '</svg>';
197
198
            return $output;
199
        }
200
    }
201