vendor/gregwar/image-bundle/Services/ImageHandling.php line 88

Open in your IDE?
  1. <?php
  2. namespace Gregwar\ImageBundle\Services;
  3. use Gregwar\ImageBundle\ImageHandler;
  4. use Symfony\Component\Config\FileLocatorInterface;
  5. use Symfony\Component\DependencyInjection\ContainerInterface;
  6. use Symfony\Component\Asset\Packages;
  7. use Symfony\Component\HttpKernel\KernelInterface;
  8. /**
  9.  * Image manipulation service.
  10.  *
  11.  * @author Gregwar <g.passault@gmail.com>
  12.  * @author Sullivan Senechal <soullivaneuh@gmail.com>
  13.  */
  14. class ImageHandling
  15. {
  16.     /**
  17.      * @var string
  18.      */
  19.     private $cacheDirectory;
  20.     /**
  21.      * @var int
  22.      */
  23.     private $cacheDirMode;
  24.     /**
  25.      * @var ContainerInterface
  26.      */
  27.     private $container;
  28.     /**
  29.      * @var Packages
  30.      */
  31.     private $assetsPackages;
  32.     /**
  33.      * @var string
  34.      */
  35.     private $handlerClass;
  36.     /**
  37.      * @var FileLocatorInterface|KernelInterface
  38.      */
  39.     private $fileLocator;
  40.     /**
  41.      * @var bool
  42.      */
  43.     private $throwException;
  44.     /**
  45.      * @param string                               $cacheDirectory
  46.      * @param int                                  $cacheDirMode
  47.      * @param string                               $handlerClass
  48.      * @param ContainerInterface                   $container
  49.      * @param KernelInterface|FileLocatorInterface $fileLocator
  50.      * @param bool                                 $throwException
  51.      * @param string                               $fallbackImage
  52.      */
  53.     public function __construct($cacheDirectory$cacheDirMode$handlerClassContainerInterface $containerPackages $assetsPackages$fileLocator$throwException$fallbackImage)
  54.     {
  55.         if (!$fileLocator instanceof FileLocatorInterface && $fileLocator instanceof KernelInterface) {
  56.             throw new \InvalidArgumentException(
  57.                 'Argument 5 passed to '.__METHOD__.' must be an instance of '.
  58.                 'Symfony\Component\Config\FileLocatorInterface or Symfony\Component\HttpKernel\KernelInterface.'
  59.             );
  60.         }
  61.         if ($fileLocator instanceof KernelInterface) {
  62.             @trigger_error(
  63.                 'Pass Symfony\Component\HttpKernel\KernelInterface to '.__CLASS__.
  64.                 ' is deprecated since version 2.1.0 and will be removed in 3.0.'.
  65.                 ' Use Symfony\Component\Config\FileLocatorInterface instead.',
  66.                 E_USER_DEPRECATED
  67.             );
  68.         }
  69.         $this->cacheDirectory $cacheDirectory;
  70.         $this->cacheDirMode intval($cacheDirMode);
  71.         $this->handlerClass $handlerClass;
  72.         $this->container $container;
  73.         $this->assetsPackages $assetsPackages;
  74.         $this->fileLocator $fileLocator;
  75.         $this->throwException $throwException;
  76.         $this->fallbackImage $fallbackImage;
  77.     }
  78.     /**
  79.      * Get a manipulable image instance.
  80.      *
  81.      * @param string $file the image path
  82.      *
  83.      * @return ImageHandler a manipulable image instance
  84.      */
  85.     public function open($file)
  86.     {
  87.         if (strlen($file) >= && $file[0] == '@') {
  88.             try {
  89.                 if ($this->fileLocator instanceof FileLocatorInterface) {
  90.                     $file $this->fileLocator->locate($file);
  91.                 } else {
  92.                     $this->fileLocator->locateResource($file);
  93.                 }
  94.             } catch (\InvalidArgumentException $exception) {
  95.                 if ($this->throwException || false == $this->fallbackImage) {
  96.                     throw $exception;
  97.                 }
  98.                 $file $this->fallbackImage;
  99.             }
  100.         }
  101.         return $this->createInstance($file);
  102.     }
  103.     /**
  104.      * Get a new image.
  105.      *
  106.      * @param string $w the width
  107.      * @param string $h the height
  108.      *
  109.      * @return ImageHandler a manipulable image instance
  110.      */
  111.     public function create($w$h)
  112.     {
  113.         return $this->createInstance(null$w$h);
  114.     }
  115.     /**
  116.      * Creates an instance defining the cache directory.
  117.      *
  118.      * @param string      $file
  119.      * @param string|null $w
  120.      * @param string|null $h
  121.      *
  122.      * @return ImageHandler
  123.      */
  124.     private function createInstance($file$w null$h null)
  125.     {
  126.         $container $this->container;
  127.         $webDir $container->getParameter('gregwar_image.web_dir');
  128.         $handlerClass $this->handlerClass;
  129.         /** @var ImageHandler $image */
  130.         $image = new $handlerClass($file$w$h$this->throwException$this->fallbackImage);
  131.         $image->setCacheDir($this->cacheDirectory);
  132.         $image->setCacheDirMode($this->cacheDirMode);
  133.         $image->setActualCacheDir($webDir.'/'.$this->cacheDirectory);
  134.         if ($container->has('templating.helper.assets')) {
  135.             $image->setFileCallback(function ($file) use ($container) {
  136.                 return $container->get('templating.helper.assets')->getUrl($file);
  137.             });
  138.         } else {
  139.             $image->setFileCallback(function ($file) use ($container) {
  140.                 return $this->assetsPackages->getUrl($file);
  141.             });
  142.         }
  143.         return $image;
  144.     }
  145. }