src/Controller/RecuperationPhotoController.php line 200

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Repository\PhotoRecupererRepository;
  4. use App\Repository\PhotoRepository;
  5. use DateTime;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. /**
  11.  * @Route("/recuperation", name="app_recuperation_")
  12.  */
  13. class RecuperationPhotoController extends AbstractController
  14. {
  15.     private $em;
  16.     private $adresseSite "https://www.pharmadexi.com";
  17.     public function __construct(EntityManagerInterface $em)
  18.     {
  19.         $this->em           $em;
  20.     }
  21.     /**
  22.      * @Route("/remove/photonovalide/{limit}", name="remove_photonovalide")
  23.      */
  24.     public function removePhotoNovalide($limit 150PhotoRepository $photoRepo)
  25.     {
  26.         $nbphoto 0;
  27.         $page 1;
  28.         $nbAllPhoto $photoRepo->countAllNumberNotChecked()[0]['nb'];
  29.         $offset $page $limit $limit;
  30.         $pageMax $nbAllPhoto $limit;
  31.         if (is_float($pageMax)) {
  32.             $pageMax = (int)$pageMax 1;
  33.         }
  34.         $pageNow $page "/" $pageMax;
  35.         $photos $photoRepo->getAllNotChecked($limit$offset);
  36.         $projectDir $this->formatDir(__DIR__);
  37.         foreach ($photos as $photo) {
  38.             $photo->setIsChecked(true);
  39.             $photoName $this->formatLink($photo->getLien());
  40.             $realLink $projectDir $photoName;
  41.             //dd($projectDir, $realLink);
  42.             $imageSize $this->getImageSizeWithImagick($realLink);
  43.             if ($imageSize === null) {
  44.                 /* unlink($realLink); */
  45.                 $photo->setNotdisponible(true);
  46.                 $nbphoto++;
  47.             }
  48.             $this->em->persist($photo);
  49.             $this->em->flush();
  50.         }
  51.         return new JsonResponse(['photo supprimée' => $nbphoto'Page Now' => $pageNow'Offset' => $offset]);
  52.     }
  53.     private function formatDir($dir)
  54.     {
  55.         $search '/src/Controller';
  56.         return str_replace($search''$dir) . "/public";
  57.     }
  58.     private function formatLink($dir)
  59.     {
  60.         $search 'https://www.pharmadexi.com';
  61.         return str_replace($search''$dir);
  62.     }
  63.     private function getImageSizeWithImagick(string $relativePath): ?array
  64.     {
  65.         //dd($relativePath);
  66.         if (!file_exists($relativePath)) {
  67.             return null;
  68.         }
  69.         try {
  70.             $imageInfo = @getimagesize($relativePath);
  71.             if ($imageInfo !== false) {
  72.                 return [
  73.                     'width' => $imageInfo[0],  // Largeur en pixels
  74.                     'height' => $imageInfo[1], // Hauteur en pixels
  75.                     'type' => image_type_to_mime_type($imageInfo[2]) // Type MIME (ex. : image/jpeg)
  76.                 ];
  77.             }
  78.         } catch (Exception $e) {
  79.             error_log('Erreur lors de la lecture de l\'image : ' $e->getMessage());
  80.             return null;
  81.         }
  82.         return null;
  83.     }
  84.     /**
  85.      * @Route("/photo/{page}/{limit}", name="photo")
  86.      */
  87.     public function photo($page$limit 25PhotoRepository $photoRepo)
  88.     {
  89.         $nbphoto 0;
  90.         $nbAllPhoto $photoRepo->countAllNumber()[0]['nb'];
  91.         $offset $page $limit $limit;
  92.         $pageMax $nbAllPhoto $limit;
  93.         if (is_float($pageMax)) {
  94.             $pageMax = (int)$pageMax 1;
  95.         }
  96.         $pageNow $page "/" $pageMax;
  97.         $photos $photoRepo->getAllNotDownloader($limit$offset);
  98.         foreach ($photos as $photo) {
  99.             $isBase false;
  100.             if (str_starts_with($photo->getLien(), "data:image")) {
  101.                 $isBase true;
  102.             }
  103.             dump($isBase);
  104.             $name $this->downloadPhoto($photo$isBase);
  105.             if ($name) {
  106.                 $name $this->adresseSite "/assets/img/product/" $name;
  107.                 $photo->setLien($name)->setDownloaded(true);
  108.                 $this->em->persist($photo);
  109.                 $nbphoto++;
  110.             } else {
  111.                 $photo->setNotdisponible(true);
  112.                 $this->em->persist($photo);
  113.             }
  114.             $this->em->flush();
  115.         }
  116.         return new JsonResponse(['photo récupérés' => $nbphoto'Page Now' => $pageNow'Offset' => $offset]);
  117.     }
  118.     private function downloadPhoto($photo$isBase false)
  119.     {
  120.         $url $photo->getLien();
  121.         $extension $this->getExtension($url);
  122.         $name = (new \DateTime())->format("YmdHis") . $photo->getId() . "." $extension;
  123.         $saveTo "assets/img/product/" $name;
  124.         $imageContent null;
  125.         if ($isBase === false) {
  126.             if ($this->isImageAvailableWithCurl($url)) {
  127.                 /* $context = stream_context_create([
  128.                     "ssl" => [
  129.                         "verify_peer" => false,
  130.                         "verify_peer_name" => false,
  131.                     ]
  132.                 ]);
  133.                 $imageContent = file_get_contents($url, false, $context); */
  134.                 $imageContent $this->downloadWithCurl($url);
  135.             }
  136.         } else {
  137.             $url substr($urlstrpos($url',') + 1);
  138.             $imageContent base64_decode($url);
  139.         }
  140.         if ($imageContent === false) {
  141.             return false;
  142.         }
  143.         file_put_contents($saveTo$imageContent);
  144.         return $name;
  145.     }
  146.     private function downloadWithCurl($url)
  147.     {
  148.         $ch curl_init($url);
  149.         // Définir les options
  150.         curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  151.         curl_setopt($chCURLOPT_FOLLOWLOCATIONtrue);
  152.         curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse); // Désactiver la vérification SSL (utile en local)
  153.         curl_setopt($chCURLOPT_USERAGENT"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36");
  154.         $imageData curl_exec($ch);
  155.         $httpCode curl_getinfo($chCURLINFO_HTTP_CODE);
  156.         curl_close($ch);
  157.         return $imageData;
  158.     }
  159.     private function getExtension($link): string
  160.     {
  161.         $imageFormats = ['jpg''jpeg''png''gif''bmp''webp''tiff''svg''ico''heic''heif'];
  162.         foreach ($imageFormats as $format) {
  163.             if (stripos($link$format) !== false) {
  164.                 return $format;
  165.             }
  166.         }
  167.         return "jpeg";
  168.     }
  169.     private function isImageAvailableWithCurl(string $url): bool
  170.     {
  171.         $ch curl_init($url);
  172.         curl_setopt($chCURLOPT_NOBODYtrue);
  173.         curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  174.         curl_setopt($chCURLOPT_TIMEOUT10);
  175.         curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse);
  176.         curl_exec($ch);
  177.         $httpCode curl_getinfo($chCURLINFO_HTTP_CODE);
  178.         $contentType curl_getinfo($chCURLINFO_CONTENT_TYPE);
  179.         curl_close($ch);
  180.         return $httpCode === 200 && strpos($contentType'image/') === 0;
  181.     }
  182.     /**
  183.      * @Route("/copie/photo/{limit}", name="copiephoto")
  184.      */
  185.     public function copiePhoto($limit 25PhotoRepository $photoRepoPhotoRecupererRepository $photoRecupererRepo)
  186.     {
  187.         $nbphoto 0;
  188.         $nbphotoNotFound 0;
  189.         $page 1;
  190.         $nbAllPhoto $photoRepo->countAllNotCopied()[0]['nb'];
  191.         $offset $page $limit $limit;
  192.         $pageMax $nbAllPhoto $limit;
  193.         if (is_float($pageMax)) {
  194.             $pageMax = (int)$pageMax 1;
  195.         }
  196.         $photos $photoRepo->getAllPhotoNotCopied($limit$offset);
  197.         foreach ($photos as $photo) {
  198.             $photoRecup $photoRecupererRepo->find($photo->getId());
  199.             if ($photoRecup) {
  200.                 $photo->setIsCopied(true)->setLien($photoRecup->getLien());
  201.                 $this->em->persist($photoRecup);
  202.                 $this->em->flush();
  203.                 $nbphoto++;
  204.             } else {
  205.                 $nbphotoNotFound++;
  206.             }
  207.         }
  208.         return new JsonResponse(['photo copier' => $nbphoto'Page restant' => $pageMax"Photo non trouvé" => $nbphotoNotFound]);
  209.     }
  210.     /**
  211.      * @Route("/supprimer/photonotsure/{limit}", name="supprimerphotonotsure")
  212.      */
  213.     public function supprimerphotonotsure($limit 25PhotoRepository $photoRepo)
  214.     {
  215.         $nbphoto 0;
  216.         $page 1;
  217.         $nbAllPhoto $photoRepo->countAllNotSure()[0]['nb'];
  218.         $offset $page $limit $limit;
  219.         $pageMax $nbAllPhoto $limit;
  220.         if (is_float($pageMax)) {
  221.             $pageMax = (int)$pageMax 1;
  222.         }
  223.         $photos $photoRepo->getAllPhotoNotSure($limit$offset);
  224.         $projectDir $this->formatDir(__DIR__);
  225.         foreach ($photos as $photo) {
  226.             $nameFile str_replace("https://www.pharmadexi.com/assets/img/product/"""$photo->getLien());
  227.             $realLink $projectDir "/assets/img/product/" .  $nameFile;
  228.             //dd($nbAllPhoto, $realLink, count($photos));
  229.             unlink($realLink);
  230.             $this->em->remove($photo);
  231.             $this->em->flush();
  232.             $nbphoto++;
  233.         }
  234.         return new JsonResponse(['photo supprimer' => $nbphoto'Page restant' => $pageMax]);
  235.     }
  236.     /**
  237.      * @Route("/supprimer/photonotdisponible/{limit}", name="supprimerphotonotdisponible")
  238.      */
  239.     public function supprimerphotonotdisponible($limit 25PhotoRepository $photoRepo)
  240.     {
  241.         $nbphoto 0;
  242.         $page 1;
  243.         $nbAllPhoto $photoRepo->countAllNumberNotDisponible()[0]['nb'];
  244.         $offset $page $limit $limit;
  245.         $pageMax $nbAllPhoto $limit;
  246.         if (is_float($pageMax)) {
  247.             $pageMax = (int)$pageMax 1;
  248.         }
  249.         $photos $photoRepo->getAllNotDisponible($limit$offset);
  250.         $projectDir $this->formatDir(__DIR__);
  251.         foreach ($photos as $photo) {
  252.             $nameFile str_replace("https://www.pharmadexi.com/assets/img/product/"""$photo->getLien());
  253.             $realLink $projectDir "/assets/img/product/" .  $nameFile;
  254.             //dd($nbAllPhoto, $realLink, count($photos));
  255.             unlink($realLink);
  256.             $this->em->remove($photo);
  257.             $this->em->flush();
  258.             $nbphoto++;
  259.         }
  260.         return new JsonResponse(['photo supprimer' => $nbphoto'Page restant' => $pageMax]);
  261.     }
  262. }