forked from tinify/wordpress-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSource.php
More file actions
79 lines (64 loc) · 2.39 KB
/
Source.php
File metadata and controls
79 lines (64 loc) · 2.39 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
<?php
namespace Tinify;
class Source {
private $url, $commands;
public static function fromFile($path) {
return self::fromBuffer(file_get_contents($path));
}
public static function fromBuffer($string) {
$response = Tinify::getClient()->request("post", "/shrink", $string);
return new self($response->headers["location"]);
}
public static function fromUrl($url) {
$body = array("source" => array("url" => $url));
$response = Tinify::getClient()->request("post", "/shrink", $body);
return new self($response->headers["location"]);
}
public function __construct($url, $commands = array()) {
$this->url = $url;
$this->commands = $commands;
}
public function preserve() {
$options = $this->flatten(func_get_args());
$commands = array_merge($this->commands, array("preserve" => $options));
return new self($this->url, $commands);
}
public function resize($options) {
$commands = array_merge($this->commands, array("resize" => $options));
return new self($this->url, $commands);
}
public function store($options) {
$response = Tinify::getClient()->request("post", $this->url,
array_merge($this->commands, array("store" => $options)));
return new Result($response->headers, $response->body);
}
public function convert($options) {
$commands = array_merge($this->commands, array("convert" => $options));
return new self($this->url, $commands);
}
public function transform($options) {
$commands = array_merge($this->commands, array("transform" => $options));
return new self($this->url, $commands);
}
public function result() {
$response = Tinify::getClient()->request("get", $this->url, $this->commands);
return new Result($response->headers, $response->body);
}
public function toFile($path) {
return $this->result()->toFile($path);
}
public function toBuffer() {
return $this->result()->toBuffer();
}
private static function flatten($options) {
$flattened = array();
foreach ($options as $option) {
if (is_array($option)) {
$flattened = array_merge($flattened, $option);
} else {
array_push($flattened, $option);
}
}
return $flattened;
}
}