Skip to content

Commit

Permalink
Fix for thumnail image sizes.
Browse files Browse the repository at this point in the history
  • Loading branch information
alfredsgenkins committed Jan 6, 2020
1 parent ffe2315 commit b985ad6
Showing 1 changed file with 14 additions and 33 deletions.
47 changes: 14 additions & 33 deletions src/Model/Resolver/Products/DataPostProcessor/Images.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
use Magento\Catalog\Helper\Image;
use Magento\Catalog\Model\Product;
use Magento\Framework\App\Area;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\View\ConfigInterface;
use Magento\Catalog\Model\Product\ImageFactory;
use Magento\Store\Model\App\Emulation;
use Magento\Store\Model\StoreManagerInterface;
Expand All @@ -32,11 +30,6 @@ class Images implements ProductsDataPostProcessorInterface

const IMAGE_FIELDS = ['thumbnail', 'small_image', 'image'];

/**
* @var ConfigInterface
*/
protected $presentationConfig;

/**
* @var Image
*/
Expand All @@ -61,21 +54,18 @@ class Images implements ProductsDataPostProcessorInterface
* Images constructor.
*
* @param ImageFactory $productImageFactory
* @param ConfigInterface $presentationConfig
* @param Image $imageHelper
* @param Emulation $emulation
* @param StoreManagerInterface $storeManager
*/
public function __construct(
ImageFactory $productImageFactory,
ConfigInterface $presentationConfig,
Image $imageHelper,
Emulation $emulation,
StoreManagerInterface $storeManager
) {

$this->imageHelper = $imageHelper;
$this->presentationConfig = $presentationConfig;
$this->productImageFactory = $productImageFactory;
$this->emulation = $emulation;
$this->storeManager = $storeManager;
Expand Down Expand Up @@ -140,7 +130,7 @@ public function process(

$productImages[$id][$imageType] = [
'path' => $imagePath,
'url' => $this->getImageUrl($imageType, $imagePath),
'url' => $this->getImageUrl($imageType, $imagePath, $product),
'label' => $imageLabel ?? $product->getName()
];
}
Expand Down Expand Up @@ -168,40 +158,31 @@ public function process(
/**
* @param string $imageType
* @param string|null $imagePath
* @param Product $product
* @return string
* @throws Exception
*/
protected function getImageUrl(
string $imageType,
?string $imagePath
?string $imagePath,
$product
): string {
if (!isset($imagePath)) {
return $this->imageHelper->getDefaultPlaceholderUrl($imageType);
}

$imageId = sprintf('scandipwa_%s', $imageType);

$viewImageConfig = $this->presentationConfig
->getViewConfig()
->getMediaAttributes(
'Magento_Catalog',
Image::MEDIA_TYPE_CONFIG_NODE,
$imageId
);

$image = $this->productImageFactory->create();

if (isset($viewImageConfig['width'])) {
$image->setWidth((int) $viewImageConfig['width']);
}

if (isset($viewImageConfig['height'])) {
$image->setHeight((int) $viewImageConfig['height']);
}

$image
->setDestinationSubdir($imageType)
->setBaseFile($imagePath);
$image = $this->imageHelper
->init(
$product,
$imageId,
['type' => $imageType]
)
->constrainOnly(true)
->keepAspectRatio(true)
->keepTransparency(true)
->keepFrame(false);

return $image->getUrl();
}
Expand Down

0 comments on commit b985ad6

Please sign in to comment.