2
<?php

namespace Vendor\Module\Controller\Result;

use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\Eav\Model\Config;
use Magento\Catalog\Model\ProductFactory;
use Magento\Catalog\Helper\Product;
use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Catalog\Model\Product\Visibility;

class Result extends \Magento\Framework\App\Action\Action {

    /**
     * @var Magento\Framework\View\Result\PageFactory
     */
    protected $resultPageFactory;
    protected $resultJsonFactory;

    /**
     * @param Context     $context
     * @param PageFactory $resultPageFactory
     */
    public function __construct(Context $context, PageFactory $resultPageFactory, JsonFactory $resultJsonFactory, 
                                    Collection $productCollection, Config $eavConfig, ProductFactory $productFactory, Product $productHelper,
                                    Status $productStatus, Visibility $productVisibility
    ) {

        $this->resultPageFactory = $resultPageFactory;
        $this->resultJsonFactory = $resultJsonFactory;

        $this->productCollection = $productCollection;
        $this->eavConfig = $eavConfig;
        $this->productFactory = $productFactory;
        $this->productHelper = $productHelper;
        $this->productStatus = $productStatus;
        $this->productVisibility = $productVisibility;
        return parent::__construct($context);
    }

    public function execute() {


            $productModel = $this->productFactory->create();
            $module_products = array();

            $this->productCollection->addAttributeToFilter(
                'status', ['in' => $this->productStatus->getVisibleStatusIds()]
            );
            $this->productCollection->setVisibility($this->productVisibility->getVisibleInSiteIds());

            foreach ($this->productCollection as $product) {

                $productId = $product->getId();
                $product = $productModel->load($productId);
                $thumbnailImageUrl = $this->productHelper->getThumbnailUrl($product);

                $my_product[$productId]['name'] = $product->getName();
                $my_product[$productId]['thumbnail'] = $thumbnailImageUrl;
            }

            if(isset($currentId)){
                $module_products[$currentId] = $my_product[$currentId];
            } else{
                $module_products = $my_product;
            }



        $result = $this->resultJsonFactory->create();
        $resultPage = $this->resultPageFactory->create();

        $template = 'Vendor_Module::result.phtml';


        $block = $resultPage->getLayout()
                ->createBlock('Vendor\Module\Block\Index')
                ->setTemplate($template)
                ->setData('module_products', $module_products)
                ->toHtml();

        $result->setData(['output' => $block]);
        return $result;
    }

}

Here method getThumbnail gives product name but not with complete path. Under module-catalog/Helper/Product.php - I have used the method getThumbnailUrl to display the image, but it is thowing 500 Internal Server exception

How do I get product thumbnail image with URL in order to display it ?

3 Answers 3

3

this code working for me also optimize that too

public function execute() {


        $imagewidth=50;
        $imageheight=50;
        $this->productCollection->addAttributeToSelect(array('*'));
        foreach ($this->productCollection as $product) {

             $productId = $product->getId();
             $productSku = $product->getSku();

            echo $thumbnailImageUrl = $this->productHelper->getThumbnailUrl($product);

        }

    }

output

http://localhost/magento22/pub/media/catalog/product//m/b/mb01-blue-0.jpg
http://localhost/magento22/pub/media/catalog/product//m/b/mb04-black-0.jpg
http://localhost/magento22/pub/media/catalog/product//m/b/mb03-black-0.jpg

Code i tried

<?php

namespace QaisarSatti\HelloWorld\Controller\Index;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\Catalog\Model\ProductFactory;
use Magento\Catalog\Helper\Product;
class Index extends \Magento\Framework\App\Action\Action

{

    public function __construct(Context $context,
                            PageFactory $resultPageFactory,
                            JsonFactory $resultJsonFactory,
                            Collection $productCollection,
                            ProductFactory $productFactory,
                            Product $productHelper
    ) {

        $this->resultPageFactory = $resultPageFactory;
        $this->resultJsonFactory = $resultJsonFactory;
        $this->productCollection = $productCollection;
        $this->productFactory = $productFactory;
        $this->productHelper = $productHelper;
        return parent::__construct($context);
    }


    public function execute() {


        $imagewidth=50;
        $imageheight=50;
        $this->productCollection->addAttributeToSelect(array('*'));
        foreach ($this->productCollection as $product) {

             $productId = $product->getId();
             $productSku = $product->getSku();

            echo $thumbnailImageUrl = $this->productHelper->getThumbnailUrl($product).'<br />';

            $productThumbnail = $thumbnailImageUrl ;
        }

    }



}
9
  • Thanks for responding, but I am still unable to get output and its throwing 500 exception. Can you once try with my code which may expose why I am getting error Commented Jan 31, 2018 at 10:50
  • @Vikram added the code which i tried i found no error make sure you set developer mode and remove generation and cache folder too Commented Jan 31, 2018 at 10:54
  • Well I am loading $product = $productModel->load($productModel->getIdBySku($productSku));. Could this be an issue ? Commented Jan 31, 2018 at 10:57
  • @Vikram no need of loading that use this code it fulfill your requirement Commented Jan 31, 2018 at 10:58
  • I figured out that when I include ` Product $productHelper` and $this->productHelper = $productHelper; in __construct functions, it throws exception 500 Internal Server error Commented Jan 31, 2018 at 11:18
2

Please use code like below.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$imageHelper  = $objectManager->get('\Magento\Catalog\Helper\Image');
$width = 100;
$height = 100;
$imageUrl = $imageHelper->init($_product, 'product_page_image_small')
->constrainOnly(FALSE)
->keepAspectRatio(TRUE)
->keepFrame(FALSE)
->resize($width, $height)
->getUrl();
2
  • I discourage direct use of objectManager . Can you post your answer wrt to my code ? Commented Jan 31, 2018 at 9:09
  • then just add dependency of "Magento\Catalog\Helper\Image" in your Controller class then use like $this->imageHelper Commented Jan 31, 2018 at 10:04
0

Please use code like below:

<?php

namespace Vendor\Module\Controller\Result;

use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\Catalog\Model\ProductFactory;
use Magento\Catalog\Helper\Image as ImageHelper;

class Result extends \Magento\Framework\App\Action\Action {


protected imageHelper; 

public function __construct(Context $context, 
                            PageFactory $resultPageFactory, 
                            JsonFactory $resultJsonFactory, 
                            Collection $productCollection, 
                            ProductFactory $productFactory,
                            ImageHelper $imageHelper
    ) {

        $this->resultPageFactory = $resultPageFactory;
        $this->resultJsonFactory = $resultJsonFactory;
        $this->productCollection = $productCollection;
        $this->productFactory = $productFactory;
        $this->imageHelper = $imageHelper;
        return parent::__construct($context);
    }


    public function execute() {

        $productModel = $this->productFactory->create();
        $width = 100;
        $height = 100;


        foreach ($this->productCollection as $product) {

            $productId = $product->getId();
            $productSku = $product->getSku();
            $product = $productModel->load($productModel->getIdBySku($productSku));

            $imageUrl = $this->imageHelper->init($product, 'product_page_image_small')
                            ->constrainOnly(FALSE)
                            ->keepAspectRatio(TRUE)
                            ->keepFrame(FALSE)
                            ->resize($width, $height)
                            ->getUrl();
        }

    }

}
4
  • I have revised my question with ImageHelper. It gives 500 internal server error. Commented Jan 31, 2018 at 10:16
  • Check my code i have used like $imageUrl = $this->imageHelper->init($product, 'product_page_image_small') ->constrainOnly(FALSE) ->keepAspectRatio(TRUE) ->keepFrame(FALSE) ->resize($width, $height) ->getUrl(); Commented Jan 31, 2018 at 10:19
  • Did you check my question with getThumbnailUrl method using Magento\Catalog\Helper\Product instead of Magento\Catalog\Helper\Image ? Commented Jan 31, 2018 at 10:35
  • getThumbnailUrl function return "" in Magento\Catalog\Helper\Product Commented Jan 31, 2018 at 11:07

Your Answer

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

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