1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nickcheek\Handwriting; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\Psr7\Request; |
7
|
|
|
|
8
|
|
|
class Writer extends Builder |
9
|
|
|
{ |
10
|
|
|
protected string $key; |
11
|
|
|
protected array $auth; |
12
|
|
|
protected string $secret; |
13
|
|
|
protected Client $client; |
14
|
|
|
|
15
|
|
|
public function __construct($key, $secret) |
16
|
|
|
{ |
17
|
|
|
parent::__construct(); |
18
|
|
|
$this->key = $key; |
19
|
|
|
$this->secret = $secret; |
20
|
|
|
$this->auth = ['auth' => [$this->key, $this->secret]]; |
21
|
|
|
$this->client = new Client(['base_uri' => 'https://api.handwriting.io/']); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function getAllHandwriting(array $values=[]): array |
25
|
|
|
{ |
26
|
|
|
if(!empty($values)){ |
27
|
|
|
$client = $this->client->request('GET', 'handwritings?' . $this->buildURL($values), $this->auth); |
28
|
|
|
} else { |
29
|
|
|
$client = $this->client->request('GET', 'handwritings', $this->auth); |
30
|
|
|
} |
31
|
|
|
return json_decode($client->getBody()->getContents()); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function getHandwriting(string $handwritingID): object |
35
|
|
|
{ |
36
|
|
|
$client = $this->client->request('GET', 'handwritings/'.$handwritingID, $this->auth); |
37
|
|
|
return json_decode($client->getBody()->getContents()); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function renderPNGImage(string $build): string |
41
|
|
|
{ |
42
|
|
|
$params = ['headers'=> ['Content-Type' => 'image/png']]; |
43
|
|
|
$this->auth = array_merge($this->auth, $params); |
44
|
|
|
$client = $this->client->request('GET', '/render/png?'.$build, $this->auth); |
45
|
|
|
$png = 'data:image/png;base64,' . base64_encode($client->getBody()->getContents()); |
46
|
|
|
return '<img src="'.$png.'">'; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function renderPNGString(string $build): string |
50
|
|
|
{ |
51
|
|
|
$params = ['headers'=> ['Content-Type' => 'image/png']]; |
52
|
|
|
$this->auth = array_merge($this->auth, $params); |
53
|
|
|
$client = $this->client->request('GET', '/render/png?'.$build, $this->auth); |
54
|
|
|
return 'data:image/png;base64,' . base64_encode($client->getBody()->getContents()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function renderPDF(string $build, string $filePath=__DIR__.'/pdf/',string $fileName='test.pdf'): string |
58
|
|
|
{ |
59
|
|
|
if (!file_exists($filePath)) { mkdir($filePath, 0777, true); } |
60
|
|
|
$params = ['headers'=> ['Content-Type' => 'application/pdf','Content-disposition'=>'inline; filename=myFile.pdf','Accept-Ranges' => 'bytes'],'sink'=> $filePath.$fileName ]; |
61
|
|
|
$this->auth = array_merge($this->auth, $params); |
62
|
|
|
$this->client->request('GET', '/render/pdf?'.$build, $this->auth); |
63
|
|
|
return $filePath.$fileName; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function buildURL(array $values): string |
67
|
|
|
{ |
68
|
|
|
$this->url = ''; |
69
|
|
|
foreach($values as $key=>$value) { |
70
|
|
|
switch ($key) { |
71
|
|
|
case 'limit': |
72
|
|
|
$this->url .= '&limit=' . $value; |
73
|
|
|
break; |
74
|
|
|
case 'offset': |
75
|
|
|
$this->url .= '&offset=' . $value; |
76
|
|
|
break; |
77
|
|
|
case 'order_dir': |
78
|
|
|
$this->url .= '&order_dir=' . $value; |
79
|
|
|
break; |
80
|
|
|
case 'order_by': |
81
|
|
|
$this->url .= '&order_by=' . $value; |
82
|
|
|
break; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
return $this->url; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|