1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kaliop\eZLoremIpsumBundle\Faker\Provider; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Almost the same as Image from Faker, but uses picsum.photos |
7
|
|
|
* @todo add support for https://loremflickr.com/ as an alternative, or see the list at |
8
|
|
|
* https://www.johanbostrom.se/blog/the-best-image-placeholder-services-on-the-web |
9
|
|
|
*/ |
10
|
|
|
class Picture extends Base |
11
|
|
|
{ |
12
|
|
|
public function pictureUrl($width = 640, $height = 480, $randomize = true, $word = null, $gray = false) |
|
|
|
|
13
|
|
|
{ |
14
|
|
|
$baseUrl = "https://picsum.photos/"; |
15
|
|
|
$url = "{$width}/{$height}/"; |
16
|
|
|
|
17
|
|
|
if ($gray) { |
18
|
|
|
$url = "g/" . $url; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
if ($randomize) { |
22
|
|
|
$url .= '?random'; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
return $baseUrl . $url; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Download a remote random image to disk and return its location |
30
|
|
|
* |
31
|
|
|
* Requires curl, or allow_url_fopen to be on in php.ini. |
32
|
|
|
* |
33
|
|
|
* @example '/path/to/dir/13b73edae8443990be1aa8f1a483bc27.jpg' |
34
|
|
|
*/ |
35
|
|
|
public function picture($dir = null, $width = 640, $height = 480, $fullPath = true, $randomize = true, $word = null) |
36
|
|
|
{ |
37
|
|
|
$dir = is_null($dir) ? sys_get_temp_dir() : $dir; // GNU/Linux / OS X / Windows compatible |
38
|
|
|
// Validate directory path |
39
|
|
|
if (!is_dir($dir) || !is_writable($dir)) { |
40
|
|
|
throw new \InvalidArgumentException(sprintf('Cannot write to directory "%s"', $dir)); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
// Generate a random filename. Use the server address so that a file |
44
|
|
|
// generated at the same time on a different server won't have a collision. |
45
|
|
|
$name = md5(uniqid(empty($_SERVER['SERVER_ADDR']) ? '' : $_SERVER['SERVER_ADDR'], true)); |
46
|
|
|
$filename = $name .'.jpg'; |
47
|
|
|
$filepath = $dir . DIRECTORY_SEPARATOR . $filename; |
48
|
|
|
|
49
|
|
|
$url = $this->pictureUrl($width, $height, $randomize, $word); |
50
|
|
|
|
51
|
|
|
// save file |
52
|
|
|
if (function_exists('curl_exec')) { |
53
|
|
|
// use cURL |
54
|
|
|
$fp = fopen($filepath, 'w'); |
55
|
|
|
$ch = curl_init($url); |
56
|
|
|
curl_setopt($ch, CURLOPT_FILE, $fp); |
57
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); |
58
|
|
|
$success = curl_exec($ch) && curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200; |
59
|
|
|
|
60
|
|
|
if ($success) { |
61
|
|
|
fclose($fp); |
62
|
|
|
} else { |
63
|
|
|
unlink($filepath); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
curl_close($ch); |
67
|
|
|
} elseif (ini_get('allow_url_fopen')) { |
68
|
|
|
// use remote fopen() via copy() |
69
|
|
|
$success = copy($url, $filepath); |
70
|
|
|
} else { |
71
|
|
|
return new \RuntimeException('The image formatter downloads an image from a remote HTTP server. Therefore, it requires that PHP can request remote hosts, either via cURL or fopen()'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if (!$success) { |
75
|
|
|
// could not contact the distant URL or HTTP error - fail silently. |
76
|
|
|
return null; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $fullPath ? $filepath : $filename; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.