-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
ImageStream.php
255 lines (239 loc) · 4.74 KB
/
ImageStream.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
declare(strict_types=1);
/**
* ImageStream class.
*
* @package YetiForcePDF\Objects\Basic
*
* @copyright YetiForce Sp. z o.o
* @license MIT
* @author Rafal Pospiech <r.pospiech@yetiforce.com>
*/
namespace YetiForcePDF\Objects;
use YetiForcePDF\Math;
/**
* Class ImageStream.
*/
class ImageStream extends \YetiForcePDF\Objects\Resource
{
/**
* Object name.
*
* @var string
*/
protected $name = 'ImageStream';
/**
* Text x position at current page.
*
* @var int
*/
protected $x = 0;
/**
* Text y position at current page.
*
* @var int
*/
protected $y = 0;
/**
* @var string Image data
*/
protected $imageData = '';
/**
* @var string image original width
*/
protected $width = '0';
/**
* @var string image original height
*/
protected $height = '0';
/**
* Width / Height ratio.
*
* @var string
*/
protected $ratio = '0';
/**
* @var int bits per component
*/
protected $bitsPerComponent = 8;
/**
* Convert images to jpeg.
*
* @param string $imageData
*
* @return string
*/
protected function convertToJpg(string $imageData)
{
if ('data:image' === substr($imageData, 0, 10)) {
$matches = [];
preg_match('/data:image\/[a-z]+;base64,(.*)/', $imageData, $matches);
$imageData = base64_decode($matches[1]);
}
$image = @imagecreatefromstring($imageData);
$bg = imagecreatetruecolor(imagesx($image), imagesy($image));
imagefill($bg, 0, 0, imagecolorallocate($bg, 255, 255, 255));
imagealphablending($bg, true);
imagecopy($bg, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));
ob_start();
imagejpeg($bg, null, 90);
$imageString = ob_get_contents();
ob_end_clean();
imagedestroy($image);
imagedestroy($bg);
return $imageString;
}
/**
* Load image data.
*
* @param string $fileName
*
* @return $this
*/
public function loadImage(string $fileName)
{
if ('data:image' === substr($fileName, 0, 10)) {
$this->imageData = $this->convertToJpg($fileName);
} elseif (filter_var($fileName, FILTER_VALIDATE_URL)) {
try {
$client = new \GuzzleHttp\Client();
$res = $client->request('GET', $fileName, ['verify' => \Composer\CaBundle\CaBundle::getSystemCaRootBundlePath()]);
if (200 === $res->getStatusCode()) {
$res->getHeader('content-type');
$this->imageData = $this->convertToJpg((string) $res->getBody());
}
} catch (\Exception $e) {
}
} else {
if (file_exists($fileName)) {
$this->imageData = $this->convertToJpg(file_get_contents($fileName));
}
}
if ($this->imageData) {
$info = getimagesizefromstring($this->imageData);
$this->bitsPerComponent = (string) $info['bits'];
$this->width = (string) $info[0];
$this->height = (string) $info[1];
$this->displayWidth = $this->width;
$this->displayHeight = $this->height;
Math::setAccurate(true);
$this->ratio = Math::div($this->getWidth(), $this->getHeight());
Math::setAccurate(false);
}
return $this;
}
/**
* Get image name.
*
* @return string
*/
public function getImageName()
{
return 'Im' . $this->id;
}
/**
* Get image width.
*
* @return string
*/
public function getWidth()
{
return $this->width;
}
/**
* Get image height.
*
* @return string
*/
public function getHeight()
{
return $this->height;
}
/**
* {@inheritdoc}
*/
public function render(): string
{
$stream = $this->imageData;
return implode("\n", [
$this->getRawId() . ' obj',
'<<',
' /Type /XObject',
' /Subtype /Image',
' /ColorSpace /DeviceRGB',
' /Width ' . $this->width,
' /Height ' . $this->height,
' /BitsPerComponent ' . $this->bitsPerComponent,
' /Length ' . \strlen($stream),
' /Filter /DCTDecode',
'>>',
'stream',
$stream,
'endstream',
'endobj',
]);
}
/**
* Get width / Height ratio.
*
* @return string
*/
public function getRatio()
{
return $this->ratio;
}
/**
* Set width / Height ratio.
*
* @param string $ratio Width / Height ratio.
*
* @return self
*/
public function setRatio(string $ratio)
{
$this->ratio = $ratio;
return $this;
}
/**
* Get display width.
*
* @return string
*/
public function getDisplayWidth()
{
return $this->displayWidth;
}
/**
* Set display width.
*
* @param string $displayWidth Display width
*
* @return self
*/
public function setDisplayWidth(string $displayWidth)
{
$this->displayWidth = $displayWidth;
return $this;
}
/**
* Get display height.
*
* @return string
*/
public function getDisplayHeight()
{
return $this->displayHeight;
}
/**
* Set display height.
*
* @param string $displayHeight Display height
*
* @return self
*/
public function setDisplayHeight(string $displayHeight)
{
$this->displayHeight = $displayHeight;
return $this;
}
}