src/Twig/ImagesExtenstion.php line 59

Open in your IDE?
  1. <?php
  2. namespace App\Twig;
  3. use App\Entity\Post;
  4. use Twig\Environment;
  5. use Twig\TwigFunction;
  6. use App\Entity\Category;
  7. use App\Entity\Language;
  8. use App\Entity\TypePost;
  9. use App\Entity\CpPostField;
  10. use App\Entity\PostCategory;
  11. use App\Entity\ParametrePost;
  12. use App\Entity\ParamPublicite;
  13. use Symfony\Component\Intl\Locale;
  14. use Symfony\Component\Intl\Languages;
  15. use Twig\Extension\AbstractExtension;
  16. use Doctrine\ORM\EntityManagerInterface;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Templating\EngineInterface;
  19. use MobileDetectBundle\DeviceDetector\MobileDetectorInterface;
  20. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  21. class ImagesExtenstion extends AbstractExtension
  22. {
  23.     protected $em;
  24.     private $theme;
  25.     private $twig;
  26.     private $mobileDetector;
  27.     private $tokenStorage;
  28.     public function __construct(EntityManagerInterface $em,
  29.                                 string $theme,Environment $twig,
  30.                                 MobileDetectorInterface $mobileDetector,
  31.                                 TokenStorageInterface $tokenStorage) {
  32.         $this->em $em;
  33.         $this->theme $theme;
  34.         $this->twig $twig;
  35.         $this->mobileDetector $mobileDetector;
  36.         $this->tokenStorage $tokenStorage;
  37.     }
  38.     public function getFunctions()
  39.     {
  40.         return array(
  41.             new TwigFunction('asset_image', array($this'AssetImageFunction')),
  42.             new TwigFunction('asset_image_gallery', array($this'AssetImageGalleryFunction')),
  43.             new TwigFunction('asset_image_affichage', array($this'AssetImageAffichageFunction')),
  44.         );
  45.     }
  46.     public function AssetImageFunction($filePath,$size 'medium',$array_path = []){
  47.         $userAgent $_SERVER['HTTP_USER_AGENT'];
  48.         $extension_file pathinfo($filePathPATHINFO_EXTENSION);
  49.         if (strpos($userAgent'Safari') !== false && strpos($userAgent'Chrome') === false) {
  50.             $chemin_webp $filePath;
  51.         }else{
  52.             $chemin_webp str_replace('.' $extension_file'.webp'$filePath);
  53.         }
  54.   
  55.         $cdn_path '';
  56.         //$cdn_path = "https://assets.webspirit.ovh";
  57.         if(!(is_array($array_path) and count($array_path) >= 1)){
  58.             if ($this->mobileDetector->isMobile()) {
  59.                 $file str_replace("/files/",$cdn_path."/thumbs/files380/",$chemin_webp);
  60.             }elseif($this->mobileDetector->isTablet()){
  61.                 $file str_replace("/files/",$cdn_path."/thumbs/files800/",$chemin_webp);
  62.             }else{
  63.                 if ($size == 'large') {
  64.                     $file str_replace("/files/",$cdn_path."/thumbs/files1920/",$chemin_webp);
  65.                 }elseif($size == 'medium'){
  66.                     $file str_replace("/files/",$cdn_path."/thumbs/files1024/",$chemin_webp);
  67.                 }elseif($size == 'medium2'){
  68.                     $file str_replace("/files/",$cdn_path."/thumbs/files480/",$chemin_webp);
  69.                 }
  70.             }
  71.         }else{
  72.             $pc_path "files1920";
  73.             $tablet_path "files800";
  74.             $mobile_path "files480";
  75.             if (is_array($array_path)) {
  76.                 if (count($array_path) >= 3) {
  77.                     $pc_path $array_path[0];
  78.                     $tablet_path $array_path[1];
  79.                     $mobile_path $array_path[2];
  80.                 }elseif (count($array_path) == 2) {
  81.                     $pc_path $array_path[0];
  82.                     $tablet_path $array_path[1];
  83.                     $mobile_path $array_path[1];
  84.                 }elseif (count($array_path) == 1) {
  85.                     $pc_path $array_path[0];
  86.                     $tablet_path $array_path[0];
  87.                     $mobile_path $array_path[0];
  88.                 }
  89.             }
  90.             if ($this->mobileDetector->isMobile()) {
  91.                 $file str_replace("/files/",$cdn_path."/thumbs/" $mobile_path ."/",$chemin_webp);
  92.             }elseif($this->mobileDetector->isTablet()){
  93.                 $file str_replace("/files/",$cdn_path."/thumbs/" $tablet_path ."/",$chemin_webp);
  94.             }else{
  95.                 $file str_replace("/files/",$cdn_path."/thumbs/" $pc_path ."/",$chemin_webp);
  96.             }
  97.         }
  98.         
  99.         return $file;
  100.     }
  101.     public function AssetImageGalleryFunction($filePath,$array_path = []){
  102.         $userAgent $_SERVER['HTTP_USER_AGENT'];
  103.         
  104.         $extension_file pathinfo($filePathPATHINFO_EXTENSION);
  105.         if (strpos($userAgent'Safari') !== false && strpos($userAgent'Chrome') === false) {
  106.             $chemin_webp $filePath;
  107.         }else{
  108.             $chemin_webp str_replace('.' $extension_file'.webp'$filePath);
  109.         }
  110.         $cdn_path '';
  111.         //$cdn_path = "https://assets.webspirit.ovh";
  112.         if(!(is_array($array_path) and count($array_path) >= 1)){
  113.             if ($this->mobileDetector->isMobile()) {
  114.                 $file str_replace("storage/galleries/",$cdn_path."/thumbs/files380/",$chemin_webp);
  115.             }elseif($this->mobileDetector->isTablet()){
  116.                 $file str_replace("storage/galleries/",$cdn_path."/thumbs/files800/",$chemin_webp);
  117.             }else{
  118.                 $file str_replace("storage/galleries/",$cdn_path."/thumbs/files1920/",$chemin_webp);
  119.             }
  120.         }else{
  121.             $pc_path "files1920";
  122.             $tablet_path "files800";
  123.             $mobile_path "files480";
  124.             if (is_array($array_path)) {
  125.                 if (count($array_path) >= 3) {
  126.                     $pc_path $array_path[0];
  127.                     $tablet_path $array_path[1];
  128.                     $mobile_path $array_path[2];
  129.                 }elseif (count($array_path) == 2) {
  130.                     $pc_path $array_path[0];
  131.                     $tablet_path $array_path[1];
  132.                     $mobile_path $array_path[1];
  133.                 }elseif (count($array_path) == 1) {
  134.                     $pc_path $array_path[0];
  135.                     $tablet_path $array_path[0];
  136.                     $mobile_path $array_path[0];
  137.                 }
  138.             }
  139.             if ($this->mobileDetector->isMobile()) {
  140.                 $file str_replace("storage/galleries/",$cdn_path."/thumbs/" $mobile_path."/" ,$chemin_webp);
  141.             }elseif($this->mobileDetector->isTablet()){
  142.                 $file str_replace("storage/galleries/",$cdn_path."/thumbs/" $tablet_path."/" ,$chemin_webp);
  143.             }else{
  144.                 $file str_replace("storage/galleries/",$cdn_path."/thumbs/" $pc_path."/" ,$chemin_webp);
  145.             }
  146.         }
  147.         return $file;
  148.     }
  149.     public function AssetImageAffichageFunction($filePath,$type_affichage 'affichage3'){
  150.         $userAgent $_SERVER['HTTP_USER_AGENT'];
  151.         $extension_file pathinfo($filePathPATHINFO_EXTENSION);
  152.         if (strpos($userAgent'Safari') !== false && strpos($userAgent'Chrome') === false) {
  153.             $chemin_webp $filePath;
  154.         }else{
  155.             $chemin_webp str_replace('.' $extension_file'.webp'$filePath);
  156.         }
  157.         $array_path = ['files1024','files800','files480'];
  158.         switch ($type_affichage) {
  159.             case 'affichage1':
  160.                 $array_path = ['files800','files800','files480'];
  161.                 break;
  162.             case 'affichage2':
  163.                 $array_path = ['files1024','files1024','files800'];
  164.                 break;
  165.             case 'affichage3':
  166.                 $array_path = ['files800','files800','files480'];
  167.                 break;
  168.             case 'affichage4':
  169.                 $array_path = ['files1024','files1024','files480'];
  170.                 break;
  171.             case 'affichage5':
  172.                 $array_path = ['files1920','files800','files480'];
  173.                 break;
  174.             case 'affichage6':
  175.                 $array_path = ['files1024','files1024','files480'];
  176.                 break;
  177.             case 'affichage7':
  178.                 $array_path = ['files800','files800','files480'];
  179.                 break;
  180.             case 'affichage8':
  181.                 $array_path = ['files800','files800','files480'];
  182.                 break;
  183.             case 'affichage9':
  184.                 $array_path = ['files800','files800','files480'];
  185.                 break;
  186.             case 'affichage10':
  187.                 $array_path = ['files1920','files1024','files480'];
  188.                 break;
  189.             case 'affichage11':
  190.                 $array_path = ['files800','files800','files480'];
  191.                 break;
  192.             
  193.             default:
  194.                 $array_path = ['files1920','files800','files480'];
  195.                 break;
  196.         }
  197.         $cdn_path '';
  198.         //$cdn_path = "https://assets.webspirit.ovh";
  199.         if (is_array($array_path)) {
  200.             $pc_path $array_path[0];
  201.             $tablet_path $array_path[1];
  202.             $mobile_path $array_path[2];
  203.         }
  204.         if ($this->mobileDetector->isMobile()) {
  205.             $file str_replace("/files/",$cdn_path."/thumbs/" $mobile_path ."/",$chemin_webp);
  206.         }elseif($this->mobileDetector->isTablet()){
  207.             $file str_replace("/files/",$cdn_path."/thumbs/" $tablet_path ."/",$chemin_webp);
  208.         }else{
  209.             $file str_replace("/files/",$cdn_path."/thumbs/" $pc_path ."/",$chemin_webp);
  210.         }
  211.         return $file;
  212.     }
  213.  
  214.     public function getUser()
  215.     {
  216.         return $this->tokenStorage->getToken()->getUser();
  217.     }
  218. }