infoweb\cms\behaviors\ImageBehave::getImage PHP Method

getImage() public method

Returns main model image
public getImage ( boolean $fallbackToPlaceholder = true, mixed $placeHolderPath = null ) : array | null | ActiveRecord
$fallbackToPlaceholder boolean A flag to determine if a placeholder has to be used when no image is found
$placeHolderPath mixed The alternative placeholder path
return array | null | ActiveRecord
    public function getImage($fallbackToPlaceholder = true, $placeHolderPath = null)
    {
        $finder = $this->getImagesFinder();
        $imageQuery = Image::find()->where($finder);
        $imageQuery->orderBy(['isMain' => SORT_DESC, 'id' => SORT_ASC]);
        $img = $imageQuery->one();
        // No image model + fallback to placeholder or
        // image model but image does not exist + fallback to placeholder
        if (!$img && $fallbackToPlaceholder || $img !== null && !file_exists($img->getBaseUrl()) && $fallbackToPlaceholder) {
            // Custom placeholder
            if ($placeHolderPath) {
                $placeHolder = new Image(['filePath' => basename(Yii::getAlias($placeHolderPath)), 'urlAlias' => basename($placeHolderPath)]);
                return $placeHolder;
                // Default placeholder
            } else {
                return new PlaceHolder();
            }
        }
        return $img;
    }