|
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); |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
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; |
|
|
|
|
|
|
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
|
|
|
|
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: