1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Worksheet; |
4
|
|
|
|
5
|
|
|
use GdImage; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Exception; |
7
|
|
|
|
8
|
|
|
class MemoryDrawing extends BaseDrawing |
9
|
|
|
{ |
10
|
|
|
// Rendering functions |
11
|
|
|
const RENDERING_DEFAULT = 'imagepng'; |
12
|
|
|
const RENDERING_PNG = 'imagepng'; |
13
|
|
|
const RENDERING_GIF = 'imagegif'; |
14
|
|
|
const RENDERING_JPEG = 'imagejpeg'; |
15
|
|
|
|
16
|
|
|
// MIME types |
17
|
|
|
const MIMETYPE_DEFAULT = 'image/png'; |
18
|
|
|
const MIMETYPE_PNG = 'image/png'; |
19
|
|
|
const MIMETYPE_GIF = 'image/gif'; |
20
|
|
|
const MIMETYPE_JPEG = 'image/jpeg'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Image resource. |
24
|
|
|
* |
25
|
|
|
* @var null|GdImage|resource |
26
|
|
|
*/ |
27
|
|
|
private $imageResource; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Rendering function. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $renderingFunction; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Mime type. |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $mimeType; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Unique name. |
45
|
|
|
* |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
private $uniqueName; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Create a new MemoryDrawing. |
52
|
|
|
*/ |
53
|
10 |
|
public function __construct() |
54
|
|
|
{ |
55
|
|
|
// Initialise values |
56
|
10 |
|
$this->renderingFunction = self::RENDERING_DEFAULT; |
57
|
10 |
|
$this->mimeType = self::MIMETYPE_DEFAULT; |
58
|
10 |
|
$this->uniqueName = md5(mt_rand(0, 9999) . time() . mt_rand(0, 9999)); |
59
|
|
|
|
60
|
|
|
// Initialize parent |
61
|
10 |
|
parent::__construct(); |
62
|
10 |
|
} |
63
|
|
|
|
64
|
1 |
|
public function __destruct() |
65
|
|
|
{ |
66
|
1 |
|
if ($this->imageResource) { |
67
|
1 |
|
imagedestroy($this->imageResource); |
68
|
1 |
|
$this->imageResource = null; |
69
|
|
|
} |
70
|
1 |
|
} |
71
|
|
|
|
72
|
1 |
|
public function __clone() |
73
|
|
|
{ |
74
|
1 |
|
parent::__clone(); |
75
|
1 |
|
$this->cloneResource(); |
76
|
1 |
|
} |
77
|
|
|
|
78
|
1 |
|
private function cloneResource(): void |
79
|
|
|
{ |
80
|
1 |
|
if (!$this->imageResource) { |
81
|
|
|
return; |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
$width = imagesx($this->imageResource); |
85
|
1 |
|
$height = imagesy($this->imageResource); |
86
|
|
|
|
87
|
1 |
|
if (imageistruecolor($this->imageResource)) { |
88
|
1 |
|
$clone = imagecreatetruecolor($width, $height); |
89
|
1 |
|
if (!$clone) { |
90
|
|
|
throw new Exception('Could not clone image resource'); |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
imagealphablending($clone, false); |
94
|
1 |
|
imagesavealpha($clone, true); |
95
|
|
|
} else { |
96
|
|
|
$clone = imagecreate($width, $height); |
97
|
|
|
if (!$clone) { |
98
|
|
|
throw new Exception('Could not clone image resource'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// If the image has transparency... |
102
|
|
|
$transparent = imagecolortransparent($this->imageResource); |
103
|
|
|
if ($transparent >= 0) { |
104
|
|
|
$rgb = imagecolorsforindex($this->imageResource, $transparent); |
105
|
|
|
if ($rgb === false) { |
106
|
|
|
throw new Exception('Could not get image colors'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
imagesavealpha($clone, true); |
110
|
|
|
$color = imagecolorallocatealpha($clone, $rgb['red'], $rgb['green'], $rgb['blue'], $rgb['alpha']); |
111
|
|
|
if ($color === false) { |
112
|
|
|
throw new Exception('Could not get image alpha color'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
imagefill($clone, 0, 0, $color); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
//Create the Clone!! |
120
|
1 |
|
imagecopy($clone, $this->imageResource, 0, 0, 0, 0, $width, $height); |
121
|
|
|
|
122
|
1 |
|
$this->imageResource = $clone; |
123
|
1 |
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get image resource. |
127
|
|
|
* |
128
|
|
|
* @return null|GdImage|resource |
129
|
|
|
*/ |
130
|
6 |
|
public function getImageResource() |
131
|
|
|
{ |
132
|
6 |
|
return $this->imageResource; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Set image resource. |
137
|
|
|
* |
138
|
|
|
* @param GdImage|resource $value |
139
|
|
|
* |
140
|
|
|
* @return $this |
141
|
|
|
*/ |
142
|
10 |
|
public function setImageResource($value) |
143
|
|
|
{ |
144
|
10 |
|
$this->imageResource = $value; |
145
|
|
|
|
146
|
10 |
|
if ($this->imageResource !== null) { |
147
|
|
|
// Get width/height |
148
|
10 |
|
$this->width = imagesx($this->imageResource); |
149
|
10 |
|
$this->height = imagesy($this->imageResource); |
150
|
|
|
} |
151
|
|
|
|
152
|
10 |
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Get rendering function. |
157
|
|
|
* |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
6 |
|
public function getRenderingFunction() |
161
|
|
|
{ |
162
|
6 |
|
return $this->renderingFunction; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Set rendering function. |
167
|
|
|
* |
168
|
|
|
* @param string $value see self::RENDERING_* |
169
|
|
|
* |
170
|
|
|
* @return $this |
171
|
|
|
*/ |
172
|
10 |
|
public function setRenderingFunction($value) |
173
|
|
|
{ |
174
|
10 |
|
$this->renderingFunction = $value; |
175
|
|
|
|
176
|
10 |
|
return $this; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Get mime type. |
181
|
|
|
* |
182
|
|
|
* @return string |
183
|
|
|
*/ |
184
|
8 |
|
public function getMimeType() |
185
|
|
|
{ |
186
|
8 |
|
return $this->mimeType; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Set mime type. |
191
|
|
|
* |
192
|
|
|
* @param string $value see self::MIMETYPE_* |
193
|
|
|
* |
194
|
|
|
* @return $this |
195
|
|
|
*/ |
196
|
10 |
|
public function setMimeType($value) |
197
|
|
|
{ |
198
|
10 |
|
$this->mimeType = $value; |
199
|
|
|
|
200
|
10 |
|
return $this; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Get indexed filename (using image index). |
205
|
|
|
* |
206
|
|
|
* @return string |
207
|
|
|
*/ |
208
|
6 |
|
public function getIndexedFilename() |
209
|
|
|
{ |
210
|
6 |
|
$extension = strtolower($this->getMimeType()); |
211
|
6 |
|
$extension = explode('/', $extension); |
212
|
6 |
|
$extension = $extension[1]; |
213
|
|
|
|
214
|
6 |
|
return $this->uniqueName . $this->getImageIndex() . '.' . $extension; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Get hash code. |
219
|
|
|
* |
220
|
|
|
* @return string Hash code |
221
|
|
|
*/ |
222
|
6 |
|
public function getHashCode() |
223
|
|
|
{ |
224
|
6 |
|
return md5( |
225
|
6 |
|
$this->renderingFunction . |
226
|
6 |
|
$this->mimeType . |
227
|
6 |
|
$this->uniqueName . |
228
|
6 |
|
parent::getHashCode() . |
229
|
6 |
|
__CLASS__ |
230
|
|
|
); |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|