<?php
namespace App\Controller;
use App\Repository\PhotoRecupererRepository;
use App\Repository\PhotoRepository;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
/**
* @Route("/recuperation", name="app_recuperation_")
*/
class RecuperationPhotoController extends AbstractController
{
private $em;
private $adresseSite = "https://www.pharmadexi.com";
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
/**
* @Route("/remove/photonovalide/{limit}", name="remove_photonovalide")
*/
public function removePhotoNovalide($limit = 150, PhotoRepository $photoRepo)
{
$nbphoto = 0;
$page = 1;
$nbAllPhoto = $photoRepo->countAllNumberNotChecked()[0]['nb'];
$offset = $page * $limit - $limit;
$pageMax = $nbAllPhoto / $limit;
if (is_float($pageMax)) {
$pageMax = (int)$pageMax + 1;
}
$pageNow = $page . "/" . $pageMax;
$photos = $photoRepo->getAllNotChecked($limit, $offset);
$projectDir = $this->formatDir(__DIR__);
foreach ($photos as $photo) {
$photo->setIsChecked(true);
$photoName = $this->formatLink($photo->getLien());
$realLink = $projectDir . $photoName;
//dd($projectDir, $realLink);
$imageSize = $this->getImageSizeWithImagick($realLink);
if ($imageSize === null) {
/* unlink($realLink); */
$photo->setNotdisponible(true);
$nbphoto++;
}
$this->em->persist($photo);
$this->em->flush();
}
return new JsonResponse(['photo supprimée' => $nbphoto, 'Page Now' => $pageNow, 'Offset' => $offset]);
}
private function formatDir($dir)
{
$search = '/src/Controller';
return str_replace($search, '', $dir) . "/public";
}
private function formatLink($dir)
{
$search = 'https://www.pharmadexi.com';
return str_replace($search, '', $dir);
}
private function getImageSizeWithImagick(string $relativePath): ?array
{
//dd($relativePath);
if (!file_exists($relativePath)) {
return null;
}
try {
$imageInfo = @getimagesize($relativePath);
if ($imageInfo !== false) {
return [
'width' => $imageInfo[0], // Largeur en pixels
'height' => $imageInfo[1], // Hauteur en pixels
'type' => image_type_to_mime_type($imageInfo[2]) // Type MIME (ex. : image/jpeg)
];
}
} catch (Exception $e) {
error_log('Erreur lors de la lecture de l\'image : ' . $e->getMessage());
return null;
}
return null;
}
/**
* @Route("/photo/{page}/{limit}", name="photo")
*/
public function photo($page, $limit = 25, PhotoRepository $photoRepo)
{
$nbphoto = 0;
$nbAllPhoto = $photoRepo->countAllNumber()[0]['nb'];
$offset = $page * $limit - $limit;
$pageMax = $nbAllPhoto / $limit;
if (is_float($pageMax)) {
$pageMax = (int)$pageMax + 1;
}
$pageNow = $page . "/" . $pageMax;
$photos = $photoRepo->getAllNotDownloader($limit, $offset);
foreach ($photos as $photo) {
$isBase = false;
if (str_starts_with($photo->getLien(), "data:image")) {
$isBase = true;
}
dump($isBase);
$name = $this->downloadPhoto($photo, $isBase);
if ($name) {
$name = $this->adresseSite . "/assets/img/product/" . $name;
$photo->setLien($name)->setDownloaded(true);
$this->em->persist($photo);
$nbphoto++;
} else {
$photo->setNotdisponible(true);
$this->em->persist($photo);
}
$this->em->flush();
}
return new JsonResponse(['photo récupérés' => $nbphoto, 'Page Now' => $pageNow, 'Offset' => $offset]);
}
private function downloadPhoto($photo, $isBase = false)
{
$url = $photo->getLien();
$extension = $this->getExtension($url);
$name = (new \DateTime())->format("YmdHis") . $photo->getId() . "." . $extension;
$saveTo = "assets/img/product/" . $name;
$imageContent = null;
if ($isBase === false) {
if ($this->isImageAvailableWithCurl($url)) {
/* $context = stream_context_create([
"ssl" => [
"verify_peer" => false,
"verify_peer_name" => false,
]
]);
$imageContent = file_get_contents($url, false, $context); */
$imageContent = $this->downloadWithCurl($url);
}
} else {
$url = substr($url, strpos($url, ',') + 1);
$imageContent = base64_decode($url);
}
if ($imageContent === false) {
return false;
}
file_put_contents($saveTo, $imageContent);
return $name;
}
private function downloadWithCurl($url)
{
$ch = curl_init($url);
// Définir les options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Désactiver la vérification SSL (utile en local)
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36");
$imageData = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $imageData;
}
private function getExtension($link): string
{
$imageFormats = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp', 'tiff', 'svg', 'ico', 'heic', 'heif'];
foreach ($imageFormats as $format) {
if (stripos($link, $format) !== false) {
return $format;
}
}
return "jpeg";
}
private function isImageAvailableWithCurl(string $url): bool
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
curl_close($ch);
return $httpCode === 200 && strpos($contentType, 'image/') === 0;
}
/**
* @Route("/copie/photo/{limit}", name="copiephoto")
*/
public function copiePhoto($limit = 25, PhotoRepository $photoRepo, PhotoRecupererRepository $photoRecupererRepo)
{
$nbphoto = 0;
$nbphotoNotFound = 0;
$page = 1;
$nbAllPhoto = $photoRepo->countAllNotCopied()[0]['nb'];
$offset = $page * $limit - $limit;
$pageMax = $nbAllPhoto / $limit;
if (is_float($pageMax)) {
$pageMax = (int)$pageMax + 1;
}
$photos = $photoRepo->getAllPhotoNotCopied($limit, $offset);
foreach ($photos as $photo) {
$photoRecup = $photoRecupererRepo->find($photo->getId());
if ($photoRecup) {
$photo->setIsCopied(true)->setLien($photoRecup->getLien());
$this->em->persist($photoRecup);
$this->em->flush();
$nbphoto++;
} else {
$nbphotoNotFound++;
}
}
return new JsonResponse(['photo copier' => $nbphoto, 'Page restant' => $pageMax, "Photo non trouvé" => $nbphotoNotFound]);
}
/**
* @Route("/supprimer/photonotsure/{limit}", name="supprimerphotonotsure")
*/
public function supprimerphotonotsure($limit = 25, PhotoRepository $photoRepo)
{
$nbphoto = 0;
$page = 1;
$nbAllPhoto = $photoRepo->countAllNotSure()[0]['nb'];
$offset = $page * $limit - $limit;
$pageMax = $nbAllPhoto / $limit;
if (is_float($pageMax)) {
$pageMax = (int)$pageMax + 1;
}
$photos = $photoRepo->getAllPhotoNotSure($limit, $offset);
$projectDir = $this->formatDir(__DIR__);
foreach ($photos as $photo) {
$nameFile = str_replace("https://www.pharmadexi.com/assets/img/product/", "", $photo->getLien());
$realLink = $projectDir . "/assets/img/product/" . $nameFile;
//dd($nbAllPhoto, $realLink, count($photos));
unlink($realLink);
$this->em->remove($photo);
$this->em->flush();
$nbphoto++;
}
return new JsonResponse(['photo supprimer' => $nbphoto, 'Page restant' => $pageMax]);
}
/**
* @Route("/supprimer/photonotdisponible/{limit}", name="supprimerphotonotdisponible")
*/
public function supprimerphotonotdisponible($limit = 25, PhotoRepository $photoRepo)
{
$nbphoto = 0;
$page = 1;
$nbAllPhoto = $photoRepo->countAllNumberNotDisponible()[0]['nb'];
$offset = $page * $limit - $limit;
$pageMax = $nbAllPhoto / $limit;
if (is_float($pageMax)) {
$pageMax = (int)$pageMax + 1;
}
$photos = $photoRepo->getAllNotDisponible($limit, $offset);
$projectDir = $this->formatDir(__DIR__);
foreach ($photos as $photo) {
$nameFile = str_replace("https://www.pharmadexi.com/assets/img/product/", "", $photo->getLien());
$realLink = $projectDir . "/assets/img/product/" . $nameFile;
//dd($nbAllPhoto, $realLink, count($photos));
unlink($realLink);
$this->em->remove($photo);
$this->em->flush();
$nbphoto++;
}
return new JsonResponse(['photo supprimer' => $nbphoto, 'Page restant' => $pageMax]);
}
}