|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mechpave\ImgurClient\Api; |
|
4
|
|
|
|
|
5
|
|
|
use Mechpave\ImgurClient\Entity\ImageInterface; |
|
6
|
|
|
use Mechpave\ImgurClient\Http\HttpClientInterface; |
|
7
|
|
|
use Mechpave\ImgurClient\Mapper\ImageMapper; |
|
8
|
|
|
use Mechpave\ImgurClient\Model\ImageModel; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class Image |
|
12
|
|
|
* @package Mechpave\ImgurClient\Api |
|
13
|
|
|
* @see http://api.imgur.com/endpoints/image |
|
14
|
|
|
*/ |
|
15
|
|
|
class Image extends AbstractApi |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var ImageMapper |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $imageMapper; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Image constructor. |
|
24
|
|
|
* @param HttpClientInterface $httpClient |
|
25
|
|
|
* @param ImageMapper $imageMapper |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct(HttpClientInterface $httpClient, ImageMapper $imageMapper) |
|
28
|
|
|
{ |
|
29
|
|
|
parent::__construct($httpClient); |
|
30
|
|
|
$this->imageMapper = $imageMapper; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Get image Information endpoint |
|
35
|
|
|
* |
|
36
|
|
|
* @param $id |
|
37
|
|
|
* @return ImageInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
public function get($id) |
|
40
|
|
|
{ |
|
41
|
|
|
$response = $this->httpClient->get( |
|
42
|
|
|
'image/' . $id |
|
43
|
|
|
); |
|
44
|
|
|
|
|
45
|
|
|
$image = $this->imageMapper->buildImage($response->getBody()['data']); |
|
46
|
|
|
|
|
47
|
|
|
return $image; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Upload Image to Imgur |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $image A binary file (path to file), base64 data, or a URL for an image. (up to 10MB) |
|
54
|
|
|
* @param string $type Image type. Use Mechpave\ImgurClient\Model\ImageModel |
|
55
|
|
|
* @param string $title The title of the image |
|
56
|
|
|
* @param string $description The description of the image |
|
57
|
|
|
* @param string $album The id of the album you want to add the image to. For anonymous albums, {album} should be the deletehash that is returned at creation. |
|
58
|
|
|
* @param string $name The name of the file |
|
59
|
|
|
* @throws \UnexpectedValueException |
|
60
|
|
|
* @return ImageInterface |
|
61
|
|
|
*/ |
|
62
|
|
|
public function upload( |
|
63
|
|
|
$image, |
|
64
|
|
|
$type, |
|
65
|
|
|
$title = null, |
|
66
|
|
|
$description = null, |
|
67
|
|
|
$album = null, |
|
68
|
|
|
$name = null |
|
69
|
|
|
) |
|
70
|
|
|
{ |
|
71
|
|
|
if ($type == ImageModel::TYPE_FILE) { |
|
72
|
|
|
//check if file exists and get its contents |
|
73
|
|
|
|
|
74
|
|
|
if (!file_exists($image)) { |
|
75
|
|
|
throw new \UnexpectedValueException('Provided file does not exist'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$contents = file_get_contents($image); |
|
79
|
|
|
if (!$contents) { |
|
80
|
|
|
throw new \UnexpectedValueException('Could not get file contents'); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$image = $contents; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$postData = [ |
|
87
|
|
|
'image' => $image, |
|
88
|
|
|
'type' => $type, |
|
89
|
|
|
]; |
|
90
|
|
|
|
|
91
|
|
|
if ($title) { |
|
|
|
|
|
|
92
|
|
|
$postData['title'] = $title; |
|
93
|
|
|
} |
|
94
|
|
|
if ($name) { |
|
|
|
|
|
|
95
|
|
|
$postData['name'] = $name; |
|
96
|
|
|
} |
|
97
|
|
|
if ($description) { |
|
|
|
|
|
|
98
|
|
|
$postData['description'] = $description; |
|
99
|
|
|
} |
|
100
|
|
|
if ($album) { |
|
|
|
|
|
|
101
|
|
|
$postData['album'] = $album; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$response = $this->httpClient->post( |
|
105
|
|
|
'image', |
|
106
|
|
|
$postData |
|
107
|
|
|
); |
|
108
|
|
|
|
|
109
|
|
|
$image = $this->imageMapper->buildImage($response->getBody()['data']); |
|
110
|
|
|
|
|
111
|
|
|
return $image; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Update uploaded image. You can only update title or description of the image |
|
116
|
|
|
* |
|
117
|
|
|
* @param string $id Imgur Id of the image. For anonymous image it must be deleteHash |
|
118
|
|
|
* @param string $title The title of the image. |
|
119
|
|
|
* @param string $description The description of the image |
|
120
|
|
|
* @return bool |
|
121
|
|
|
*/ |
|
122
|
|
|
public function update( |
|
123
|
|
|
$id, |
|
124
|
|
|
$title = null, |
|
125
|
|
|
$description = null |
|
126
|
|
|
) |
|
127
|
|
|
{ |
|
128
|
|
|
$postData = []; |
|
129
|
|
|
|
|
130
|
|
|
if ($title) { |
|
|
|
|
|
|
131
|
|
|
$postData['title'] = $title; |
|
132
|
|
|
} |
|
133
|
|
|
if ($description) { |
|
|
|
|
|
|
134
|
|
|
$postData['description'] = $description; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
$response = $this->httpClient->post( |
|
138
|
|
|
'image/' . $id, |
|
139
|
|
|
$postData |
|
140
|
|
|
); |
|
141
|
|
|
|
|
142
|
|
|
return $response->getBody()['success']; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Delete image from Imgur |
|
147
|
|
|
* |
|
148
|
|
|
* @param string $id Imgur Id of the image. For anonymous image it must be deleteHash |
|
149
|
|
|
* @return bool |
|
150
|
|
|
*/ |
|
151
|
|
|
public function delete($id) |
|
152
|
|
|
{ |
|
153
|
|
|
$response = $this->httpClient->delete( |
|
154
|
|
|
'image/' . $id |
|
155
|
|
|
); |
|
156
|
|
|
|
|
157
|
|
|
return $response->getBody()['success']; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Favorite an image with the given ID. The user is required to be logged in to favorite the image. |
|
162
|
|
|
* |
|
163
|
|
|
* @param string $id Imgur Id of the image. |
|
164
|
|
|
* @return bool |
|
165
|
|
|
*/ |
|
166
|
|
|
public function favorite($id) |
|
167
|
|
|
{ |
|
168
|
|
|
$response = $this->httpClient->post( |
|
169
|
|
|
'image/' . $id . '/favorite' |
|
170
|
|
|
); |
|
171
|
|
|
|
|
172
|
|
|
return $response->getBody()['success']; |
|
173
|
|
|
} |
|
174
|
|
|
} |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: