src/Service/ImageCacheService.php line 72

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use Gregwar\Image\Image;
  4. use Symfony\Component\HttpFoundation\RequestStack;
  5. use Symfony\Component\HttpKernel\KernelInterface;
  6. class ImageCacheService
  7. {
  8.     private $container;
  9.     public function __construct(KernelInterface $kernel, private readonly RequestStack $requestStack, private $projectRoot)
  10.     {
  11.         $this->container $kernel->getContainer();
  12.     }
  13.     // fetch image or generate new depending on parameter provided
  14.     public function imageCache($pathtofile$filter$width 0$height 0$background 'transparent')
  15.     {
  16.         // echo $this->projectRoot . '/public' . $pathtofile." - ".$filter." W=".$width." H=".$height."<br/>";
  17.         if (== $width || == $height) {
  18.             echo '( Error: width or height not set )';
  19.             return false;
  20.         }
  21.         // echo  getcwd().$this->container->getParameter('gregwar_image.fallback_image')."----";
  22.         $fallback $this->container->getParameter('gregwar_image.fallback_image');
  23.         $pathtofile is_file($this->projectRoot.'/public'.$pathtofile) ? $this->projectRoot.'/public'.$pathtofile $this->projectRoot.'/public'.$fallback;
  24.         $pathtofile str_replace('\\''/', (string) $pathtofile);
  25.         $file explode('/'$pathtofile);
  26.         $filename end($file);
  27.         $folder $file[count($file) - 2];
  28.         $folder .= '/'.$width.'x'.$height;
  29.         $cacheDir $this->projectRoot.'/public/userfiles/image_cache/'.$folder;
  30.         $extArray explode('.'$filename);
  31.         $ext strtolower(end($extArray));
  32.         // default to jpg 80%
  33.         $method 'guess';
  34.         $param 80;
  35.         // Check Browser Supports WebP - Currently Chrome/Edge/Android
  36.         if ($this->isSupported(
  37.             $this->requestStack->getCurrentRequest()->headers->get('User-Agent')
  38.         )) {
  39.             $method 'webp';
  40.             $param 100;
  41.         }
  42.         // if webp isnt supported and the image is png we need to run
  43.         // png function to preserve transparancy
  44.         if ('guess' === $method && 'png' == $ext) {
  45.             $method 'png';
  46.         }
  47.         $generateFile = match (strtolower((string) $filter)) {
  48.             'scaleresize' => Image::open($pathtofile)->scaleResize($width$height)->fixOrientation()
  49.                 ->setCacheDir($cacheDir.'-sr')
  50.                 ->setPrettyName($filenamefalse)->{$method}($param),
  51.             'cropresize' => Image::open($pathtofile)->cropResize($width$height)->fixOrientation()
  52.                 ->setCacheDir($cacheDir.'-cr')
  53.                 ->setPrettyName($filenamefalse)->{$method}($param),
  54.             'zoomcrop' => Image::open($pathtofile)->zoomCrop($width$height)->fixOrientation()
  55.                 ->setCacheDir($cacheDir.'-zc')
  56.                 ->setPrettyName($filenamefalse)->{$method}($param),
  57.             default => Image::open($pathtofile)->scaleResize($width$height)->fixOrientation()
  58.                 ->setCacheDir($cacheDir.'-sr')
  59.                 ->setPrettyName($filenamefalse)->{$method}($param),
  60.         };
  61.         return str_replace(getcwd(), '', (string) $generateFile);
  62.     }
  63.     private function isSupported($agent)
  64.     {
  65.         $known_good = [
  66.             'Chrome',
  67.             'Android',
  68.         ];
  69.         foreach ($known_good as $good) {
  70.             if (str_contains((string) $agent$good)) {
  71.                 return true;
  72.             }
  73.         }
  74.         return false;
  75.     }
  76. }