/** * @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); }
/** * @param ResourceType $sourceFileResourceType * @param string $sourceFilePath * @param string $sourceFileName * @param int $width * @param int $height * * @return ResizedImage|null */ public function getResizedImageBySize(ResourceType $sourceFileResourceType, $sourceFilePath, $sourceFileName, $width, $height) { $resizedImagesPath = Path::combine($sourceFileResourceType->getDirectory(), $sourceFilePath, ResizedImage::DIR, $sourceFileName); $backend = $sourceFileResourceType->getBackend(); if (!$backend->hasDirectory($resizedImagesPath)) { return null; } $resizedImagesFiles = array_filter($backend->listContents($resizedImagesPath), function ($v) { return isset($v['type']) && $v['type'] === 'file'; }); $thresholdPixels = $this->config->get('images.threshold.pixels'); $thresholdPercent = (double) $this->config->get('images.threshold.percent') / 100; foreach ($resizedImagesFiles as $resizedImage) { $resizedImageSize = ResizedImage::getSizeFromFilename($resizedImage['basename']); $resizedImageWidth = $resizedImageSize['width']; $resizedImageHeight = $resizedImageSize['height']; if ($resizedImageWidth >= $width && ($resizedImageWidth <= $width + $thresholdPixels || $resizedImageWidth <= $width + $width * $thresholdPercent) && $resizedImageHeight >= $height && ($resizedImageHeight <= $height + $thresholdPixels || $resizedImageHeight <= $height + $height * $thresholdPercent)) { $resizedImage = new ResizedImage($this, $sourceFileResourceType, $sourceFilePath, $sourceFileName, $resizedImageWidth, $resizedImageHeight); if ($resizedImage->exists()) { $resizedImage->load(); return $resizedImage; } } } return null; }