<?php

namespace common\components;


use artbox\core\components\imagemanager\components\ImageManagerGetPath;

/**
 * Class ImageManager
 * @package common\components
 */
class ImageManager extends ImageManagerGetPath
{
    /**
     * @inheritdoc
     */
    public function getImagePath($ImageManager_id, $width = 400, $height = 400, $thumbnailMode = "outbound")
    {
        //default return
        $return = null;
        $mImageManager = ImageManager::findOne($ImageManager_id);

        //check if not empty
        if ($mImageManager !== null) {
            //set crop mode
            $mode = $thumbnailMode == "outbound" ? "outbound" : "inset";

            $sMediaPath = null;
            if ($this->mediaPath !== null) {
                $sMediaPath = $this->mediaPath;
            }

            $sFileExtension = pathinfo($mImageManager->fileName, PATHINFO_EXTENSION);
            //get image file path
            $sImageFilePath = $sMediaPath . '/' . $mImageManager->id . '_' . $mImageManager->fileHash . '.' . $sFileExtension;
            //check file exists
            if (file_exists($sImageFilePath)) {
                $return = \Yii::$app->imageresize->getUrl(
                    $sImageFilePath,
                    $width,
                    $height,
                    $mode,
                    100,
                    $mImageManager->fileName
                );
            } else {
                $return = null; //isset(\Yii::$app->controller->module->assetPublishedUrl) ? \Yii::$app->controller->module->assetPublishedUrl. "/img/img_no-image.png" : null;
            }
        }
        return $return;
    }
}