Exemplo n.º 1
0
 /**
  * @param ResizedImageRepository $resizedImageRepository
  * @param ResourceType           $sourceFileResourceType
  * @param string                 $sourceFileDir
  * @param string                 $sourceFileName
  * @param int                    $requestedWidth
  * @param int                    $requestedHeight
  *
  * @throws \Exception if source image is invalid
  */
 public function __construct(ResizedImageRepository $resizedImageRepository, ResourceType $sourceFileResourceType, $sourceFileDir, $sourceFileName, $requestedWidth, $requestedHeight)
 {
     parent::__construct($sourceFileResourceType, $sourceFileDir, $sourceFileName, $requestedWidth, $requestedHeight);
     $this->resizedImageRepository = $resizedImageRepository;
     $backend = $this->backend = $sourceFileResourceType->getBackend();
     // Check if there's info about source image in cache
     $app = $this->resizedImageRepository->getContainer();
     $cacheKey = Path::combine($sourceFileResourceType->getName(), $sourceFileDir, $sourceFileName);
     $cachedInfo = $app['cache']->get($cacheKey);
     // No info cached, get original image
     if (null === $cachedInfo || !isset($cachedInfo['width']) || !isset($cachedInfo['height'])) {
         $sourceFilePath = Path::combine($sourceFileResourceType->getDirectory(), $sourceFileDir, $sourceFileName);
         if ($backend->isHiddenFile($sourceFileName) || !$backend->has($sourceFilePath)) {
             throw new FileNotFoundException('ResizedImage::create(): Source file not found');
         }
         $originalImage = $this->image = Image::create($backend->read($sourceFilePath));
         $app['cache']->set($cacheKey, $originalImage->getInfo());
         $originalImageWidth = $originalImage->getWidth();
         $originalImageHeight = $originalImage->getHeight();
     } else {
         $originalImageWidth = $cachedInfo['width'];
         $originalImageHeight = $cachedInfo['height'];
     }
     $targetSize = Image::calculateAspectRatio($requestedWidth, $requestedHeight, $originalImageWidth, $originalImageHeight);
     if ($targetSize['width'] >= $originalImageWidth || $targetSize['height'] >= $originalImageHeight) {
         $this->width = $originalImageWidth;
         $this->height = $originalImageHeight;
         $this->requestedSizeIsValid = false;
     } else {
         $this->width = $targetSize['width'];
         $this->height = $targetSize['height'];
     }
     $this->resizedImageFileName = static::createFilename($sourceFileName, $this->width, $this->height);
 }
Exemplo n.º 2
0
 /**
  * @param ThumbnailRepository $thumbnailRepository
  * @param ResourceType        $sourceFileResourceType
  * @param string              $sourceFileDir
  * @param string              $sourceFileName
  * @param int                 $requestedWidth
  * @param int                 $requestedHeight
  */
 public function __construct(ThumbnailRepository $thumbnailRepository, ResourceType $sourceFileResourceType, $sourceFileDir, $sourceFileName, $requestedWidth, $requestedHeight)
 {
     parent::__construct($sourceFileResourceType, $sourceFileDir, $sourceFileName, $requestedWidth, $requestedHeight);
     $this->thumbnailRepository = $thumbnailRepository;
     $this->adjustDimensions();
     $this->backend = $thumbnailRepository->getThumbnailBackend();
     $width = $this->adjustedSizeInfo['width'];
     $height = $this->adjustedSizeInfo['height'];
     $this->resizedImageFileName = ResizedImage::createFilename($sourceFileName, $width, $height);
 }