1212use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
1313use Magento \Framework \Exception \LocalizedException ;
1414use Magento \Store \Api \Data \StoreInterface ;
15+ use Magento \Framework \Filesystem \DirectoryList ;
1516
1617/**
1718 * Resolve category image to a fully qualified URL
1819 */
1920class Image implements ResolverInterface
2021{
22+ /** @var DirectoryList */
23+ private $ directoryList ;
24+
25+ /**
26+ * @param DirectoryList $directoryList
27+ */
28+ public function __construct (DirectoryList $ directoryList )
29+ {
30+ $ this ->directoryList = $ directoryList ;
31+ }
32+
2133 /**
2234 * @inheritdoc
2335 */
@@ -31,14 +43,21 @@ public function resolve(
3143 if (!isset ($ value ['model ' ])) {
3244 throw new LocalizedException (__ ('"model" value should be specified ' ));
3345 }
46+ /** @var \Magento\Catalog\Model\Category $category */
3447 $ category = $ value ['model ' ];
3548 $ imagePath = $ category ->getImage ();
3649 if (empty ($ imagePath )) {
3750 return null ;
3851 }
3952 /** @var StoreInterface $store */
4053 $ store = $ context ->getExtensionAttributes ()->getStore ();
41- $ baseUrl = $ store ->getBaseUrl ();
54+ $ baseUrl = $ store ->getBaseUrl ('media ' );
55+
56+ $ mediaPath = $ this ->directoryList ->getUrlPath ('media ' );
57+ $ pos = strpos ($ imagePath , $ mediaPath );
58+ if ($ pos !== false ) {
59+ $ imagePath = substr ($ imagePath , $ pos + strlen ($ mediaPath ), strlen ($ baseUrl ));
60+ }
4261 $ imageUrl = rtrim ($ baseUrl , '/ ' ) . '/ ' . ltrim ($ imagePath , '/ ' );
4362
4463 return $ imageUrl ;
0 commit comments