![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/cartforge.co/vendor/magento/module-catalog/Model/Category/ |
<?php /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ declare(strict_types=1); namespace Magento\Catalog\Model\Category; use Magento\Catalog\Model\Category; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\UrlInterface; use Magento\Store\Model\StoreManagerInterface; /** * Category Image Service */ class Image { private const ATTRIBUTE_NAME = 'image'; /** * @var FileInfo */ private $fileInfo; /** * @var StoreManagerInterface */ private $storeManager; /** * Initialize dependencies. * * @param FileInfo $fileInfo * @param StoreManagerInterface $storeManager */ public function __construct( FileInfo $fileInfo, StoreManagerInterface $storeManager ) { $this->fileInfo = $fileInfo; $this->storeManager = $storeManager; } /** * Resolve category image URL * * @param Category $category * @param string $attributeCode * @return string * @throws LocalizedException */ public function getUrl(Category $category, string $attributeCode = self::ATTRIBUTE_NAME): string { $url = ''; $image = $category->getData($attributeCode); if ($image) { if (is_string($image)) { $store = $this->storeManager->getStore(); $mediaBaseUrl = $store->getBaseUrl(UrlInterface::URL_TYPE_MEDIA); if ($this->fileInfo->isBeginsWithMediaDirectoryPath($image)) { $relativePath = $this->fileInfo->getRelativePathToMediaDirectory($image); $url = rtrim($mediaBaseUrl, '/') . '/' . ltrim($relativePath, '/'); } elseif (substr($image, 0, 1) !== '/') { $url = rtrim($mediaBaseUrl, '/') . '/' . ltrim(FileInfo::ENTITY_MEDIA_PATH, '/') . '/' . $image; } else { $url = $image; } } else { throw new LocalizedException( __('Something went wrong while getting the image url.') ); } } return $url; } }