How to Find the Magento Image Path URL – Developer’s Guide

Posted on:
Developer Guides
How to Find the Magento Image Path URL – Developer’s Guide

In this developer support guide we look at category image paths in Magento 2. There appears to be either a bug or an oddity in the way Magento 2 stores images against categories. There are 3 types of image path that may be returned from a category. These are:

1. The image name

This does not include path information and typically will be from attributes added in third party modules. The full base path and image path needs to be prepended to get the full path of the image.

2. The image path from ‘/media’

This includes the relative path from the media folder and does not include ‘pub’.

3. The image path from ‘/pub’

This includes the full relative path from the root directory.

Magento’s core code checks for whether its path starts with a slash to determine what path modifications need to be made to get the full image path. 

The Fix

Here’s how to account for category image path behaviour in Magento 2:

There you have it, a useful helper to manage category image paths Magento 2.

Our Developer Guides have handy tips to help developers. We’ll cover everything from simple life hacks to technical solutions and everything in between. Keep an eye out for more useful tips in our Magento 2 developer support guides. 

Want to work with our team? Find out more about careers at Aware Digital.

Don't be shy. Let's talk.

Start your journey towards a better website. Our expert staff are happy to talk and answer your questions.