BCGBarcode::setOffsetX()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
namespace tinymeng\code\Gateways\barcode;
3
/**
4
 *--------------------------------------------------------------------
5
 *
6
 * Base class for Barcode 1D and 2D
7
 *
8
 *--------------------------------------------------------------------
9
 * Copyright (C) Jean-Sebastien Goupil
10
 * http://www.barcodephp.com
11
 */
12
abstract class BCGBarcode {
13
    const COLOR_BG = 0;
14
    const COLOR_FG = 1;
15
16
    protected $colorFg, $colorBg;       // Color Foreground, Barckground
17
    protected $scale;                   // Scale of the graphic, default: 1
18
    protected $offsetX, $offsetY;       // Position where to start the drawing
19
    protected $labels = array();        // Array of BCGLabel
20
    protected $pushLabel = array(0, 0); // Push for the label, left and top
21
22
    /**
23
     * Constructor.
24
     */
25
    protected function __construct() {
26
        $this->setOffsetX(0);
27
        $this->setOffsetY(0);
28
        $this->setForegroundColor(0x000000);
29
        $this->setBackgroundColor(0xffffff);
30
        $this->setScale(1);
31
    }
32
33
    /**
34
     * Parses the text before displaying it.
35
     *
36
     * @param mixed $text
37
     */
38
    public function parse($text) {
0 ignored issues
show
Unused Code introduced by
The parameter $text is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

38
    public function parse(/** @scrutinizer ignore-unused */ $text) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
39
    }
40
41
    /**
42
     * Gets the foreground color of the barcode.
43
     *
44
     * @return BCGColor
45
     */
46
    public function getForegroundColor() {
47
        return $this->colorFg;
48
    }
49
50
    /**
51
     * Sets the foreground color of the barcode. It could be a BCGColor
52
     * value or simply a language code (white, black, yellow...) or hex value.
53
     *
54
     * @param mixed $code
55
     */
56
    public function setForegroundColor($code) {
57
        if ($code instanceof BCGColor) {
58
            $this->colorFg = $code;
59
        } else {
60
            $this->colorFg = new BCGColor($code);
61
        }
62
    }
63
64
    /**
65
     * Gets the background color of the barcode.
66
     *
67
     * @return BCGColor
68
     */
69
    public function getBackgroundColor() {
70
        return $this->colorBg;
71
    }
72
73
    /**
74
     * Sets the background color of the barcode. It could be a BCGColor
75
     * value or simply a language code (white, black, yellow...) or hex value.
76
     *
77
     * @param mixed $code
78
     */
79
    public function setBackgroundColor($code) {
80
        if ($code instanceof BCGColor) {
81
            $this->colorBg = $code;
82
        } else {
83
            $this->colorBg = new BCGColor($code);
84
        }
85
86
        foreach ($this->labels as $label) {
87
            $label->setBackgroundColor($this->colorBg);
88
        }
89
    }
90
91
    /**
92
     * Sets the color.
93
     *
94
     * @param mixed $fg
95
     * @param mixed $bg
96
     */
97
    public function setColor($fg, $bg) {
98
        $this->setForegroundColor($fg);
99
        $this->setBackgroundColor($bg);
100
    }
101
102
    /**
103
     * Gets the scale of the barcode.
104
     *
105
     * @return int
106
     */
107
    public function getScale() {
108
        return $this->scale;
109
    }
110
111
    /**
112
     * Sets the scale of the barcode in pixel.
113
     * If the scale is lower than 1, an exception is raised.
114
     *
115
     * @param int $scale
116
     */
117
    public function setScale($scale) {
118
        $scale = intval($scale);
119
        if ($scale <= 0) {
120
            throw new BCGArgumentException('The scale must be larger than 0.', 'scale');
121
        }
122
123
        $this->scale = $scale;
124
    }
125
126
    /**
127
     * Abstract method that draws the barcode on the resource.
128
     *
129
     * @param resource $im
130
     */
131
    public abstract function draw($im);
132
133
    /**
134
     * Returns the maximal size of a barcode.
135
     * [0]->width
136
     * [1]->height
137
     *
138
     * @param int $w
139
     * @param int $h
140
     * @return int[]
141
     */
142
    public function getDimension($w, $h) {
143
        $labels = $this->getBiggestLabels(false);
144
        $pixelsAround = array(0, 0, 0, 0); // TRBL
145
        if (isset($labels[BCGLabel::POSITION_TOP])) {
146
            $dimension = $labels[BCGLabel::POSITION_TOP]->getDimension();
147
            $pixelsAround[0] += $dimension[1];
148
        }
149
150
        if (isset($labels[BCGLabel::POSITION_RIGHT])) {
151
            $dimension = $labels[BCGLabel::POSITION_RIGHT]->getDimension();
152
            $pixelsAround[1] += $dimension[0];
153
        }
154
155
        if (isset($labels[BCGLabel::POSITION_BOTTOM])) {
156
            $dimension = $labels[BCGLabel::POSITION_BOTTOM]->getDimension();
157
            $pixelsAround[2] += $dimension[1];
158
        }
159
160
        if (isset($labels[BCGLabel::POSITION_LEFT])) {
161
            $dimension = $labels[BCGLabel::POSITION_LEFT]->getDimension();
162
            $pixelsAround[3] += $dimension[0];
163
        }
164
165
        $finalW = ($w + $this->offsetX) * $this->scale;
166
        $finalH = ($h + $this->offsetY) * $this->scale;
167
168
        // This section will check if a top/bottom label is too big for its width and left/right too big for its height
169
        $reversedLabels = $this->getBiggestLabels(true);
170
        foreach ($reversedLabels as $label) {
171
            $dimension = $label->getDimension();
172
            $alignment = $label->getAlignment();
173
            if ($label->getPosition() === BCGLabel::POSITION_LEFT || $label->getPosition() === BCGLabel::POSITION_RIGHT) {
174
                if ($alignment === BCGLabel::ALIGN_TOP) {
175
                    $pixelsAround[2] = max($pixelsAround[2], $dimension[1] - $finalH);
176
                } elseif ($alignment === BCGLabel::ALIGN_CENTER) {
177
                    $temp = ceil(($dimension[1] - $finalH) / 2);
178
                    $pixelsAround[0] = max($pixelsAround[0], $temp);
179
                    $pixelsAround[2] = max($pixelsAround[2], $temp);
180
                } elseif ($alignment === BCGLabel::ALIGN_BOTTOM) {
181
                    $pixelsAround[0] = max($pixelsAround[0], $dimension[1] - $finalH);
182
                }
183
            } else {
184
                if ($alignment === BCGLabel::ALIGN_LEFT) {
185
                    $pixelsAround[1] = max($pixelsAround[1], $dimension[0] - $finalW);
186
                } elseif ($alignment === BCGLabel::ALIGN_CENTER) {
187
                    $temp = ceil(($dimension[0] - $finalW) / 2);
188
                    $pixelsAround[1] = max($pixelsAround[1], $temp);
189
                    $pixelsAround[3] = max($pixelsAround[3], $temp);
190
                } elseif ($alignment === BCGLabel::ALIGN_RIGHT) {
191
                    $pixelsAround[3] = max($pixelsAround[3], $dimension[0] - $finalW);
192
                }
193
            }
194
        }
195
196
        $this->pushLabel[0] = $pixelsAround[3];
197
        $this->pushLabel[1] = $pixelsAround[0];
198
199
        $finalW = ($w + $this->offsetX) * $this->scale + $pixelsAround[1] + $pixelsAround[3];
200
        $finalH = ($h + $this->offsetY) * $this->scale + $pixelsAround[0] + $pixelsAround[2];
201
202
        return array($finalW, $finalH);
203
    }
204
205
    /**
206
     * Gets the X offset.
207
     *
208
     * @return int
209
     */
210
    public function getOffsetX() {
211
        return $this->offsetX;
212
    }
213
214
    /**
215
     * Sets the X offset.
216
     *
217
     * @param int $offsetX
218
     */
219
    public function setOffsetX($offsetX) {
220
        $offsetX = intval($offsetX);
221
        if ($offsetX < 0) {
222
            throw new BCGArgumentException('The offset X must be 0 or larger.', 'offsetX');
223
        }
224
225
        $this->offsetX = $offsetX;
226
    }
227
228
    /**
229
     * Gets the Y offset.
230
     *
231
     * @return int
232
     */
233
    public function getOffsetY() {
234
        return $this->offsetY;
235
    }
236
237
    /**
238
     * Sets the Y offset.
239
     *
240
     * @param int $offsetY
241
     */
242
    public function setOffsetY($offsetY) {
243
        $offsetY = intval($offsetY);
244
        if ($offsetY < 0) {
245
            throw new BCGArgumentException('The offset Y must be 0 or larger.', 'offsetY');
246
        }
247
248
        $this->offsetY = $offsetY;
249
    }
250
251
    /**
252
     * Adds the label to the drawing.
253
     *
254
     * @param BCGLabel $label
255
     */
256
    public function addLabel(BCGLabel $label) {
257
        $label->setBackgroundColor($this->colorBg);
258
        $this->labels[] = $label;
259
    }
260
261
    /**
262
     * Removes the label from the drawing.
263
     *
264
     * @param BCGLabel $label
265
     */
266
    public function removeLabel(BCGLabel $label) {
267
        $remove = -1;
268
        $c = count($this->labels);
269
        for ($i = 0; $i < $c; $i++) {
270
            if ($this->labels[$i] === $label) {
271
                $remove = $i;
272
                break;
273
            }
274
        }
275
276
        if ($remove > -1) {
277
            array_splice($this->labels, $remove, 1);
278
        }
279
    }
280
281
    /**
282
     * Clears the labels.
283
     */
284
    public function clearLabels() {
285
        $this->labels = array();
286
    }
287
288
    /**
289
     * Draws the text.
290
     * The coordinate passed are the positions of the barcode.
291
     * $x1 and $y1 represent the top left corner.
292
     * $x2 and $y2 represent the bottom right corner.
293
     *
294
     * @param resource $im
295
     * @param int $x1
296
     * @param int $y1
297
     * @param int $x2
298
     * @param int $y2
299
     */
300
    protected function drawText($im, $x1, $y1, $x2, $y2) {
301
        foreach ($this->labels as $label) {
302
            $label->draw($im,
303
                ($x1 + $this->offsetX) * $this->scale + $this->pushLabel[0],
304
                ($y1 + $this->offsetY) * $this->scale + $this->pushLabel[1],
305
                ($x2 + $this->offsetX) * $this->scale + $this->pushLabel[0],
306
                ($y2 + $this->offsetY) * $this->scale + $this->pushLabel[1]);
307
        }
308
    }
309
310
    /**
311
     * Draws 1 pixel on the resource at a specific position with a determined color.
312
     *
313
     * @param resource $im
314
     * @param int $x
315
     * @param int $y
316
     * @param int $color
317
     */
318
    protected function drawPixel($im, $x, $y, $color = self::COLOR_FG) {
319
        $xR = ($x + $this->offsetX) * $this->scale + $this->pushLabel[0];
320
        $yR = ($y + $this->offsetY) * $this->scale + $this->pushLabel[1];
321
322
        // We always draw a rectangle
323
        imagefilledrectangle($im,
324
            $xR,
325
            $yR,
326
            $xR + $this->scale - 1,
327
            $yR + $this->scale - 1,
328
            $this->getColor($im, $color));
0 ignored issues
show
Bug introduced by
$this->getColor($im, $color) of type resource is incompatible with the type integer expected by parameter $color of imagefilledrectangle(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

328
            /** @scrutinizer ignore-type */ $this->getColor($im, $color));
Loading history...
329
    }
330
331
    /**
332
     * Draws an empty rectangle on the resource at a specific position with a determined color.
333
     *
334
     * @param resource $im
335
     * @param int $x1
336
     * @param int $y1
337
     * @param int $x2
338
     * @param int $y2
339
     * @param int $color
340
     */
341
    protected function drawRectangle($im, $x1, $y1, $x2, $y2, $color = self::COLOR_FG) {
342
        if ($this->scale === 1) {
343
            imagefilledrectangle($im,
344
                ($x1 + $this->offsetX) + $this->pushLabel[0],
345
                ($y1 + $this->offsetY) + $this->pushLabel[1],
346
                ($x2 + $this->offsetX) + $this->pushLabel[0],
347
                ($y2 + $this->offsetY) + $this->pushLabel[1],
348
                $this->getColor($im, $color));
0 ignored issues
show
Bug introduced by
$this->getColor($im, $color) of type resource is incompatible with the type integer expected by parameter $color of imagefilledrectangle(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

348
                /** @scrutinizer ignore-type */ $this->getColor($im, $color));
Loading history...
349
        } else {
350
            imagefilledrectangle($im, ($x1 + $this->offsetX) * $this->scale + $this->pushLabel[0], ($y1 + $this->offsetY) * $this->scale + $this->pushLabel[1], ($x2 + $this->offsetX) * $this->scale + $this->pushLabel[0] + $this->scale - 1, ($y1 + $this->offsetY) * $this->scale + $this->pushLabel[1] + $this->scale - 1, $this->getColor($im, $color));
351
            imagefilledrectangle($im, ($x1 + $this->offsetX) * $this->scale + $this->pushLabel[0], ($y1 + $this->offsetY) * $this->scale + $this->pushLabel[1], ($x1 + $this->offsetX) * $this->scale + $this->pushLabel[0] + $this->scale - 1, ($y2 + $this->offsetY) * $this->scale + $this->pushLabel[1] + $this->scale - 1, $this->getColor($im, $color));
352
            imagefilledrectangle($im, ($x2 + $this->offsetX) * $this->scale + $this->pushLabel[0], ($y1 + $this->offsetY) * $this->scale + $this->pushLabel[1], ($x2 + $this->offsetX) * $this->scale + $this->pushLabel[0] + $this->scale - 1, ($y2 + $this->offsetY) * $this->scale + $this->pushLabel[1] + $this->scale - 1, $this->getColor($im, $color));
353
            imagefilledrectangle($im, ($x1 + $this->offsetX) * $this->scale + $this->pushLabel[0], ($y2 + $this->offsetY) * $this->scale + $this->pushLabel[1], ($x2 + $this->offsetX) * $this->scale + $this->pushLabel[0] + $this->scale - 1, ($y2 + $this->offsetY) * $this->scale + $this->pushLabel[1] + $this->scale - 1, $this->getColor($im, $color));
354
        }
355
    }
356
357
    /**
358
     * Draws a filled rectangle on the resource at a specific position with a determined color.
359
     *
360
     * @param resource $im
361
     * @param int $x1
362
     * @param int $y1
363
     * @param int $x2
364
     * @param int $y2
365
     * @param int $color
366
     */
367
    protected function drawFilledRectangle($im, $x1, $y1, $x2, $y2, $color = self::COLOR_FG) {
368
        if ($x1 > $x2) { // Swap
369
            $x1 ^= $x2 ^= $x1 ^= $x2;
370
        }
371
372
        if ($y1 > $y2) { // Swap
373
            $y1 ^= $y2 ^= $y1 ^= $y2;
374
        }
375
376
        imagefilledrectangle($im,
377
            ($x1 + $this->offsetX) * $this->scale + $this->pushLabel[0],
378
            ($y1 + $this->offsetY) * $this->scale + $this->pushLabel[1],
379
            ($x2 + $this->offsetX) * $this->scale + $this->pushLabel[0] + $this->scale - 1,
380
            ($y2 + $this->offsetY) * $this->scale + $this->pushLabel[1] + $this->scale - 1,
381
            $this->getColor($im, $color));
0 ignored issues
show
Bug introduced by
$this->getColor($im, $color) of type resource is incompatible with the type integer expected by parameter $color of imagefilledrectangle(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

381
            /** @scrutinizer ignore-type */ $this->getColor($im, $color));
Loading history...
382
    }
383
384
    /**
385
     * Allocates the color based on the integer.
386
     *
387
     * @param resource $im
388
     * @param int $color
389
     * @return resource
390
     */
391
    protected function getColor($im, $color) {
392
        if ($color === self::COLOR_BG) {
393
            return $this->colorBg->allocate($im);
394
        } else {
395
            return $this->colorFg->allocate($im);
396
        }
397
    }
398
399
    /**
400
     * Returning the biggest label widths for LEFT/RIGHT and heights for TOP/BOTTOM.
401
     *
402
     * @param bool $reversed
403
     * @return BCGLabel[]
404
     */
405
    private function getBiggestLabels($reversed = false) {
406
        $searchLR = $reversed ? 1 : 0;
407
        $searchTB = $reversed ? 0 : 1;
408
409
        $labels = array();
410
        foreach ($this->labels as $label) {
411
            $position = $label->getPosition();
412
            if (isset($labels[$position])) {
413
                $savedDimension = $labels[$position]->getDimension();
414
                $dimension = $label->getDimension();
415
                if ($position === BCGLabel::POSITION_LEFT || $position === BCGLabel::POSITION_RIGHT) {
416
                    if ($dimension[$searchLR] > $savedDimension[$searchLR]) {
417
                        $labels[$position] = $label;
418
                    }
419
                } else {
420
                    if ($dimension[$searchTB] > $savedDimension[$searchTB]) {
421
                        $labels[$position] = $label;
422
                    }
423
                }
424
            } else {
425
                $labels[$position] = $label;
426
            }
427
        }
428
429
        return $labels;
430
    }
431
}
432
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...