Convertion des images webp et png vers jpeg

This commit is contained in:
ext.jeremy.guillot@maxicoffee.domains
2024-09-30 22:16:20 +02:00
parent 21b2adfa07
commit 5f15d14ae1
13 changed files with 226 additions and 158 deletions

View File

@@ -8,7 +8,6 @@ use App\Entity\Manga;
use App\Event\PageScrappingProgressEvent;
use App\Manager\FileSystemManager;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\RequestException;
@@ -19,11 +18,10 @@ abstract class AbstractScraper implements ScraperInterface
protected Client $httpClient;
public function __construct(
protected FileSystemManager $fileSystemManager,
protected FileSystemManager $fileSystemManager,
protected EventDispatcherInterface $eventDispatcher,
protected EntityManagerInterface $entityManager
)
{
protected EntityManagerInterface $entityManager
) {
$this->httpClient = new Client();
}
@@ -45,7 +43,8 @@ abstract class AbstractScraper implements ScraperInterface
{
try {
$response = $this->httpClient->head($url);
return $response->getStatusCode() === 200;
return 200 === $response->getStatusCode();
} catch (RequestException $e) {
return false;
}
@@ -60,14 +59,15 @@ abstract class AbstractScraper implements ScraperInterface
$chapter->getVolume(),
$chapter->getNumber()
);
return $volumeDir . '/' . $fileName;
return $volumeDir.'/'.$fileName;
}
protected function createCbzFile(array $pageData, string $cbzFilePath): void
{
$zip = new \ZipArchive();
if ($zip->open($cbzFilePath, \ZipArchive::CREATE) === TRUE) {
if (true === $zip->open($cbzFilePath, \ZipArchive::CREATE)) {
foreach ($pageData as $page) {
$zip->addFile($page['local_image_url'], basename($page['local_image_url']));
}
@@ -93,21 +93,67 @@ abstract class AbstractScraper implements ScraperInterface
/**
* @throws GuzzleException
* @throws Exception
* @throws \Exception
*/
protected function downloadAndSaveImage(string $imageUrl, string $destinationPath): void
protected function downloadAndSaveImage(string $imageUrl, string $destinationPath): string
{
try {
$response = $this->httpClient->get($imageUrl);
$contentType = $response->getHeaderLine('Content-Type');
if (str_starts_with($contentType, 'image/')) {
file_put_contents($destinationPath, $response->getBody()->getContents());
} else {
throw new Exception('Le contenu récupéré n\'est pas une image. Type de contenu : ' . $contentType);
if (!str_starts_with($contentType, 'image/')) {
throw new \Exception('Le contenu récupéré n\'est pas une image. Type de contenu : '.$contentType);
}
} catch (Exception $e) {
throw new Exception('Erreur lors de la récupération de l\'image : ' . $e->getMessage());
$imageData = $response->getBody()->getContents();
$tempFilePath = $this->saveTempFile($imageData);
$image = $this->createImageResource($tempFilePath, $contentType);
if (false === $image) {
throw new \Exception('Échec de la création de la ressource image.');
}
$destinationPath = $this->ensureJpgExtension($destinationPath);
if (!imagejpeg($image, $destinationPath)) {
imagedestroy($image);
unlink($tempFilePath);
throw new \Exception('Échec de la sauvegarde de l\'image en JPG.');
}
imagedestroy($image);
unlink($tempFilePath);
return $destinationPath;
} catch (\Exception $e) {
throw new \Exception('Erreur lors de la récupération de l\'image : '.$e->getMessage());
}
}
private function saveTempFile(string $data): string
{
$tempFilePath = tempnam(sys_get_temp_dir(), 'manga_img_');
file_put_contents($tempFilePath, $data);
return $tempFilePath;
}
/**
* @throws \Exception
*/
private function createImageResource(string $filePath, string $contentType)
{
return match ($contentType) {
'image/webp' => imagecreatefromwebp($filePath),
'image/png' => imagecreatefrompng($filePath),
'image/jpeg', 'image/jpg' => imagecreatefromjpeg($filePath),
default => throw new \Exception('Format d\'image non pris en charge : '.$contentType),
};
}
private function ensureJpgExtension(string $path): string
{
$info = pathinfo($path);
return $info['dirname'].'/'.$info['filename'].'.jpg';
}
}