forked from typpo/quickchart-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickChart.php
More file actions
140 lines (117 loc) · 4.04 KB
/
QuickChart.php
File metadata and controls
140 lines (117 loc) · 4.04 KB
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
<?php
class QuickChart {
public $protocol;
public $host;
public $port;
public $config;
public $width;
public $height;
public $devicePixelRatio;
public $format;
public $backgroundColor;
public $apiKey;
function __construct($options = array()) {
$this->protocol = isset($options['protocol']) ? $options['protocol'] : 'https';
$this->host = isset($options['host']) ? $options['host'] : 'quickchart.io';
$this->port = isset($options['port']) ? $options['port'] : 443;
$this->width = isset($options['width']) ? $options['width'] : 500;
$this->height = isset($options['height']) ? $options['height'] : 300;
$this->devicePixelRatio = isset($options['devicePixelRatio']) ? $options['devicePixelRatio'] : 1.0;
$this->format = isset($options['format']) ? $options['format'] : 'png';
$this->backgroundColor = isset($options['backgroundColor']) ? $options['backgroundColor'] : 'transparent';
$this->apiKey = isset($options['apiKey']) ? $options['apiKey'] : null;
}
function setConfig($chartjsConfig) {
$this->config = $chartjsConfig;
}
function setWidth($width) {
$this->width = $width;
}
function setHeight($height) {
$this->height = $height;
}
function setDevicePixelRatio($devicePixelRatio) {
$this->devicePixelRatio = $devicePixelRatio;
}
function setFormat($format) {
$this->format = $format;
}
function setBackgroundColor($backgroundColor) {
$this->backgroundColor = $backgroundColor;
}
function setApiKey($apiKey) {
$this->apiKey = $apiKey;
}
function getConfigStr() {
if (is_array($this->config)) {
return json_encode($this->config);
}
return $this->config;
}
function getUrl() {
$configStr = urlencode($this->getConfigStr());
$width = $this->width;
$height = $this->height;
$devicePixelRatio = number_format($this->devicePixelRatio, 1);
$format = $this->format;
$backgroundColor = $this->backgroundColor;
$url = sprintf('%s://%s:%s/chart?c=%s&w=%d&h=%d&devicePixelRatio=%f&format=%s&bkg=%s', $this->protocol, $this->host, $this->port, $configStr, $width, $height, $devicePixelRatio, $format, $backgroundColor);
if ($this->apiKey) {
$url .= '&key=' . $this->apiKey;
}
return $url;
}
function getShortUrl() {
if ($this->host != 'quickchart.io') {
throw new Exception('Short URLs must use quickchart.io host');
}
$ch = curl_init($this->getRootEndpoint() . '/chart/create');
$postData = array(
'backgroundColor' => $this->backgroundColor,
'width' => $this->width,
'height' => $this->height,
'format' => $this->format,
'chart' => $this->getConfigStr(),
);
if ($this->apiKey) {
$postData['key'] = $this->apiKey;
}
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
if ($result === false) {
throw new Exception(curl_error($ch), curl_errno($ch));
}
curl_close($ch);
// Note: do not dereference json_decode directly for 5.3 compatibility.
$ret = json_decode($result, true);
return $ret['url'];
}
function toBinary() {
$ch = curl_init($this->getRootEndpoint() . '/chart');
$postData = array(
'backgroundColor' => $this->backgroundColor,
'width' => $this->width,
'height' => $this->height,
'format' => $this->format,
'chart' => $this->getConfigStr(),
);
if ($this->apiKey) {
$postData['key'] = $this->apiKey;
}
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
function toFile($path) {
$data = $this->toBinary();
file_put_contents($path, $data);
}
protected function getRootEndpoint() {
return $this->protocol . '://' . $this->host . ':' . $this->port;
}
}