|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpPresentation\Shape\Drawing; |
|
4
|
|
|
|
|
5
|
|
|
class Gd extends AbstractDrawingAdapter |
|
6
|
|
|
{ |
|
7
|
|
|
/* Rendering functions */ |
|
8
|
|
|
const RENDERING_DEFAULT = 'imagepng'; |
|
9
|
|
|
const RENDERING_PNG = 'imagepng'; |
|
10
|
|
|
const RENDERING_GIF = 'imagegif'; |
|
11
|
|
|
const RENDERING_JPEG = 'imagejpeg'; |
|
12
|
|
|
|
|
13
|
|
|
/* MIME types */ |
|
14
|
|
|
const MIMETYPE_DEFAULT = 'image/png'; |
|
15
|
|
|
const MIMETYPE_PNG = 'image/png'; |
|
16
|
|
|
const MIMETYPE_GIF = 'image/gif'; |
|
17
|
|
|
const MIMETYPE_JPEG = 'image/jpeg'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Image resource |
|
21
|
|
|
* |
|
22
|
|
|
* @var resource |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $imageResource; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Rendering function |
|
28
|
|
|
* |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $renderingFunction = self::RENDERING_DEFAULT; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Mime type |
|
35
|
|
|
* |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $mimeType = self::MIMETYPE_DEFAULT; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Unique name |
|
42
|
|
|
* |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $uniqueName; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Gd constructor |
|
49
|
|
|
*/ |
|
50
|
11 |
|
public function __construct() |
|
51
|
|
|
{ |
|
52
|
11 |
|
parent::__construct(); |
|
53
|
11 |
|
$this->uniqueName = md5(rand(0, 9999) . time() . rand(0, 9999)); |
|
54
|
11 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Get image resource |
|
58
|
|
|
* |
|
59
|
|
|
* @return resource |
|
60
|
|
|
*/ |
|
61
|
2 |
|
public function getImageResource() |
|
62
|
|
|
{ |
|
63
|
2 |
|
return $this->imageResource; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Set image resource |
|
68
|
|
|
* |
|
69
|
|
|
* @param $value resource |
|
70
|
|
|
* @return $this |
|
71
|
|
|
*/ |
|
72
|
11 |
|
public function setImageResource($value = null) |
|
73
|
|
|
{ |
|
74
|
11 |
|
$this->imageResource = $value; |
|
75
|
|
|
|
|
76
|
11 |
|
if (!is_null($this->imageResource)) { |
|
77
|
|
|
// Get width/height |
|
78
|
11 |
|
$this->width = imagesx($this->imageResource); |
|
79
|
11 |
|
$this->height = imagesy($this->imageResource); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
11 |
|
return $this; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Get rendering function |
|
87
|
|
|
* |
|
88
|
|
|
* @return string |
|
89
|
|
|
*/ |
|
90
|
2 |
|
public function getRenderingFunction() |
|
91
|
|
|
{ |
|
92
|
2 |
|
return $this->renderingFunction; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Set rendering function |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $value |
|
99
|
|
|
* @return $this |
|
100
|
|
|
*/ |
|
101
|
1 |
|
public function setRenderingFunction($value = self::RENDERING_DEFAULT) |
|
102
|
|
|
{ |
|
103
|
1 |
|
$this->renderingFunction = $value; |
|
104
|
1 |
|
return $this; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Get mime type |
|
109
|
|
|
* |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
2 |
|
public function getMimeType() |
|
113
|
|
|
{ |
|
114
|
2 |
|
return $this->mimeType; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Set mime type |
|
119
|
|
|
* |
|
120
|
|
|
* @param string $value |
|
121
|
|
|
* @return $this |
|
122
|
|
|
*/ |
|
123
|
1 |
|
public function setMimeType($value = self::MIMETYPE_DEFAULT) |
|
124
|
|
|
{ |
|
125
|
1 |
|
$this->mimeType = $value; |
|
126
|
1 |
|
return $this; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return string |
|
131
|
|
|
*/ |
|
132
|
2 |
|
public function getContents() |
|
133
|
|
|
{ |
|
134
|
2 |
|
ob_start(); |
|
135
|
2 |
|
call_user_func($this->getRenderingFunction(), $this->getImageResource()); |
|
136
|
2 |
|
$imageContents = ob_get_contents(); |
|
137
|
2 |
|
ob_end_clean(); |
|
138
|
2 |
|
return $imageContents; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @return string |
|
143
|
|
|
*/ |
|
144
|
2 |
|
public function getExtension() |
|
145
|
|
|
{ |
|
146
|
2 |
|
$extension = strtolower($this->getMimeType()); |
|
147
|
2 |
|
$extension = explode('/', $extension); |
|
148
|
2 |
|
$extension = $extension[1]; |
|
149
|
2 |
|
return $extension; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @return string |
|
154
|
|
|
*/ |
|
155
|
2 |
|
public function getIndexedFilename() |
|
156
|
|
|
{ |
|
157
|
2 |
|
return $this->uniqueName . $this->getImageIndex() . '.' . $this->getExtension(); |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|