# Resizing Images

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 [how to extend Nosto module](/magento-2/guides/overriding-or-extending-functionalities.md) and [how to alter Nosto product data](/magento-2/guides/overriding-or-extending-functionalities/overriding-product-data.md).

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

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

```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);
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.nosto.com/magento-2/guides/overriding-or-extending-functionalities/overriding-product-data/resizing-images.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
