Skip to content
Peter Jaap Blaakmeer edited this page Nov 19, 2020 · 4 revisions

Welcome to the magento2-imgix wiki!

Use a ViewModel to use Imgix on any image you want.

Be sure to add a Web proxy source in Imgix and configure your secure sign key.

<?php

namespace Vendor\ImgixExtended\ViewModel;

use Magento\Framework\View\Element\Block\ArgumentInterface;

class Image implements ArgumentInterface
{
    /**
     * @var \Elgentos\Imgix\Model\Image
     */
    protected $image;

    public function __construct(\Elgentos\Imgix\Model\Image $image)
    {
        $this->image = $image;
    }

    public function getSignedUrl($url, $params = [])
    {
        return $this->image->getSignedUrl($url, $params);
    }
}

Now inject it into your block;

<block class="\Magento\Framework\View\Element\Template" template="template.phtml">
    <arguments>
        <argument name="imgix" xsi:type="object">\Elgentos\PrismicImgix\ViewModel\Image</argument>
    </arguments>
</block>

And use it in your template;

<?php declare(strict_types=1);
/** @var $block \Magento\Framework\View\Element\Temp */
$imgix = $block->getImgix();
?>
<div>
    <img src="<?=$imgix->getSignedUrl($yourentity->getImageUrl(), ['w' => 500, 'h' => 500, 'auto' => 'compress']); ?>" />
</div>
Clone this wiki locally