mirror of
https://github.com/ceratic/MediaCollectorLibary.git
synced 2026-05-13 23:56:46 +02:00
actors / poster images
This commit is contained in:
@@ -10,13 +10,19 @@ class ImageDownloader
|
||||
private Client $httpClient;
|
||||
private string $basePath;
|
||||
|
||||
public function __construct(string $basePath = 'public/images')
|
||||
public function __construct(string $basePath = 'public/images', ?string $apiKey = null)
|
||||
{
|
||||
$headers = [
|
||||
'User-Agent' => 'MediaCollector/1.0'
|
||||
];
|
||||
|
||||
if ($apiKey) {
|
||||
$headers['ApiKey'] = $apiKey;
|
||||
}
|
||||
|
||||
$this->httpClient = new Client([
|
||||
'timeout' => 30,
|
||||
'headers' => [
|
||||
'User-Agent' => 'MediaCollector/1.0'
|
||||
]
|
||||
'headers' => $headers
|
||||
]);
|
||||
$this->basePath = rtrim($basePath, '/');
|
||||
}
|
||||
@@ -27,6 +33,7 @@ class ImageDownloader
|
||||
public function downloadImage(string $url, string $filename, string $subfolder = ''): ?string
|
||||
{
|
||||
if (empty($url) || !filter_var($url, FILTER_VALIDATE_URL)) {
|
||||
error_log("Invalid URL provided: {$url}");
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -48,10 +55,45 @@ class ImageDownloader
|
||||
return $filePath;
|
||||
}
|
||||
|
||||
$response = $this->httpClient->get($url, ['sink' => $filePath]);
|
||||
error_log("Downloading image from: {$url} to: {$filePath}");
|
||||
|
||||
if ($response->getStatusCode() === 200) {
|
||||
return $filePath;
|
||||
$response = $this->httpClient->get($url, [
|
||||
'sink' => $filePath,
|
||||
'headers' => [
|
||||
'User-Agent' => 'MediaCollector/1.0',
|
||||
'Accept' => 'image/*',
|
||||
]
|
||||
]);
|
||||
|
||||
$statusCode = $response->getStatusCode();
|
||||
$contentType = $response->getHeaderLine('content-type');
|
||||
|
||||
error_log("Download response - Status: {$statusCode}, Content-Type: {$contentType}");
|
||||
|
||||
if ($statusCode === 200) {
|
||||
$fileSize = filesize($filePath);
|
||||
error_log("Successfully downloaded image. Size: {$fileSize} bytes");
|
||||
|
||||
// Check if file is actually an image and not empty
|
||||
if ($fileSize > 0) {
|
||||
$imageInfo = getimagesize($filePath);
|
||||
if ($imageInfo !== false) {
|
||||
error_log("Valid image downloaded: {$imageInfo[0]}x{$imageInfo[1]} {$imageInfo['mime']}");
|
||||
return $filePath;
|
||||
} else {
|
||||
error_log("Downloaded file is not a valid image");
|
||||
if (file_exists($filePath)) {
|
||||
unlink($filePath);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
error_log("Downloaded file is empty");
|
||||
if (file_exists($filePath)) {
|
||||
unlink($filePath);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
error_log("Failed to download image. HTTP Status: {$statusCode}");
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user