Advertisements

How to add a class in body tag

Today we will create function which will resize image with requested width and height.

<?php
    /**
     * Name of folder where all resized image get Store
     */
    const XML_PATH_RESIZED_DIR = "resized/";
.....
    /**
     * @var Filesystem
     */
    protected $_filesystem;

    /**
     * @var AdapterFactory
     */
    protected $_imageFactory;
.....

      /**
     * Constructor
     * ...
     * @param \Magento\Framework\Filesystem $filesystem
     * @param \Magento\Framework\Image\AdapterFactory $imageFactory
     * ...
     */
    public function __construct(
        ...
        \Magento\Framework\Filesystem $filesystem,
        \Magento\Framework\Image\AdapterFactory $imageFactory,
        ...
    ) {
        ...
        $this->_filesystem = $filesystem;
        $this->_imageFactory = $imageFactory;
        ...
    }

.....
    /**
     * Image Optimization
     *
     * @param String $image
     * @param Int $width
     * @param Int $height
     * @return void
     */
    public function resize($image, $width = null, $height = null)
    {
        if ($width == null && $height == null) {
            return $image;
        }

        $absolutePath = $this->_filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)->getAbsolutePath() . $image;
        if (!file_exists($absolutePath)) {
            return false;
        }

        $imageResized = $this->_filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)->getAbsolutePath(self::XML_PATH_RESIZED_DIR . '/' . $width . '/') . $image;
        if (!file_exists($imageResized)) { // Only resize image if not already exists.
            //create image factory...
            $imageResize = $this->_imageFactory->create();
            $imageResize->open($absolutePath);
            $imageResize->constrainOnly(true);
            $imageResize->keepTransparency(true);
            $imageResize->keepFrame(false);
            $imageResize->keepAspectRatio(true);
            $imageResize->resize($width, $height);
            //destination folder
            $destination = $imageResized;
            //save image
            $imageResize->save($destination);
        }
        $resizedURL = $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . self::XML_PATH_RESIZED_DIR . '/' . $width . '/' . $image;
        return $resizedURL;
    }

You all are the semicolon to my statements; Please support me; Thanks