<?php
/******************************************************************************
 *   ______                                                                   *
 *  /       \                                                                 *
 * /$$$$$$  | ________  __    __   ______    ______                           *
 * $$ |__$$ |/        |/  |  /  | /      \  /      \                          *
 * $$    $$ |$$$$$$$$/ $$ |  $$ |/$$$$$$  |/$$$$$$  |                         *
 * $$$$$$$$ |  /  $$/  $$ |  $$ |$$ |  $$/ $$    $$ |                         *
 * $$ |  $$ | /$$$$/__ $$ \__$$ |$$ |      $$$$$$$$/                          *
 * $$ |  $$ |/$$      |$$    $$/ $$ |      $$       |                         *
 * $$/   $$/ $$$$$$$$/  $$$$$$/  $$/        $$$$$$$/                          *
 *                                                                            *
 * @PROJECT    : 3D Preview Extension [Magetop.com]                           *
 * @AUTHOR     : Azure - Developer                                            *
 * @COPYRIGHT  : © 2019  Magetop.com                                          *
 * @LINK       : https://Magetop.com                                          *
 * @CREATED    :  10/01/2022                                                  *
 ******************************************************************************/

namespace Magetop\Advancepreview\Model;

use Magento\Framework\UrlInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\Filesystem;
use Magento\Framework\Image\AdapterFactory;
use Magento\Framework\App\Filesystem\DirectoryList;
class AdvancePreviewFile
{
    /**
     * media sub folder
     * @var string
     */
    protected $advanceMediaDir = 'advancepreview/';

    /**
     * url builder
     *
     * @var \Magento\Framework\UrlInterface
     */
    protected $urlBuilder;
    /**
     * url builder
     *
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $_storeManager;
    /**
     * @var \Magento\Framework\Filesystem
     */
    protected $fileSystem;
    /**
     * @var \Magento\Framework\Image\AdapterFactory
     */
    protected $_imageFactory;
    /**
     * @param UrlInterface $urlBuilder
     * @param Filesystem $fileSystem
     */
    public function __construct(
        UrlInterface $urlBuilder,
        StoreManagerInterface $storeManager,
        Filesystem $fileSystem,
        AdapterFactory $imageFactory
    )
    {
        $this->urlBuilder = $urlBuilder;
        $this->fileSystem = $fileSystem;
        $this->_storeManager = $storeManager;
        $this->_imageFactory = $imageFactory;
    }

    /**
     * @param string $subUrl
     * @return string
     */
    public function getBaseUrl($subUrl = '')
    {
        return $this->urlBuilder->getBaseUrl(['_type' => UrlInterface::URL_TYPE_MEDIA]).$this->advanceMediaDir . $subUrl;
    }

    /**
     * @param string $subFolder
     * @return string
     * @throws \Magento\Framework\Exception\FileSystemException
     */
    public function getBaseDir($subFolder = '')
    {
        return $this->fileSystem->getDirectoryWrite(DirectoryList::MEDIA)->getAbsolutePath($this->advanceMediaDir .$subFolder);
    }
    public function resizeImage($image,$subFolder,$width,$height)
    {
        $absPath = $this->fileSystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath($this->advanceMediaDir .$subFolder).$image;
        $imageResized = $this->fileSystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath($this->advanceMediaDir .$subFolder.'resize/').$width.'x'.$height.'/'.$image;
        $imageResize = $this->_imageFactory->create();
        $imageResize->open($absPath);
        $imageResize->constrainOnly(TRUE);
        $imageResize->keepAspectRatio(TRUE);
        $imageResize->keepFrame(true);
        $imageResize->keepTransparency(TRUE);
        $imageResize->backgroundColor(array(255,255,255));
        $imageResize->resize($width,$height);
        $dest = $imageResized ;
        $imageResize->save($dest);
        $resizeURL = $this->_storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_MEDIA).$this->advanceMediaDir .'resize/'.$width.'x'.$height.'/'.$image;
        return $resizeURL;
    }
    public function resizeImagePro($image,$subFolder,$width,$height, $isNew = true, $isResideFolder = true, $keepRatio = TRUE, $keepFrame = TRUE, $keepTrans = TRUE)
    {
        $absPath = $this->fileSystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath($this->advanceMediaDir .$subFolder).$image;
        if($isResideFolder) {
            $imageResized = $this->fileSystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath($this->advanceMediaDir .$subFolder.'resize/').$width.'x'.$height.'/'.$image;
        }
        else {
            $arImages = explode('.',$image);
            if(!$isNew && count($arImages)) {
                $ext = $arImages[count($arImages) - 1];
                $image = 'image-'.time().'.'.$ext;
            }
            $imageResized = $this->fileSystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath($this->advanceMediaDir .$subFolder).$image;
        }
        try {
            $imageResize = $this->_imageFactory->create();
            $imageResize->open($absPath);
            $imageResize->constrainOnly(TRUE);
            $imageResize->keepAspectRatio($keepRatio);
            $imageResize->keepFrame($keepFrame);
            $imageResize->keepTransparency($keepTrans);
            $imageResize->backgroundColor(array(255,255,255));
            $imageResize->resize($width,$height);
            $dest = $imageResized ;
            $imageResize->save($dest);
            if(!$isNew) {
                unlink($absPath);
            }
            return $image;
        }
        catch (\Exception $exception) {

        }
        return  '';
    }
    function getAdvanceMediaFolder() {
        return $this->advanceMediaDir ;
    }
}
