Passed
Push — master ( 5aa4b6...5758c7 )
by Francimar
02:45
created

Image   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 141
c 0
b 0
f 0
wmc 21
lcom 1
cbo 2
ccs 75
cts 75
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 3
A getLines() 0 4 1
A getWidth() 0 4 1
A getData() 0 4 1
A bytesPerRow() 0 4 1
A loadImage() 0 24 5
A loadImageData() 0 11 2
B readImage() 0 40 6
A getLineData() 0 4 1
1
<?php
2
3
namespace Thermal\Graphics;
4
5
use Thermal\Printer;
6
use Thermal\Buffer\Encoding;
7
use Thermal\Connection\Connection;
8
use Thermal\Graphics\Filter\FloydSteinberg;
9
10
class Image
11
{
12
    private $data;
13
    private $lines;
14
    private $width;
15
    private $bytes_per_row;
16
17 6
    public function __construct($filename, $filter = null)
18
    {
19 6
        $filter = $filter ?: new FloydSteinberg();
20 6
        if (is_array($filename)) {
21 2
            $this->loadImageData($filename, $filter);
22
        } else {
23 4
            $this->loadImage($filename, $filter);
24
        }
25 2
    }
26
27 1
    public function getLines()
28
    {
29 1
        return $this->lines;
30
    }
31
32 1
    public function getWidth()
33
    {
34 1
        return $this->width;
35
    }
36
37 2
    public function getData()
38
    {
39 2
        return $this->data;
40
    }
41
42 1
    public function bytesPerRow()
43
    {
44 1
        return $this->bytes_per_row;
45
    }
46
47
    /**
48
     * Load an image from disk, into memory, using GD.
49
     *
50
     * @param string $filename The filename to load from
51
     * @param Filter $filter filter process
52
     * @throws Exception if the image format is not supported,
53
     *  or the file cannot be opened.
54
     */
55 4
    protected function loadImage($filename, $filter)
56
    {
57 4
        $ext = pathinfo($filename, PATHINFO_EXTENSION);
58
        switch ($ext) {
59 4
            case 'png':
60 1
                $image = @imagecreatefrompng($filename);
61 1
                break;
62 3
            case 'jpg':
63 1
                $image = @imagecreatefromjpeg($filename);
64 1
                break;
65 2
            case 'gif':
66 1
                $image = @imagecreatefromgif($filename);
67 1
                break;
68
            default:
69 1
                throw new \Exception(sprintf('Image format "%s" not supported in GD', $ext));
70
        }
71 3
        if (!is_resource($image)) {
72 2
            throw new \Exception(sprintf('Failed to load image "%s"', $filename));
73
        }
74 1
        $processed_image = $filter->process($image);
0 ignored issues
show
Documentation introduced by
$image is of type resource, but the function expects a object<Thermal\Graphics\ImageResource>.

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...
75 1
        imagedestroy($image);
76 1
        $this->readImage($processed_image);
77 1
        imagedestroy($processed_image);
78 1
    }
79
80
    /**
81
     * Load an image from disk, into memory, using GD.
82
     *
83
     * @param string $filename The filename to load from
0 ignored issues
show
Bug introduced by
There is no parameter named $filename. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
84
     * @param Filter $filter filter process
85
     * @throws Exception if the image format is not supported,
86
     *  or the file cannot be opened.
87
     */
88 2
    protected function loadImageData($data, $filter)
89
    {
90 2
        $image = @imagecreatefromstring($data['data']);
91 2
        if (!is_resource($image)) {
92 1
            throw new \Exception(sprintf('Failed to load image "%s"', $data['name']));
93
        }
94 1
        $processed_image = $filter->process($image);
0 ignored issues
show
Documentation introduced by
$image is of type resource, but the function expects a object<Thermal\Graphics\ImageResource>.

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...
95 1
        imagedestroy($image);
96 1
        $this->readImage($processed_image);
97 1
        imagedestroy($processed_image);
98 1
    }
99
100
    /**
101
     * Load actual image pixels from GD resource.
102
     *
103
     * @param resource $image GD resource to use
104
     */
105 2
    private function readImage($image)
106
    {
107 2
        $width = imagesx($image);
108 2
        $img_height = imagesy($image);
109 2
        $bits = 24;
110 2
        $slices = (int)($bits / 8);
111 2
        $height = (int)(($img_height + $bits - 1) / $bits) * $bits;
112 2
        $this->width = $width;
113 2
        $this->bytes_per_row = $slices * $width;
114 2
        $this->lines = (int)($height / $bits);
115 2
        $pos = 0;
116 2
        $data = str_repeat("\x00", $width * $height / 8);
117 2
        for ($by = 0; $by < $img_height; $by += $bits) {
118 2
            for ($x = 0; $x < $width; $x++) {
119
                // loop slices
120 2
                for ($s = 0; $s < $slices; $s++) {
121 2
                    $slice = 0b00000000;
122 2
                    for ($bit = 0; $bit < 8; $bit++) {
123 2
                        $y = $by + $s * 8 + $bit;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $y. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
124 2
                        if ($y >= $img_height) {
125 2
                            break;
126
                        }
127 2
                        $color = imagecolorat($image, $x, $y);
128 2
                        $alpha = ($color >> 24) & 0xFF;
129 2
                        $red = ($color >> 16) & 0xFF;
130 2
                        $green = ($color >> 8) & 0xFF;
131 2
                        $blue = $color & 0xFF;
132 2
                        $greyness = (int)($red * 0.3 + $green * 0.59 + $blue * 0.11) >> 7;
133
                        // 1 for black, 0 for white, taking into account transparency
134 2
                        $dot = (1 - $greyness) >> ($alpha >> 6);
135
                        // apply the dot
136 2
                        $slice |= $dot << (7 - $bit);
137
                    }
138 2
                    $data[$pos] = chr($slice);
139 2
                    $pos++;
140
                }
141
            }
142
        }
143 2
        $this->data = $data;
144 2
    }
145
146 1
    public function getLineData($index)
147
    {
148 1
        return substr($this->getData(), $index * $this->bytesPerRow(), $this->bytesPerRow());
149
    }
150
}
151