Magento Expert Forum - Improve your Magento experience

Results 1 to 7 of 7

How to resize images in Magento 1 & 2?

  1. #1

  2. #2
    Junior Member kanetailor's Avatar
    Join Date
    Apr 2018
    Posts
    69
    Thanks
    1
    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
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

  4. #4

  5. #5
    Junior Member
    Join Date
    Sep 2016
    Posts
    181
    Thanks
    0
    Thanked 2 Times in 2 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.


    Cafeteria Chairs | Student Writing Chairs | Revolving Office Chairs

  6. #6

  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
  •