Should I consider an image path with or without the «pub» prefix?

Magento2 provides two index.php in root directories pub and magento root and we can run magento using both index php. I created one module in magento2 and i want to take image path but sometime i am getting image path with pub for some customer and without pub for other based on .htaccess .How can i decide whether i have to consider image path with pub or without pub.

Please publish your problem source code here.

Here is the code
where “/a/b/test.jpg” is the image path from database.if customer added the media url then it is fine.if they dont add media url then i will get path link like this “” but actually customer pointed his site to pub/index.php for security reason so actual path for image is

If you need to get the filesystem path for a media resource, then follow the article How to get an absolute media (image) path programmatically by a relative path.

I just used below code to check whether the customer site pointed to pub/index.php or magento-root/index.php if pub exits in below string then i will take media path with pub in my code otherwise i will remove pub from image path.

$check_root = \Magento\Framework\App\ObjectManager::getInstance()->get(‘Magento\Backend\Block\Page\RequireJs’)->getViewFileUrl(‘requirejs/require.js’);