|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Siro\RandImg; |
|
4
|
|
|
|
|
5
|
|
|
use Faker\Provider\Base; |
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
use Exception; |
|
8
|
|
|
|
|
9
|
|
|
class RandImgProvider extends Base |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var string |
|
13
|
|
|
*/ |
|
14
|
|
|
private $baseUrl = 'http://www.rand-img.com'; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Utility method for provide random numbers for the urls. |
|
18
|
|
|
* |
|
19
|
|
|
* @return integer Random number. |
|
20
|
|
|
*/ |
|
21
|
9 |
|
private function getRandNumber($min = 1, $max = 1000000) |
|
22
|
|
|
{ |
|
23
|
9 |
|
return mt_rand($min, $max); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Downloads a file from the specified url and saves it in the |
|
28
|
|
|
* full path passed. It uses cURL. |
|
29
|
|
|
* |
|
30
|
|
|
* @param string $url The url of the image to download |
|
31
|
|
|
* @param string $filePath The full path where store the image |
|
32
|
|
|
* |
|
33
|
|
|
* @return bool true if success, else remove the image and return false. |
|
34
|
|
|
*/ |
|
35
|
9 |
|
private function getRemoteImage($url, $filePath) |
|
36
|
|
|
{ |
|
37
|
9 |
|
$fp = fopen($filePath, 'w'); |
|
38
|
9 |
|
$ch = curl_init($url); |
|
39
|
9 |
|
curl_setopt($ch, CURLOPT_FILE, $fp); |
|
40
|
9 |
|
$success = curl_exec($ch) && curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200; |
|
41
|
9 |
|
fclose($fp); |
|
42
|
9 |
|
curl_close($ch); |
|
43
|
|
|
|
|
44
|
9 |
|
if (!$success) { |
|
45
|
3 |
|
unlink($filePath); |
|
46
|
1 |
|
} |
|
47
|
|
|
|
|
48
|
9 |
|
return $success; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Obtain the full path for an image file. |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $dir The directory where store the image to download |
|
55
|
|
|
* @param string $type The image format |
|
56
|
|
|
* @throws InvalidArgumentException if $dir is not a directory or is not writeable |
|
57
|
|
|
* |
|
58
|
|
|
* @return string full path. |
|
59
|
|
|
*/ |
|
60
|
15 |
|
private function getFullPath($dir = null, $type = 'jpg') |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
15 |
|
$dir = is_null($dir) ? sys_get_temp_dir() : $dir; |
|
63
|
15 |
|
if (!is_dir($dir) || !is_writeable($dir)) { |
|
64
|
6 |
|
throw new InvalidArgumentException(sprintf('Cannot write to directory "%s"', $dir)); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
9 |
|
$fileName = md5(uniqid(empty($_SERVER['SERVER_ADDR']) ? '' : $_SERVER['SERVER_ADDR'], true)) .'.jpg'; |
|
68
|
9 |
|
return $dir . DIRECTORY_SEPARATOR . $fileName; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Generate a random image url. |
|
73
|
|
|
* |
|
74
|
|
|
* @param integer $width |
|
75
|
|
|
* @param integer $height |
|
76
|
|
|
* @param string $category The image topic. Defaults to empty (no category). |
|
77
|
|
|
* @param array $params Optional associative array with the list of parameters for the image. |
|
78
|
|
|
* You can see a list of parameters and its possible values in |
|
79
|
|
|
* https://github.com/SiroDiaz/RandImgProvider/blob/master/README.md |
|
80
|
|
|
* |
|
81
|
|
|
* @return string Returns the phrase passed in |
|
82
|
|
|
*/ |
|
83
|
30 |
|
public function imageUrl($width = 720, $height = 480, $category = '', array $params = []) |
|
84
|
|
|
{ |
|
85
|
30 |
|
$url = $this->baseUrl; |
|
86
|
30 |
|
$url .= "/$width"; |
|
87
|
30 |
|
$url .= "/$height"; |
|
88
|
30 |
|
$url .= !empty($category) ? "/$category" : ''; |
|
89
|
|
|
|
|
90
|
30 |
|
if (isset($params['rand']) && $params['rand']) { |
|
91
|
6 |
|
$params['rand'] = $this->getRandNumber(); |
|
92
|
2 |
|
} |
|
93
|
|
|
|
|
94
|
30 |
|
if (count($params)) { |
|
95
|
6 |
|
$url .= '?'. http_build_query($params); |
|
96
|
2 |
|
} |
|
97
|
|
|
|
|
98
|
30 |
|
return $url; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Helper method that generate a squared image url. |
|
103
|
|
|
* |
|
104
|
|
|
* @param int $width The image width. Default to 720px. |
|
105
|
|
|
* @param array $params Optional associative array with the list of parameters for the image. |
|
106
|
|
|
*/ |
|
107
|
9 |
|
public function squaredImageUrl($width = 720, $category = '', array $params = []) |
|
108
|
|
|
{ |
|
109
|
9 |
|
return $this->imageUrl($width, $width, $category, $params); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Generate a random gif url. It can attach |
|
114
|
|
|
* a random number to avoid that multiple gifs loaded |
|
115
|
|
|
* in the page will be all the same gif. |
|
116
|
|
|
* |
|
117
|
|
|
* @param bool $rand Defaults to false |
|
118
|
|
|
*/ |
|
119
|
9 |
|
public function gifUrl($rand = false) |
|
120
|
|
|
{ |
|
121
|
6 |
|
return $rand |
|
122
|
5 |
|
? $this->baseUrl .'/gif?rand='. $this->getRandNumber() |
|
123
|
9 |
|
: $this->baseUrl .'/gif'; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Downloads an image to the specified directory. |
|
128
|
|
|
* |
|
129
|
|
|
* @param mixed $dir |
|
130
|
|
|
* @param integer $width |
|
131
|
|
|
* @param integer $height |
|
132
|
|
|
* @param string $category |
|
133
|
|
|
* @param array $params |
|
134
|
|
|
* @throws InvalidArgumentException If not a directory or writeable |
|
135
|
|
|
* |
|
136
|
|
|
* @return string Filename with the path |
|
137
|
|
|
*/ |
|
138
|
9 |
|
public function image($dir = null, $width = 720, $height = 480, $category = '', array $params = []) |
|
139
|
|
|
{ |
|
140
|
9 |
|
$fullPath = $this->getFullPath($dir); |
|
141
|
6 |
|
$url = $this->imageUrl($width, $height, $category, $params); |
|
142
|
6 |
|
if (!$this->getRemoteImage($url, $fullPath)) { |
|
143
|
3 |
|
throw new Exception('error downloading the image'); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
3 |
|
return $fullPath; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Downloads a gif to the specified directory. |
|
151
|
|
|
* |
|
152
|
|
|
* @param string $dir |
|
153
|
|
|
* @throws InvalidArgumentException If not a directory or writeable |
|
154
|
|
|
* |
|
155
|
|
|
* @return string Filename with the path |
|
156
|
|
|
*/ |
|
157
|
6 |
|
public function gif($dir = null) |
|
158
|
|
|
{ |
|
159
|
6 |
|
$fullPath = $this->getFullPath($dir, 'gif'); |
|
160
|
3 |
|
$url = $this->gifUrl(); |
|
161
|
3 |
|
if (!$this->getRemoteImage($url, $fullPath)) { |
|
162
|
|
|
throw new Exception('error downloading the image'); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
3 |
|
return $fullPath; |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.