src/Twig/ImageRenderExtension.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Twig;
  3. use Twig\Extension\AbstractExtension;
  4. use Twig\TwigFunction;
  5. class ImageRenderExtension extends AbstractExtension
  6. {
  7.     public function getFunctions()
  8.     {
  9.         return [
  10.             new TwigFunction('responsive_src', [$this'generateSrc']),
  11.             new TwigFunction('responsive_background', [$this'generateBackground']),
  12.             new TwigFunction('get_assets', [$this'getFiles']),
  13.         ];
  14.     }
  15.     public static function acceptWebp(): bool
  16.     {
  17.         return (bool)strstr($_SERVER['HTTP_ACCEPT'], 'image/webp');
  18.     }
  19.     public function generateSrc(string $imagePath)
  20.     {
  21.         [$fileName$extension$basePath] = $this->extractImageParameters($imagePath);
  22.         if (file_exists(".$basePath/$fileName/3x.$extension")) {
  23.             [$s$m$l] = $this->getFileSizes($imagePath);
  24.             $fileName str_replace(' ''%20'$fileName);
  25.                 return "
  26.             src='$basePath/$fileName/3x.$extension
  27.             srcset='$basePath/$fileName/1x.$extension ${s}w,$basePath/$fileName/2x.$extension ${m}w,$basePath/$fileName/3x.$extension ${l}w' 
  28.             sizes='100vw'
  29.         ";
  30.         }
  31.         return "src='$imagePath'";
  32.     }
  33.     private function getFileSizes($imagePath): array
  34.     {
  35.         [$fileName$extension$basePath] = $this->extractImageParameters($imagePath);
  36.         if(file_exists(".$imagePath")){
  37.             return [
  38.                 getimagesize(".$basePath/$fileName/1x.$extension")[0],
  39.                 getimagesize(".$basePath/$fileName/2x.$extension")[0],
  40.                 getimagesize(".$basePath/$fileName/3x.$extension")[0]
  41.             ];
  42.         }
  43.         else{
  44.             return [0,0,0];
  45.         }
  46.     }
  47.     public function generateBackground(string $imagePath,string $additionnalStyle '',bool $addGradient false)
  48.     {
  49.         [$fileName$extension$basePath] = $this->extractImageParameters($imagePath);
  50.         if (file_exists(".$basePath/$fileName/3x.$extension")) {
  51.             [$s$m$l] = $this->getFileSizes($imagePath);
  52.             $addGradient $addGradient '' '';
  53.             $fileName str_replace(' ''%20'$fileName);
  54.                 return "data-custom-bg-image=true style='
  55.             background-image: $addGradient url(\"$basePath/$fileName/3x.$extension\");
  56.             background-image : $addGradient -webkit-image-set(
  57.                     url(\"$basePath/$fileName/1x.$extension\") 1x, 
  58.                     url(\"$basePath/$fileName/2x.$extension\") 2x, 
  59.                     url(\"$basePath/$fileName/3x.$extension\") 3x); 
  60.             background-image : $addGradient image-set(
  61.                     url(\"$basePath/$fileName/1x.$extension\") 1x,
  62.                     url(\"$basePath/$fileName/2x.$extension\") 2x,
  63.                     url(\"$basePath/$fileName/3x.$extension\") 3x);
  64.             background-image : $addGradient image-set(
  65.                     url(\"$basePath/$fileName/1x.$extension\") ${s}w,
  66.                     url(\"$basePath/$fileName/2x.$extension\") ${m}w,
  67.                     url(\"$basePath/$fileName/3x.$extension\") ${l}w);
  68.             $additionnalStyle'";
  69.         }
  70.         return "data-bg-image='$imagePath' style='$additionnalStyle'";
  71.     }
  72.     public function getFiles(string $imagePath)
  73.     {
  74.         [$fileName$extension$basePath] = $this->extractImageParameters($imagePath);
  75.         return [
  76.                 '1x' => "$basePath/$fileName/1x.$extension",
  77.                 '2x' => "$basePath/$fileName/2x.$extension",
  78.                 '3x' => "$basePath/$fileName/3x.$extension",
  79.         ];
  80.     }/**
  81.  * @param string $imagePath
  82.  * @return array
  83.  */
  84.     protected function extractImageParameters(string $imagePath): array
  85.     {
  86. //        dump("nom de l'image : $imagePath");
  87.         $fileName pathinfo($imagePathPATHINFO_FILENAME);
  88.         $extension self::acceptWebp() ? 'webp' pathinfo($imagePathPATHINFO_EXTENSION);
  89.         $basePath pathinfo($imagePathPATHINFO_DIRNAME);
  90.         $basePath str_replace('/images''/image_build/images'$basePath);
  91.         $basePath str_replace('/websiteImage''/image_build/websiteImage'$basePath);
  92.         return [$fileName$extension$basePath];
  93.     }
  94. }