Files
MediaCollectorLibary/app/Utils/ImageDownloader.php
Lars Behrends 929ee43001 first commit
2025-10-17 13:29:28 +02:00

95 lines
2.5 KiB
PHP

<?php
namespace App\Utils;
use GuzzleHttp\Client;
use Exception;
class ImageDownloader
{
private Client $httpClient;
private string $basePath;
public function __construct(string $basePath = 'public/images')
{
$this->httpClient = new Client([
'timeout' => 30,
'headers' => [
'User-Agent' => 'MediaCollector/1.0'
]
]);
$this->basePath = rtrim($basePath, '/');
}
/**
* Download an image from URL and save it locally
*/
public function downloadImage(string $url, string $filename, string $subfolder = ''): ?string
{
if (empty($url) || !filter_var($url, FILTER_VALIDATE_URL)) {
return null;
}
try {
$folderPath = $this->basePath;
if (!empty($subfolder)) {
$folderPath .= '/' . trim($subfolder, '/');
}
// Create folder if it doesn't exist
if (!is_dir($folderPath)) {
mkdir($folderPath, 0755, true);
}
$filePath = $folderPath . '/' . $filename;
// Check if file already exists
if (file_exists($filePath)) {
return $filePath;
}
$response = $this->httpClient->get($url, ['sink' => $filePath]);
if ($response->getStatusCode() === 200) {
return $filePath;
}
return null;
} catch (Exception $e) {
// Log error but don't throw - images are not critical
error_log("Failed to download image {$url}: " . $e->getMessage());
return null;
}
}
/**
* Generate a unique filename for an image
*/
public function generateFilename(string $url, string $prefix = ''): string
{
$extension = pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_EXTENSION);
if (empty($extension)) {
$extension = 'jpg'; // Default fallback
}
$hash = substr(md5($url . time()), 0, 8);
return ($prefix ? $prefix . '_' : '') . $hash . '.' . $extension;
}
/**
* Get the public URL for a local image
*/
public function getPublicUrl(string $localPath): ?string
{
if (empty($localPath) || !file_exists($localPath)) {
return null;
}
// Remove the public/ prefix to get the web-accessible path
$relativePath = str_replace($this->basePath . '/', '', $localPath);
return '/' . $relativePath;
}
}