|
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
|
|
|
$client = $this->client->request('GET', 'handwritings', $this->auth); |
|
27
|
|
|
return json_decode($client->getBody()->getContents()); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function getHandwriting(string $handwritingID): object |
|
31
|
|
|
{ |
|
32
|
|
|
$client = $this->client->request('GET', 'handwritings/'.$handwritingID, $this->auth); |
|
33
|
|
|
return json_decode($client->getBody()->getContents()); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function renderPNGImage($build): string |
|
37
|
|
|
{ |
|
38
|
|
|
$params = ['headers'=> ['Content-Type' => 'image/png']]; |
|
39
|
|
|
$this->auth = array_merge($this->auth, $params); |
|
40
|
|
|
$client = $this->client->request('GET', '/render/png?'.$build, $this->auth); |
|
41
|
|
|
$png = 'data:image/png;base64,' . base64_encode($client->getBody()->getContents()); |
|
42
|
|
|
return '<img src="'.$png.'">'; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function renderPNGString($build): string |
|
46
|
|
|
{ |
|
47
|
|
|
$params = ['headers'=> ['Content-Type' => 'image/png']]; |
|
48
|
|
|
$this->auth = array_merge($this->auth, $params); |
|
49
|
|
|
$client = $this->client->request('GET', '/render/png?'.$build, $this->auth); |
|
50
|
|
|
return 'data:image/png;base64,' . base64_encode($client->getBody()->getContents()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function renderPDF($build,$filePath=__DIR__.'/pdf/',$fileName='test.pdf'): string |
|
54
|
|
|
{ |
|
55
|
|
|
if (!file_exists($filePath)) { mkdir($filePath, 0777, true); } |
|
56
|
|
|
$params = ['headers'=> ['Content-Type' => 'application/pdf','Content-disposition'=>'inline; filename=myFile.pdf','Accept-Ranges' => 'bytes'],'sink'=> $filePath.$fileName ]; |
|
57
|
|
|
$this->auth = array_merge($this->auth, $params); |
|
58
|
|
|
$this->client->request('GET', '/render/pdf?'.$build, $this->auth); |
|
59
|
|
|
return $filePath.$fileName; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.