0

I have created custom module for get category's products which are assigned to particular category like "Featured Product". Feature product is a category and it has a many products. i got that array by developing custom module but i don't know how to get resized image for that product.

please help me Thank you.

1 Answer 1

1

You can use the below function to resize the product base image and get resized image URL.

public function __construct(
    ..
    \Magento\Catalog\Block\Product\Context $productContext,
    ...
) {    
    ...
    $this->productImageHelper = $productContext->getImageHelper();
    ...
}

function getImageUrl($productObject)
{
     $width = 180;
     $height = 180;

     $resizedImage = $this->productImageHelper
        ->init($productObject, 'product_base_image')
        ->constrainOnly(TRUE)
        ->keepAspectRatio(TRUE)
        ->keepTransparency(TRUE)
        ->keepFrame(TRUE)
        ->resize($width, $height)
        ->getUrl();

      return $resizedImage;
}
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.