Hot answers tagged

47 votes
Accepted

How to get media directory path in a phtml file in magento 2?

Instead of using direct object manager, use It like use Magento\Framework\App\Filesystem\DirectoryList; protected $_filesystem; public function __construct( \Magento\Framework\Filesystem $...
Keyur Shah's user avatar
42 votes
Accepted

Magento2: Get Media URL In Template File (Without direct Calling Object Manager )

You can get media url in your template file using below way but without using objectmanager you must have to define Block file with __construct() method with define storeManagerInterface in construct ...
Rakesh Jesadiya's user avatar
19 votes

Faster way to load media images in a product collection

The accepted answer works, but involves loading all the product attributes into memory when perhaps they are unnecessary. To take a slightly more surgical approach (tested in 1.9.2.2 CE) do $...
Aaron Bonner's user avatar
19 votes

Magento 2.1 Media path in .phtml file

Usage of Object Manager is discouraged as per Magento's coding standards. In a template file, we can get the media URL using the following code: $this->helper('\Magento\Cms\Helper\Wysiwyg\Images')-...
N. Karthic Kannan's user avatar
17 votes

Magento's Secure/Unsecure Image Link - Link Securely for Secure Pages only

Use _secure attribute. Ex: <img src="{{media url="wysiwyg/infortis/ultimo/menu/featured2.jpg" _secure="true" }}" />
Rafael Ortega Bueno's user avatar
15 votes

Magento2: Get Media URL In Template File (Without direct Calling Object Manager )

As of 2.1, there is no direct way of getting the media URL without either: calling the object manager directly (please don't do that) override the block and add a new method Rakesh mentioned one way ...
Raphael at Digital Pianism's user avatar
13 votes
Accepted

Magento 2.1 Media path in .phtml file

you can get media path by $object method $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); echo $objectManager->get('Magento\Store\Model\StoreManagerInterface') ...
Qaisar Satti's user avatar
  • 32.5k
12 votes

How to add images to product gallery programmatically in magento2?

Use Below code to add images,thumbnail Use following code to add/remove image from product in Magento2. // Instance of object manager $objectManager = \Magento\Framework\App\ObjectManager::...
akgola's user avatar
  • 2,917
11 votes

Magento 2 Get all product images in on product list page

Category loading has changed in 2.1, so this may only be relevant from 2.1 onwards: The image gallery is added to the product via an extension interface defined via di.xml. The upshot is that we can ...
Robert Egginton's user avatar
11 votes
Accepted

How to create directory and text file and write content in file in media folder programmatically in Magento 2?

Finally, I got a solution for creating files and contents programmatically in Magento 2. Here is a code <?php try { $om = \Magento\Framework\App\ObjectManager::getInstance(); $...
Niraj Patel's user avatar
10 votes

How to get media directory path in a phtml file in magento 2?

First you will need to inject DirectoryList class into your Magento 2 constructor: public function __construct(\Magento\Framework\View\Element\Template\Context $context, \Magento\Framework\App\...
Mohit Kumar Arora's user avatar
10 votes
Accepted

Magento2 category images not showing and not changing

Go to System > Cache Management and scroll to the bottom of the page. Then click the Flush Catalog Images Cache button and the clear the other caches. Update: After chat, it turned out that there ...
Shawn Abramson's user avatar
8 votes

Magento2: Get Media URL In Template File (Without direct Calling Object Manager )

If you don't want to go to the trouble of extending \Magento\Framework\View\Element\Template, and you want your .phtml file to use the block \Magento\Framework\View\Element\Template, then you can use ...
kig's user avatar
  • 169
8 votes

Magento 2.1 Media path in .phtml file

Get media path by objectManager $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); echo $objectManager->get('Magento\Store\Model\StoreManagerInterface') ->...
Prince Patel's user avatar
  • 22.7k
8 votes

Magento 2 WYSIWYG Media image directive using admin url

This is a known bug which is still present in CE 2.1.5. The known fix is to add 'add_directives' => true to getConfig function ofvendor/magento/module-cms/Model/Wysiwyg/Config.php. The best way ...
Konstantin Gerasimov's user avatar
7 votes

Magento 2 Get Media Gallery in productlist

create helper for example : <?php /** * Copyright © 2015 Magento. All rights reserved. * See COPYING.txt for license details. */ namespace Ibnab\Common\Helper; use Magento\Catalog\Model\Product\...
Ibnab's user avatar
  • 1,861
7 votes

Magento 2 Get all product images in on product list page

Use below code to get all gallery images on product list page: <?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $product = $objectManager->create('Magento\...
Prashant Valanda's user avatar
7 votes

Magento 2 Get all product images in on product list page

There is a function available in magento Magento\Catalog\Model\ResourceModel\Product\Collection::addMediaGalleryData() which will add media gallery images to your product collection. Just use it on ...
Jaimin Sutariya's user avatar
7 votes

How to get media directory path in a phtml file in magento 2?

Try to get it by using StoreManagerInterface use Magento\Store\Model\StoreManagerInterface; protected $storeManager; public function __construct( StoreManagerInterface $storeManager, ) { $...
swathi_sri's user avatar
6 votes

Magento 2 Get all product images in on product list page

create helper for example : <?php /** * Copyright © 2015 Magento. All rights reserved. * See COPYING.txt for license details. */ namespace Ibnab\Common\Helper; use Magento\Catalog\Model\Product\...
Ibnab's user avatar
  • 1,861
6 votes

How to get media directory path in a phtml file in magento 2?

Use below code to get the media path on .phtml file. $this->getUrl('pub/media'); By Objectmanager $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); echo $objectManager->...
rajat kara's user avatar
  • 1,209
5 votes

Faster way to load media images in a product collection

I know the question is old and that it already has a correct answer, but it is not bad to recommend another method that can be useful to the community: Before $_product->getMediaGalleryImages() ...
Hector Andres Rojas's user avatar
5 votes

Magento2: Get Media URL In Template File (Without direct Calling Object Manager )

Edit: Please note that this was a valid and documented answer when first answered, but helpers have since now been deprecated, and so the use of the below is highly discouraged. I'm going to use ...
Smithee's user avatar
  • 319
5 votes
Accepted

Magento 2 getMediaGalleryImages() return null

You can get the product media gallery images as given below. Method 1: Use dependency. (Recommended) Create a helper app/code/Vendor/Module/Helper/Data.php <?php namespace Vendor\Module\...
Dinesh Yadav's user avatar
  • 6,437
5 votes

Magento 2 getMediaGalleryImages() return null

/* Add media gallery data to loaded items * @since 101.0.1 * addMediaGalleryData() */ $product = $objectManager->create('\Magento\Catalog\Model\Product'); $product = $product->getCollection() ...
Andrii K's user avatar
4 votes

Magento2: Get Media URL In Template File (Without direct Calling Object Manager )

At least in 2.2.6, you can use Magento\Framework\UrlInterface::getDirectUrl() protected function buildMediaUrl($path) { return $this->urlBuilder->getDirectUrl( $path, ['_type' =&...
Vadim Kusakin's user avatar
4 votes
Accepted

Magento 2 - get cache url of media gallery images of product

You can get cached images from a product object with this: foreach($product->getMediaGalleryImages() as $k=>$image){ $thumbnailImage = $this->imageHelper->init($product, '...
Shoaib Munir's user avatar
  • 9,394
3 votes
Accepted

Retrieve image url in phtml

Call Function in Block file, Class Test{ protected $storeManager; public function __construct( \Magento\Store\Model\StoreManagerInterface $storeManager, ...
Rakesh Jesadiya's user avatar
3 votes
Accepted

giving 777 permissions for media folder

it's not directly an entry point for attackers, but it can be a security issue when someone already has access to your server. It's pretty common to store information (stolen from the Server) ...
David Verholen's user avatar
3 votes
Accepted

Magento 1 - Storing media files in the database

Magento has explained this in their doc: http://docs.magento.com/m1/ce/user_guide/system-operations/media-storage-database.html The only thing I have noticed is that, magento will need to run a php ...
Nidheesh's user avatar
  • 1,048

Only top scored, non community-wiki answers of a minimum length are eligible