Magento 2
  • Personalization for Magento 2
  • Installing
  • Disconnecting
  • Uninstalling
  • Getting Started
  • Configuring
  • Hyvä Theme
  • Guides
    • Migrating to Magento2
    • Upgrading to >= 4.0.0
    • Varnish & Full-Page Caches
    • Advanced User Guide
    • Managing Blocks
    • Overriding or Extending Functionalities
      • Overriding Product Data
        • Excluding Products
        • Customising Pricing
        • Customising Categories
        • Resizing Images
      • Overriding Customer Data
    • Console Commands
    • Tagging Providers
  • Features
    • Product data caching
      • Built-in caching
      • Database caching
    • Variations (Configurable Products)
    • Add to Cart
    • Indexer
      • On 7.x
      • On 5.x and 6.x
      • On 4.x
      • On 3.x
    • Customer Group Pricing
    • Supplier Cost & Inventory Level
    • Restore Cart Link
    • Ratings & Reviews
    • Multi Currency (Exchange Rates)
    • Marketing permission and GDPR compatibility
    • Content Staging & Special Prices
  • Integrations
    • Using Yotpo
  • FAQ
  • Developing & Contributing
  • Visit Nosto
  • Issues
  • Releases
  • Addons
    • Multi Source Inventory (MSI)
      • Installing
      • Uninstalling
      • Configuring
    • Category Merchandising (CM 1.0)
      • Installing
      • Uninstalling
      • Configuring
      • Guides
        • Translating Frontend Strings
        • PWA Implementation
        • Using Elasticsearch
        • Segmentation
        • Overriding or Extending Functionalities
          • Customize Query Parameters
          • Customize Page Size Limit
          • Customize Exclude Filters
        • Debugging
Powered by GitBook
On this page

Was this helpful?

  1. Guides
  2. Overriding or Extending Functionalities
  3. Overriding Product Data

Resizing Images

PreviousCustomising CategoriesNextOverriding Customer Data

Last updated 3 years ago

Was this helpful?

In order to use images with custom dimensions, you can use a built-in Magento re-sizer function to generate image URLs with the correct dimensions and you will need to amend the product model.

Start by reading and .

Note: In case you want a square image, only the first parameter is required.

File app/code/My/Nosto/Observer/Product/Load.php

<?php

namespace My\Nosto\Observer\Product;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;

class Load implements ObserverInterface
{
    protected $imageHelper;
    protected $productRepository;

    public function __construct(
        \Magento\Catalog\Helper\Image $imageHelper,
        ProductRepositoryInterface $productRepository
    ) {
        $this->imageHelper = $imageHelper;
        $this->productRepository = $productRepository;
    }

    /**
     * Observer for "nosto_product_load_after" event
     *
     * @param Observer $observer
     * @return void
     * @throws \Magento\Framework\Exception\NoSuchEntityException
     */
    public function execute(Observer $observer)
    {
        /** @var $product \NostoProduct */
        $product = $observer->getProduct();

        $width = 200;
        $height = 300;

        $magentoProduct = $this->productRepository->getById($product->getProductId());
        $resizedImageUrl = $this->imageHelper
            ->init($magentoProduct, 'product_base_image')
            ->constrainOnly(true) // Optional
            ->keepAspectRatio(true) // Optional
            ->keepTransparency(true) // Optional
            ->keepFrame(false) // Optional
            ->resize($width, $height)
            ->getUrl();

        $product->setImageUrl($resizedImageUrl);
    }
}
how to extend Nosto module
how to alter Nosto product data