@@ -46,6 +46,9 @@ |
||
| 46 | 46 | return $url; |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | + /** |
|
| 50 | + * @param string $url |
|
| 51 | + */ |
|
| 49 | 52 | protected function downloadMedia($url) |
| 50 | 53 | { |
| 51 | 54 | $filepath = sys_get_temp_dir().'/tmp.jpg'; |
@@ -8,54 +8,54 @@ |
||
| 8 | 8 | |
| 9 | 9 | class MediaProvider extends BaseProvider |
| 10 | 10 | { |
| 11 | - protected $mediaManager; |
|
| 12 | - |
|
| 13 | - public function __construct(MediaManager $mediaManager) |
|
| 14 | - { |
|
| 15 | - $this->mediaManager = $mediaManager; |
|
| 16 | - } |
|
| 17 | - |
|
| 18 | - public function randomMedia ($width = null, $height = null, $type = "color") |
|
| 19 | - { |
|
| 20 | - $file = $this->downloadMedia($this->generateUrl($width, $height, $type)); |
|
| 21 | - $media = $this->mediaManager->upload($file); |
|
| 22 | - return $media; |
|
| 23 | - } |
|
| 24 | - |
|
| 25 | - protected function generateUrl ($width = null, $height = null, $type = "color") |
|
| 26 | - { |
|
| 27 | - $url = "http://lorempixel.com/"; |
|
| 28 | - |
|
| 29 | - if ($type !== 'color') { |
|
| 30 | - $url .= 'g/'; |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - if($width === null && $height !== null) { |
|
| 34 | - $width = round($height * 4 / 3); |
|
| 35 | - } elseif ($width !== null && $height === null) { |
|
| 36 | - $height = round($width * 3 / 4); |
|
| 37 | - } else { |
|
| 38 | - $width = rand(800, 1600); |
|
| 39 | - $height = round($width * 3 / 4); |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - $url .= $width.'/'.$height; |
|
| 43 | - |
|
| 44 | - $category = ['abstract', 'city', 'nature']; |
|
| 45 | - $url .= '/'.$category[array_rand($category, 1)].'/'; |
|
| 46 | - return $url; |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - protected function downloadMedia($url) |
|
| 50 | - { |
|
| 51 | - $filepath = sys_get_temp_dir().'/tmp.jpg'; |
|
| 52 | - $ch = curl_init($url); |
|
| 53 | - $fp = fopen($filepath,'wb'); |
|
| 54 | - curl_setopt($ch, CURLOPT_FILE, $fp); |
|
| 55 | - curl_setopt($ch, CURLOPT_HEADER, 0); |
|
| 56 | - curl_exec($ch); |
|
| 57 | - curl_close($ch); |
|
| 58 | - fclose($fp); |
|
| 59 | - return new File($filepath, 'random'); |
|
| 60 | - } |
|
| 11 | + protected $mediaManager; |
|
| 12 | + |
|
| 13 | + public function __construct(MediaManager $mediaManager) |
|
| 14 | + { |
|
| 15 | + $this->mediaManager = $mediaManager; |
|
| 16 | + } |
|
| 17 | + |
|
| 18 | + public function randomMedia ($width = null, $height = null, $type = "color") |
|
| 19 | + { |
|
| 20 | + $file = $this->downloadMedia($this->generateUrl($width, $height, $type)); |
|
| 21 | + $media = $this->mediaManager->upload($file); |
|
| 22 | + return $media; |
|
| 23 | + } |
|
| 24 | + |
|
| 25 | + protected function generateUrl ($width = null, $height = null, $type = "color") |
|
| 26 | + { |
|
| 27 | + $url = "http://lorempixel.com/"; |
|
| 28 | + |
|
| 29 | + if ($type !== 'color') { |
|
| 30 | + $url .= 'g/'; |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + if($width === null && $height !== null) { |
|
| 34 | + $width = round($height * 4 / 3); |
|
| 35 | + } elseif ($width !== null && $height === null) { |
|
| 36 | + $height = round($width * 3 / 4); |
|
| 37 | + } else { |
|
| 38 | + $width = rand(800, 1600); |
|
| 39 | + $height = round($width * 3 / 4); |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + $url .= $width.'/'.$height; |
|
| 43 | + |
|
| 44 | + $category = ['abstract', 'city', 'nature']; |
|
| 45 | + $url .= '/'.$category[array_rand($category, 1)].'/'; |
|
| 46 | + return $url; |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + protected function downloadMedia($url) |
|
| 50 | + { |
|
| 51 | + $filepath = sys_get_temp_dir().'/tmp.jpg'; |
|
| 52 | + $ch = curl_init($url); |
|
| 53 | + $fp = fopen($filepath,'wb'); |
|
| 54 | + curl_setopt($ch, CURLOPT_FILE, $fp); |
|
| 55 | + curl_setopt($ch, CURLOPT_HEADER, 0); |
|
| 56 | + curl_exec($ch); |
|
| 57 | + curl_close($ch); |
|
| 58 | + fclose($fp); |
|
| 59 | + return new File($filepath, 'random'); |
|
| 60 | + } |
|
| 61 | 61 | } |
| 62 | 62 | \ No newline at end of file |
@@ -15,14 +15,14 @@ discard block |
||
| 15 | 15 | $this->mediaManager = $mediaManager; |
| 16 | 16 | } |
| 17 | 17 | |
| 18 | - public function randomMedia ($width = null, $height = null, $type = "color") |
|
| 18 | + public function randomMedia($width = null, $height = null, $type = "color") |
|
| 19 | 19 | { |
| 20 | 20 | $file = $this->downloadMedia($this->generateUrl($width, $height, $type)); |
| 21 | 21 | $media = $this->mediaManager->upload($file); |
| 22 | 22 | return $media; |
| 23 | 23 | } |
| 24 | 24 | |
| 25 | - protected function generateUrl ($width = null, $height = null, $type = "color") |
|
| 25 | + protected function generateUrl($width = null, $height = null, $type = "color") |
|
| 26 | 26 | { |
| 27 | 27 | $url = "http://lorempixel.com/"; |
| 28 | 28 | |
@@ -30,7 +30,7 @@ discard block |
||
| 30 | 30 | $url .= 'g/'; |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | - if($width === null && $height !== null) { |
|
| 33 | + if ($width === null && $height !== null) { |
|
| 34 | 34 | $width = round($height * 4 / 3); |
| 35 | 35 | } elseif ($width !== null && $height === null) { |
| 36 | 36 | $height = round($width * 3 / 4); |
@@ -50,7 +50,7 @@ discard block |
||
| 50 | 50 | { |
| 51 | 51 | $filepath = sys_get_temp_dir().'/tmp.jpg'; |
| 52 | 52 | $ch = curl_init($url); |
| 53 | - $fp = fopen($filepath,'wb'); |
|
| 53 | + $fp = fopen($filepath, 'wb'); |
|
| 54 | 54 | curl_setopt($ch, CURLOPT_FILE, $fp); |
| 55 | 55 | curl_setopt($ch, CURLOPT_HEADER, 0); |
| 56 | 56 | curl_exec($ch); |