EasyImage::thumbSrcOf PHP 메소드

thumbSrcOf() 공개 메소드

This method returns the URL to the cached thumbnail.
public thumbSrcOf ( string $file, array $params = [], mixed $hash = null ) : string
$file string path
$params array
$hash mixed cache version modifier
리턴 string URL path
    public function thumbSrcOf($file, $params = array(), $hash = null)
    {
        // Paths
        $hash = md5($file . serialize($params) . (string) $hash);
        $cachePath = Yii::getpathOfAlias('webroot') . $this->cachePath . $hash[0];
        $cacheFileExt = isset($params['type']) ? $params['type'] : pathinfo($file, PATHINFO_EXTENSION);
        $cacheFileName = $hash . '.' . $cacheFileExt;
        $cacheFile = $cachePath . DIRECTORY_SEPARATOR . $cacheFileName;
        $webCacheFile = Yii::app()->baseUrl . $this->cachePath . $hash[0] . '/' . $cacheFileName;
        // Return URL to the cache image
        if (file_exists($cacheFile) && time() - filemtime($cacheFile) < $this->cacheTime) {
            return $webCacheFile;
        }
        // Make cache dir
        if (!is_dir($cachePath)) {
            mkdir($cachePath, $this->newDirMode, true);
            chmod(Yii::getpathOfAlias('webroot') . $this->cachePath, $this->newDirMode);
            chmod($cachePath, $this->newDirMode);
        }
        // Create and caching thumbnail use params
        if (!is_file($file)) {
            return false;
        }
        $image = Image::factory($this->detectPath($file), $this->driver, $this->isProgressiveJpeg);
        $originWidth = $image->width;
        $originHeight = $image->height;
        $result = $this->_doThumbOf($image, $cacheFile, $params);
        unset($image);
        // Same for high-resolution image
        if ($this->retinaSupport && $result) {
            if ($this->image()->width * 2 <= $originWidth && $this->image()->height * 2 <= $originHeight) {
                $retinaFile = $cachePath . DIRECTORY_SEPARATOR . $hash . '@2x.' . $cacheFileExt;
                if (isset($params['resize']['width']) && isset($params['resize']['height'])) {
                    $params['resize']['width'] = $this->image()->width * 2;
                    $params['resize']['height'] = $this->image()->height * 2;
                }
                $this->_doThumbOf($file, $retinaFile, $params);
            }
        }
        return $webCacheFile;
    }