Graphics   F
last analyzed

Complexity

Total Complexity 84

Size/Duplication

Total Lines 578
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 69.39%

Importance

Changes 0
Metric Value
wmc 84
lcom 1
cbo 3
dl 0
loc 578
ccs 220
cts 317
cp 0.6939
rs 2
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 3
A getRasterImage() 0 7 1
B load() 0 31 9
A convertBW() 0 23 1
A save() 0 13 3
A resizeImage() 0 19 6
B imageQRCode() 0 43 5
B loadBMP() 0 59 9
B saveBMP() 0 44 5
A convertPixelBW() 0 18 3
B convertRaster() 0 49 9
B saveImage() 0 35 10
C getBMPColor() 0 55 15
A littleEndian2String() 0 9 2
A getPixelColor() 0 4 1
A closestMultiple() 0 8 2

How to fix   Complexity   

Complex Class

Complex classes like Graphics often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Graphics, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Posprint\Graphics;
4
5
/**
6
 * Classe Graphics
7
 *
8
 * @category  NFePHP
9
 * @package   Posprint
10
 * @copyright Copyright (c) 2016
11
 * @license   http://www.gnu.org/licenses/lesser.html LGPL v3
12
 * @author    Roberto L. Machado <linux dot rlm at gmail dot com>
13
 * @link      http://github.com/nfephp-org/posprint for the canonical source repository
14
 */
15
16
use Posprint\Graphics\Basic;
17
use Endroid\QrCode\ErrorCorrectionLevel;
18
use Endroid\QrCode\Writer\PngWriter;
19
use Com\Tecnick\Barcode\Barcode;
20
use RuntimeException;
21
use InvalidArgumentException;
22
23
class Graphics extends Basic
24
{
25
    /**
26
     * Image prixels in BW
27
     * @var string|null
28
     */
29
    protected $imgData = null;
30
    /**
31
     * Image Raster bit
32
     * @var string|null
33
     */
34
    protected $imgRasterData = null;
35
    
36
    /**
37
     * Constructor
38
     * Load a image, if passed a path to file and adjust dimentions
39
     * @param  string $filename
40
     * @param  int   $width
41
     * @param  int    $height
42
     * @throws RuntimeException
43
     */
44
    public function __construct($filename = null, $width = null, $height = null)
45 12
    {
46
        if (! $this->isGdSupported()) {
47 12
            throw new RuntimeException("GD module not found.");
48
        }
49
        $this->imgHeight = 0;
50 12
        $this->imgWidth = 0;
51 12
        $this->imgData = null;
52 12
        $this->imgRasterData = null;
53 12
        // Load the image, if the patch was passed
54
        if (! is_null($filename)) {
55 12
            $this->load($filename, $width, $height);
56 9
        }
57 7
    }
58 10
    
59
    /**
60
     * Return a string of bytes
61
     * for inclusion on printer commands
62
     * This method change image to Black and White and
63
     * reducing the color resolution of 1 bit per pixel
64
     * adptation fom escpos-php from Michael Billington
65
     * @return string|null
66
     */
67
    public function getRasterImage()
68 2
    {
69
        $this->resizeImage($this->imgWidth);
70 2
        $this->convertPixelBW();
71 2
        $this->convertRaster();
72 2
        return $this->imgRasterData;
73 2
    }
74
75
    /**
76
     * load
77
     * Load image file and adjust dimentions
78
     * @param  string $filename path to image file
79
     * @param  int  $width
80
     * @param  int  $height
81
     * @throws InvalidArgumentException
82
     * @throws RuntimeException
83
     * @return void
84
     */
85
    public function load($filename, $width = null, $height = null)
86 9
    {
87
        if (! is_file($filename)) {
88 9
            throw new InvalidArgumentException("Image file not found.");
89 1
        }
90
        if (! is_readable($filename)) {
91 8
            throw new RuntimeException("The file can not be read due to lack of permissions.");
92
        }
93
        //identify type of image and load with GD
94
        $tipo = $this->identifyImg($filename);
95 8
        if ($tipo == 'BMP') {
96 8
            $img = $this->loadBMP($filename);
97
            if ($img === false) {
98
                throw new InvalidArgumentException("Image file is not a BMP");
99
            }
100
        } else {
101
            $func = 'imagecreatefrom' . strtolower($tipo);
102 8
            if (! function_exists($func)) {
103 8
                throw new RuntimeException("It is not possible to use or handle this type of image with GD");
104 1
            }
105
            $this->img = $func($filename);
106 7
        }
107
        if (! $this->img) {
108 7
            throw new RuntimeException("Failed to load image '$filename'.");
109
        }
110
        //get image dimentions
111
        $this->getDimImage();
112 7
        if ($width != null || $height != null) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $width of type integer|null against null; this is ambiguous if the integer can be zero. Consider using a strict comparison !== instead.
Loading history...
Bug Best Practice introduced by
It seems like you are loosely comparing $height of type integer|null against null; this is ambiguous if the integer can be zero. Consider using a strict comparison !== instead.
Loading history...
113 7
            $this->resizeImage($width, $height);
114
        }
115
    }
116 7
    
117
    /**
118
     * Converts a true color image to Black and white
119
     * even if the image have transparency (alpha channel)
120
     * adptation from escpos-php by Michael Billington
121
     * @return void
122
     */
123
    public function convertBW()
124
    {
125
        $newimg = imagecreatetruecolor($this->imgWidth, $this->imgHeight);
126
        imagealphablending($newimg, false);
127
        imagesavealpha($newimg, true);
128
        imagecopyresampled(
129
            $newimg,
130
            $this->img,
131
            0,
132
            0,
133
            0,
134
            0,
135
            $this->imgWidth,
136
            $this->imgHeight,
137
            $this->imgWidth,
138
            $this->imgHeight
139
        );
140
        $bcg = imagecolorallocate($newimg, 255, 255, 255);
141
        imagefill($newimg, 0, 0, $bcg);
142
        imagefilter($newimg, IMG_FILTER_GRAYSCALE);
143
        imagefilter($newimg, IMG_FILTER_CONTRAST, -1000);
144
        $this->img = $newimg;
145
    }
146
    
147
    /**
148
     * Save image to file
149
     * @param string $filename
150
     * @param string $type  PNG, JPG, GIF, BMP
151
     * @param integer $quality 0 - 100 default 75
152
     * @return boolean
153
     */
154 4
    public function save($filename = null, $type = 'PNG', $quality = 75)
155
    {
156 4
        $type = strtoupper($type);
157 4
        if ($type == 'BMP') {
158 2
            $this->saveBMP($filename);
159 1
            return true;
160
        }
161 2
        $aTypes = ['PNG', 'JPG', 'JPEG',  'GIF'];
162 2
        if (! in_array($type, $aTypes)) {
163
            throw new InvalidArgumentException('This file type is not supported.');
164
        }
165 2
        return $this->saveImage($filename, $this->img, $type, $quality);
166
    }
167
    
168
    /**
169
     * resizeImage
170
     * Resize an image
171
     * NOTE: the width is always set to the multiple of 8 more
172
     * next, why? printers have a resolution of 8 dots per mm
173
     * @param  int $width
174
     * @param  int $height
175
     * @throws InvalidArgumentException
176
     */
177
    public function resizeImage($width = null, $height = null)
178 3
    {
179
        if ($width == null && $height == null) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $width of type integer|null against null; this is ambiguous if the integer can be zero. Consider using a strict comparison === instead.
Loading history...
Bug Best Practice introduced by
It seems like you are loosely comparing $height of type integer|null against null; this is ambiguous if the integer can be zero. Consider using a strict comparison === instead.
Loading history...
180 3
            throw new InvalidArgumentException("No dimensions was passed.");
181 1
        }
182
        if ($width != null) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $width of type integer|null against null; this is ambiguous if the integer can be zero. Consider using a strict comparison !== instead.
Loading history...
183 2
            $width = $this->closestMultiple($width);
184 1
            $razao = $width / $this->imgWidth;
185 1
            $height = (int) round($razao * $this->imgHeight);
186 1
        } elseif ($width == null && $height != null) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $width of type integer|null against null; this is ambiguous if the integer can be zero. Consider using a strict comparison === instead.
Loading history...
Bug Best Practice introduced by
It seems like you are loosely comparing $height of type integer|null against null; this is ambiguous if the integer can be zero. Consider using a strict comparison !== instead.
Loading history...
187 2
            $razao = $height / $this->imgHeight;
188 1
            $width = (int) round($razao * $this->imgWidth);
189 1
            $width = $this->closestMultiple($width);
190 1
        }
191 1
        $tempimg = imagecreatetruecolor($width, $height);
192 2
        imagecopyresampled($tempimg, $this->img, 0, 0, 0, 0, $width, $height, $this->imgWidth, $this->imgHeight);
193 2
        $this->img = $tempimg;
194 2
        $this->getDimImage();
195 2
    }
196 2
    
197
    /**
198
     * Creates a  GD QRCode image
199
     * @param string $dataText
200
     * @param int $width
201
     * @param int $padding
202
     * @param string $errCorretion LOW, MEDIUM, QUARTILE, HIGH
203
     * @return void
204
     */
205
    public function imageQRCode(
206
        $dataText = 'NADA NADA NADA NADA NADA NADA NADA NADA NADA NADA NADA NADA',
207 1
        $width = 200,
208
        $padding = 10,
209
        $errCorretion = 'medium'
210
    ) {
211
        switch (strtolower($errCorretion)) {
212
            case self::LOW:
213
                $ec = 'L';
214 1
                break;
215 1
            case self::HIGH:
216 1
                $ec = 'H';
217 1
                break;
218 1
            case self::QUARTILE:
219 1
                $ec = 'Q';
220 1
                break;
221 1
            default:
222 1
                $ec = 'M';
223 1
        }
224 1
        //adjust width for a closest multiple of 8
225 1
        $width = $this->closestMultiple($width, 8);
226 1
        //create image
227 1
        try {
228
            $barcode = new Barcode();
229
            $bobj = $barcode->getBarcodeObj(
230
                "QRCODE,$ec",
231
                $dataText,
232
                $width,
233
                $width,
234
                'black',
235
                array($padding, $padding, $padding, $padding)
236 1
            )->setBackgroundColor('white');
237
            $this->img = imagecreatefromstring(
238
                $bobj->getPngData()
239 1
            );
240
            $this->getDimImage();
241
        } catch (\Exception $e) {
242
            throw new \RuntimeException(
243 1
                "ERROR. Falha de validação no ajuste do tamanho do codigo "
244 1
                . "para permitir legibilidade. [" . $e->getMessage() . ']'
245
            );
246
        }
247
    }
248 1
    
249 1
    /**
250 1
     * loadBMP
251
     * Create a GD image from BMP file
252 1
     * @param  string $filename
253 1
     * @return boolean
254 1
     */
255 1
    protected function loadBMP($filename)
256 1
    {
257 1
        //open file as binary
258 1
        if (! $f1 = fopen($filename, "rb")) {
259 1
            throw new InvalidArgumentException('Can not open file.');
260 1
        }
261 1
        //get properties from image file
262
        $file = unpack("vfile_type/Vfile_size/Vreserved/Vbitmap_offset", fread($f1, 14));
263
        if ($file['file_type'] != 19778) {
264 1
            throw new InvalidArgumentException('This file is not a BMP image.');
265 1
        }
266
        //get properties form image
267
        $bmp = unpack('Vheader_size/Vwidth/Vheight/vplanes/vbits_per_pixel'.
268
           '/Vcompression/Vsize_bitmap/Vhoriz_resolution'.
269 1
           '/Vvert_resolution/Vcolors_used/Vcolors_important', fread($f1, 40));
270 1
        //check deep of colors
271
        $bmp['colors'] = pow(2, $bmp['bits_per_pixel']);
272 1
        if ($bmp['size_bitmap'] == 0) {
273 1
            $bmp['size_bitmap'] = $file['file_size'] - $file['bitmap_offset'];
274 1
        }
275 1
        $bmp['bytes_per_pixel'] = $bmp['bits_per_pixel']/8;
276
        $bmp['bytes_per_pixel2'] = ceil($bmp['bytes_per_pixel']);
277 1
        $bmp['decal'] = ($bmp['width']*$bmp['bytes_per_pixel']/4);
278 1
        $bmp['decal'] -= floor($bmp['width']*$bmp['bytes_per_pixel']/4);
279 1
        $bmp['decal'] = 4-(4*$bmp['decal']);
280
        if ($bmp['decal'] == 4) {
281 1
            $bmp['decal'] = 0;
282 1
        }
283
        $palette = array();
284
        if ($bmp['colors'] < 16777216) {
285 1
            $palette = unpack('V'.$bmp['colors'], fread($f1, $bmp['colors']*4));
286 1
        }
287 1
        //read all data form image but not the header
288 1
        $img = fread($f1, $bmp['size_bitmap']);
289 1
        fclose($f1);
290 1
        //create a true color GD resource
291 1
        $vide = chr(0);
292 1
        $res = imagecreatetruecolor($bmp['width'], $bmp['height']);
293 1
        $p = 0;
294
        $y = $bmp['height']-1;
295
        //read all bytes form original file
296
        while ($y >= 0) {
297
            $x=0;
298
            while ($x < $bmp['width']) {
299
                //get byte color from BMP
300
                $color = $this->getBMPColor($bmp['bits_per_pixel'], $img, $vide, $p, $palette);
0 ignored issues
show
Documentation introduced by
$palette is of type array, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
301
                if ($color === false) {
302 2
                    throw new RuntimeException('Fail during conversion from BMP number bit per pixel incorrect!');
303
                }
304 2
                imagesetpixel($res, $x, $y, $color[1]);
305
                $x++;
306
                $p += $bmp['bytes_per_pixel'];
307
            }
308 2
            $y--;
309 2
            $p += $bmp['decal'];
310 2
        }
311 2
        $this->img = $res;
312 2
        return true;
313 2
    }
314 2
315 2
    /**
316 2
     * Convert a GD image into a BMP string representation
317 2
     * @param string $filename
318 2
     * @return string
319 2
     */
320 2
    protected function saveBMP($filename = null)
321 2
    {
322 2
        if (! is_resource($this->img)) {
323 2
            return '';
324
        }
325 2
        //to remove alpha color and put white instead
326 2
        $img = $this->img;
327 2
        $imageX = imagesx($img);
328 2
        $imageY = imagesy($img);
329 2
        $bmp = '';
330
        for ($yInd = ($imageY - 1); $yInd >= 0; $yInd--) {
331 2
            $thisline = '';
332 2
            for ($xInd = 0; $xInd < $imageX; $xInd++) {
333 2
                $argb = self::getPixelColor($img, $xInd, $yInd);
334 2
                $thisline .= chr($argb['blue']).chr($argb['green']).chr($argb['red']);
335 2
            }
336 2
            while (strlen($thisline) % 4) {
337 2
                $thisline .= "\x00";
338 2
            }
339 2
            $bmp .= $thisline;
340 2
        }
341 2
        $bmpSize = strlen($bmp) + 14 + 40;
342 2
        // bitMapHeader [14 bytes] - http://msdn.microsoft.com/library/en-us/gdi/bitmaps_62uq.asp
343 2
        $bitMapHeader  = 'BM'; // WORD bfType;
344 1
        $bitMapHeader .= self::littleEndian2String($bmpSize, 4); // DWORD bfSize;
345
        $bitMapHeader .= self::littleEndian2String(0, 2); // WORD bfReserved1;
346
        $bitMapHeader .= self::littleEndian2String(0, 2); // WORD bfReserved2;
347
        $bitMapHeader .= self::littleEndian2String(54, 4); // DWORD bfOffBits;
348
        // bitMapInfoHeader - [40 bytes] http://msdn.microsoft.com/library/en-us/gdi/bitmaps_1rw2.asp
349
        $bitMapInfoHeader  = self::littleEndian2String(40, 4); // DWORD biSize;
350
        $bitMapInfoHeader .= self::littleEndian2String($imageX, 4); // LONG biWidth;
351
        $bitMapInfoHeader .= self::littleEndian2String($imageY, 4); // LONG biHeight;
352
        $bitMapInfoHeader .= self::littleEndian2String(1, 2); // WORD biPlanes;
353 1
        $bitMapInfoHeader .= self::littleEndian2String(24, 2); // WORD biBitCount;
354
        $bitMapInfoHeader .= self::littleEndian2String(0, 4); // DWORD biCompression;
355
        $bitMapInfoHeader .= self::littleEndian2String(0, 4); // DWORD biSizeImage;
356 1
        $bitMapInfoHeader .= self::littleEndian2String(2835, 4); // LONG biXPelsPerMeter;
357 1
        $bitMapInfoHeader .= self::littleEndian2String(2835, 4); // LONG biYPelsPerMeter;
358 1
        $bitMapInfoHeader .= self::littleEndian2String(0, 4); // DWORD biClrUsed;
359
        $bitMapInfoHeader .= self::littleEndian2String(0, 4); // DWORD biClrImportant;
360 1
        $data = $bitMapHeader.$bitMapInfoHeader.$bmp;
361
        $this->saveImage($filename, $data);
362 1
        return $data;
363
    }
364
    
365 1
    /**
366 1
     * Convert image from GD resource
367 1
     * into Black and White pixels image
368 1
     * @return string Representation of bytes image in BW
369 1
     */
370
    protected function convertPixelBW()
371
    {
372
        // Make a string of 1's and 0's
373
        $this->imgData = str_repeat("\0", $this->imgHeight * $this->imgWidth);
374
        for ($yInd = 0; $yInd < $this->imgHeight; $yInd++) {
375
            for ($xInd = 0; $xInd < $this->imgWidth; $xInd++) {
376
                //get colors from byte image
377
                $cols = imagecolorsforindex($this->img, imagecolorat($this->img, $xInd, $yInd));
378
                //convert to greyness color 1 for white, 0 for black
379
                $greyness = (int)(($cols['red'] + $cols['green'] + $cols['blue']) / 3) >> 7;
380
                //switch to Black and white
381 2
                //1 for black, 0 for white, taking into account transparency color
382
                $black = (1 - $greyness) >> ($cols['alpha'] >> 6);
383 2
                $this->imgData[$yInd * $this->imgWidth + $xInd] = $black;
384 1
            }
385
        }
386 2
        return $this->imgData;
387
    }
388
389
    /**
390 2
     * Output the image in raster (row) format.
391
     * This can result in padding on the right of the image,
392 2
     * if its width is not divisible by 8.
393
     * @throws RuntimeException Where the generated data is
394 2
     *         unsuitable for the printer (indicates a bug or oversized image).
395
     * @return string The image in raster format.
396 2
     */
397
    protected function convertRaster()
398 2
    {
399 2
        if (! is_null($this->imgRasterData)) {
400
             return $this->imgRasterData;
401
        }
402
        if (is_null($this->imgData)) {
403
            $this->convertPixelBW();
404 2
        }
405 2
        //get width in Pixels
406 2
        $widthPixels = $this->getWidth();
407 2
        //get heightin in Pixels
408 2
        $heightPixels = $this->getHeight();
409 2
        //get width in Bytes
410 2
        $widthBytes = $this->getWidthBytes();
411 2
        //initialize vars
412 2
        $xCount = $yCount = $bit = $byte = $byteVal = 0;
413 2
        //create a string for converted bytes
414
        $data = str_repeat("\0", $widthBytes * $heightPixels);
415 2
        if (strlen($data) == 0) {
416 2
            return $data;
417 2
        }
418 2
        /* Loop through and convert format */
419 2
        do {
420 2
            $num = $yCount * $widthPixels + $xCount;
421 2
            $byteVal |= (int) $this->imgData[$num] << (7 - $bit);
422 2
            $xCount++;
423 2
            $bit++;
424
            if ($xCount >= $widthPixels) {
425
                $xCount = 0;
426 2
                $yCount++;
427 2
                $bit = 8;
428
                if ($yCount >= $heightPixels) {
429
                    $data[$byte] = chr($byteVal);
430
                    break;
431
                }
432
            }
433
            if ($bit >= 8) {
434
                $data[$byte] = chr($byteVal);
435
                $byteVal = 0;
436
                $bit = 0;
437
                $byte++;
438
            }
439
        } while (true);
440
        if (strlen($data) != ($this->getWidthBytes() * $this->getHeight())) {
441 4
            throw new RuntimeException("Bug in " . __FUNCTION__ . ", wrong number of bytes.");
442
        }
443 4
         $this->imgRasterData = $data;
444
         return $this->imgRasterData;
445
    }
446 4
    
447
    /**
448
     * Save safety binary image file
449 2
     * @param string $filename
450 2
     * @param resource|string|null $data
451
     * @param string $type PNG, JPG, GIF, BMP
452
     * @param integer $quality
453 2
     * @return boolean
454
     * @throws InvalidArgumentException
455
     * @throws RuntimeException
456 2
     */
457 2
    protected function saveImage($filename = null, $data = null, $type = 'PNG', $quality = 75)
458 2
    {
459 2
        if (empty($filename) || empty($data)) {
460 2
            return false;
461
        }
462
        if (is_resource($data)) {
463 2
            //use GD to save image to file
464
            switch ($type) {
465 2
                case 'JPG':
466 2
                case 'JPEG':
467 1
                    $result = imagejpeg($data, $filename, $quality);
468
                    break;
469 1
                case 'GIF':
470 1
                    $result = imagegif($data, $filename);
471 1
                    break;
472
                default:
473
                    $result = imagepng($data, $filename);
474 1
                    break;
475
            }
476
            if (!$result) {
477
                throw new InvalidArgumentException("Fail to write in $filename.");
478
            }
479
            return true;
480
        }
481
        $handle = @fopen($filename, 'w');
482
        if (!is_resource($handle)) {
483
            throw new InvalidArgumentException("Cant open file $filename. Check permissions.");
484
        }
485
        $nbytes = fwrite($handle, $data);
486
        fclose($handle);
487
        if (!$nbytes) {
488
            throw new RuntimeException("Fail to write in $filename.");
489
        }
490
        return true;
491
    }
492
493
    /**
494
     * Get byte color form BMP
495
     * @param integer $bpp bytes_per_pixel
496
     * @param string $img bytes read of file
497
     * @param string $vide
498
     * @param integer $p
499
     * @param integer $palette
500
     * @return integer|boolean
501
     */
502
    private function getBMPColor($bpp, $img, $vide, $p, $palette)
503
    {
504
        switch ($bpp) {
505
            case 24:
506
                return unpack("V", substr($img, $p, 3).$vide);
507
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
508
            case 16:
509
                $color = unpack("v", substr($img, $p, 2));
510
                $blue = ($color[1] & 0x001f) << 3;
511
                $green = ($color[1] & 0x07e0) >> 3;
512
                $red = ($color[1] & 0xf800) >> 8;
513
                $color[1] = $red * 65536 + $green * 256 + $blue;
514
                return $color;
515
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
516
            case 8:
517
                $color = unpack("n", $vide.substr($img, $p, 1));
518
                $color[1] = $palette[$color[1]+1];
519
                return $color;
520
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
521
            case 4:
522
                $color = unpack("n", $vide.substr($img, floor($p), 1));
523
                if (($p*2)%2 == 0) {
524
                    $color[1] = ($color[1] >> 4) ;
525
                } else {
526
                    $color[1] = ($color[1] & 0x0F);
527
                }
528
                $color[1] = $palette[$color[1]+1];
529
                return $color;
530
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
531
            case 1:
532
                $color = unpack("n", $vide.substr($img, floor($p), 1));
533
                if (($p*8)%8 == 0) {
534
                    $color[1] = $color[1]>>7;
535
                } elseif (($p*8)%8 == 1) {
536
                    $color[1] = ($color[1] & 0x40)>>6;
537
                } elseif (($p*8)%8 == 2) {
538
                    $color[1] = ($color[1] & 0x20)>>5;
539
                } elseif (($p*8)%8 == 3) {
540
                    $color[1] = ($color[1] & 0x10)>>4;
541
                } elseif (($p*8)%8 == 4) {
542
                    $color[1] = ($color[1] & 0x8)>>3;
543
                } elseif (($p*8)%8 == 5) {
544
                    $color[1] = ($color[1] & 0x4)>>2;
545
                } elseif (($p*8)%8 == 6) {
546
                    $color[1] = ($color[1] & 0x2)>>1;
547
                } elseif (($p*8)%8 == 7) {
548
                    $color[1] = ($color[1] & 0x1);
549
                }
550 2
                $color[1] = $palette[$color[1]+1];
551
                return $color;
552 2
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
553 2
            default:
554 2
                return false;
555 2
        }
556 2
    }
557 2
    
558
    /**
559
     * Converts Litte Endian Bytes to String
560
     * @param int $number
561
     * @param int $minbytes
562
     * @return string
563
     */
564
    private static function littleEndian2String($number, $minbytes = 1)
565
    {
566
        $intstring = '';
567
        while ($number > 0) {
568 2
            $intstring = $intstring.chr($number & 255);
569
            $number >>= 8;
570 2
        }
571
        return str_pad($intstring, $minbytes, "\x00", STR_PAD_RIGHT);
572
    }
573
    
574
    /**
575
     * Get pixel colors
576
     * @param  resource $img
577
     * @param  int $x
578
     * @param  int $y
579
     * @return array
580 3
     */
581
    private static function getPixelColor($img, $x, $y)
582 3
    {
583 3
        return imagecolorsforindex($img, imagecolorat($img, $x, $y));
584 1
    }
585
    
586 2
    /**
587
     * Find closest multiple
588
     * @param  float $num
589
     * @param  int $num
590
     * @return int
591
     */
592
    private function closestMultiple($num = 0, $base = 8)
593
    {
594
        $iNum = ceil($num);
595
        if (($iNum % $base) === 0) {
596
            return $iNum;
597
        }
598
        return round($num/$base) * $base;
599
    }
600
}
601