|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Movies\Utils; |
|
4
|
|
|
|
|
5
|
|
|
class Poster |
|
6
|
|
|
{ |
|
7
|
|
|
const TMDB_BASE_URL = 'https://image.tmdb.org/t/p/original'; |
|
8
|
|
|
const BASE_URL = '/f/movies/{movieId}/poster.jpg'; |
|
9
|
|
|
const BASE_PATH = __DIR__.'/../../../public/f/movies/{movieId}/poster.jpg'; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @param int $movieId |
|
13
|
|
|
* @param string $posterUrl |
|
14
|
|
|
* |
|
15
|
|
|
* @return null|string |
|
16
|
|
|
*/ |
|
17
|
1 |
|
public static function savePoster(int $movieId, string $posterUrl): ?string |
|
18
|
|
|
{ |
|
19
|
1 |
|
$saveTo = str_replace('{movieId}', $movieId, self::BASE_PATH); |
|
20
|
1 |
|
$destinationDir = \dirname($saveTo); |
|
21
|
|
|
|
|
22
|
1 |
|
if (is_dir($destinationDir) === false) { |
|
23
|
1 |
|
if (is_file($destinationDir)) { |
|
24
|
|
|
unlink($destinationDir); |
|
25
|
|
|
} |
|
26
|
1 |
|
mkdir($destinationDir); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
1 |
|
$ch = curl_init($posterUrl); |
|
30
|
1 |
|
curl_setopt($ch, CURLOPT_HEADER, 0); |
|
31
|
1 |
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
32
|
1 |
|
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); |
|
33
|
1 |
|
$raw = curl_exec($ch); |
|
34
|
|
|
|
|
35
|
1 |
|
if (curl_errno($ch) !== 0) { |
|
36
|
|
|
curl_close($ch); |
|
37
|
|
|
|
|
38
|
|
|
return null; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
1 |
|
curl_close($ch); |
|
42
|
|
|
|
|
43
|
1 |
|
if (file_exists($saveTo)) { |
|
44
|
|
|
self::removePoster($movieId); |
|
45
|
|
|
} |
|
46
|
1 |
|
$fp = fopen($saveTo, 'xb'); |
|
47
|
1 |
|
fwrite($fp, $raw); |
|
48
|
1 |
|
fclose($fp); |
|
49
|
1 |
|
chmod($saveTo, 0777); |
|
50
|
1 |
|
chmod($destinationDir, 0777); |
|
51
|
|
|
|
|
52
|
1 |
|
if (file_exists($saveTo) === false) { |
|
53
|
|
|
return null; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
return $saveTo; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public static function removePoster(int $movieId): void |
|
60
|
|
|
{ |
|
61
|
|
|
$saveTo = \str_replace('{movieId}', $movieId, self::BASE_PATH); |
|
62
|
|
|
$dir = \dirname($saveTo); |
|
63
|
|
|
$files = \scandir($dir); |
|
64
|
|
|
foreach ($files as $file) { |
|
65
|
|
|
if (mb_substr($file, 0, 6) === 'poster' && mb_strpos($file, '.') !== false) { |
|
66
|
|
|
// if its file like poster.jpg or poster.260x380.jpg - remove it |
|
67
|
|
|
unlink($dir.\DIRECTORY_SEPARATOR.$file); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param int $movieId |
|
74
|
|
|
* |
|
75
|
|
|
* @return string |
|
76
|
|
|
*/ |
|
77
|
1 |
|
public static function getUrl(int $movieId): string |
|
78
|
|
|
{ |
|
79
|
1 |
|
return str_replace('{movieId}', $movieId, self::BASE_URL); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param int $movieId |
|
84
|
|
|
* |
|
85
|
|
|
* @return string |
|
86
|
|
|
*/ |
|
87
|
1 |
|
public static function getPath(int $movieId): string |
|
88
|
|
|
{ |
|
89
|
1 |
|
return str_replace('{movieId}', $movieId, self::BASE_PATH); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public static function getPredefinedSizes(): array |
|
93
|
|
|
{ |
|
94
|
|
|
return [ |
|
95
|
|
|
['width' => 320, 'height' => 480], |
|
96
|
|
|
['width' => 420, 'height' => 620], |
|
97
|
|
|
]; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|