src/Service/Channel/Image/ImageUrlGenerator.php line 69

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the nellapp package.
  4.  *
  5.  * (c) Benjamin Georgeault <https://www.drosalys-web.fr/>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace App\Service\Channel\Image;
  11. use App\Entity\Channel\Image;
  12. use Liip\ImagineBundle\Imagine\Cache\CacheManager;
  13. use Symfony\Component\Asset\Packages;
  14. use Symfony\Component\HttpFoundation\UrlHelper;
  15. /**
  16.  * Class ImageUrlGenerator
  17.  *
  18.  * @author Benjamin Georgeault
  19.  */
  20. class ImageUrlGenerator
  21. {
  22.     /**
  23.      * @var Packages
  24.      */
  25.     private $packages;
  26.     /**
  27.      * @var UrlHelper
  28.      */
  29.     private $urlHelper;
  30.     /**
  31.      * @var CacheManager
  32.      */
  33.     private $cache;
  34.     /**
  35.      * ImageUrlGenerator constructor.
  36.      * @param Packages $packages
  37.      * @param UrlHelper $urlHelper
  38.      * @param CacheManager $cache
  39.      */
  40.     public function __construct(Packages $packagesUrlHelper $urlHelperCacheManager $cache)
  41.     {
  42.         $this->packages $packages;
  43.         $this->urlHelper $urlHelper;
  44.         $this->cache $cache;
  45.     }
  46.     /**
  47.      * @param Image|null $image
  48.      * @param string|null $default
  49.      * @param string $filter
  50.      * @return string
  51.      */
  52.     public function getImageUrl(?Image $image nullstring $default nullstring $filter 'card_thumbnail'): string
  53.     {
  54.         if (null === $image) {
  55.             if (null === $default) {
  56.                 $path $this->packages->getUrl('build/common/default.jpg''common');
  57.             } else {
  58.                 $path $default;
  59.             }
  60.         } else {
  61.             return $this->cache->getBrowserPath(parse_url(
  62.                 'uploads/channel_images/'.$image->getName().'.'.$image->getExtension(),
  63.                 PHP_URL_PATH
  64.             ), $filter);
  65.         }
  66.         return $this->urlHelper->getAbsoluteUrl($path);
  67.     }
  68.     public function getImageOriginalUrl(?Image $image null): string
  69.     {
  70.         return parse_url('/uploads/channel_images/'.$image->getName().'.'.$image->getExtension(), PHP_URL_PATH);
  71.     }
  72. }