Magento Expert Forum - Improve your Magento experience

Results 1 to 8 of 8

How to resize images in Magento 1 & 2?

  1. #1

  2. #2
    Junior Member kanetailor's Avatar
    Join Date
    Apr 2018
    Posts
    69
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Resize images in Magento 1
    <code>
    <img src="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail',<br/> $_image->getFile())->backgroundcolor('000', '000', '000')->resize(100); ?>" ... />
    </code>

    Resize images in Magento 2

    <?php
    /**
    *
    * @var \Magento\Catalog\Model\ProductRepository
    */
    protected $_productRepository;

    /**
    *
    * @var \Magento\Catalog\Helper\Image
    */
    protected $_productImageHelper;

    /**
    * @param \Magento\Backend\Block\Template\Context $context
    * @param \Magento\Catalog\Model\ProductRepository $productRepository
    * @param \Magento\Catalog\Helper\Image $productImageHelper
    * @param array $data
    */
    public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Magento\Catalog\Model\ProductRepository $productRepository,
    \Magento\Catalog\Helper\Image $productImageHelper,
    array $data = []
    )
    {
    $this->_productRepository = $productRepository;
    $this->_productImageHelper = $productImageHelper;
    parent::__construct($context, $data);
    }

    /**
    * resize of the image
    * @see \Magento\Catalog\Model\Product\Image
    * @param int $width
    * @param int $height
    * @return $this
    */
    public function resizeImage($id, $image_type, $width, $height = null)
    {
    $product = $this->_productRepository->getById($id);
    $resizedImage = $this->_productImageHelper->init($product, $image_type)
    ->constrainOnly(TRUE)
    ->keepAspectRatio(TRUE)
    ->keepTransparency(TRUE)
    ->keepFrame(FALSE)
    ->resize($width, $height);
    return $resizedImage;
    }

  3. #3
    Junior Member
    Join Date
    Jul 2018
    Posts
    625
    Thanks
    0
    Thanked 1 Time in 1 Post

  4. #4

  5. #5
    Junior Member
    Join Date
    Sep 2016
    Posts
    228
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    Magento does not have any default resize function for category like product.

    So, you need to use Varien_Image class for this purpose.


    Team Building Activities in Delhi | Corporate Team Building Games | Corporate Team Building Activities
    Last edited by vishnu; 02-05-2019 at 07:11 AM.

  6. #6
    Junior Member
    Join Date
    Jul 2018
    Posts
    366
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I was not knowing this, thanks for sharing

  7. #7
    New member
    Join Date
    Jul 2018
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I followed their steps (y) anf it works
    Thanks for sharing me information <3

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •