Cookies setting

Cookies help us enhance your experience on our site by storing information about your preferences and interactions. You can customize your cookie settings by choosing which cookies to allow. Please note that disabling certain cookies might impact the functionality and features of our services, such as personalized content and suggestions. Cookie Policy

Cookie Policy
Essential cookies

These cookies are strictly necessary for the site to work and may not be disabled.

Information
Always enabled
Advertising cookies

Advertising cookies deliver ads relevant to your interests, limit ad frequency, and measure ad effectiveness.

Information
Analytics cookies

Analytics cookies collect information and report website usage statistics without personally identifying individual visitors to Google.

Information
mageplaza.com

How to upload product image in Magento 2

Vinh Jacker | 12-18-2024

Upload product image

Today’s post will provide a helpful tutorial about uploading product images in Magento 2. It’s easy for developers of any level to follow successfully.

Why product image is important?

Product images play a vital role in marketing your products. Needless to say, visual elements are alway more pleasing to eyes than text. So customers, when they first see your products, will land their eyes on the product images rather than reading product description. That’s the biggest reason why you should not ignore product images.

This directly boost your website’s visual attraction and enhance your customer experience.

Upload product image in Magento 2 via code snippet

Uploading the product image in Magento 2 via the code snippet is the helpful article for the developers. Using the picture is a close way to introduce your products to the customers in the shopping online. As you know, a huge wonder making the buyer consider carefully to buy is what that looks like. The photo with the major details will answer the question as well as doesn’t require the buyers to imagine in their minds.

So what do you need to upload the product image in Magento 2 from controller file? Here are snippet:

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Backend\App\Action;
 
protected $_fileUploaderFactory;
 
public function __construct(
    \Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory,
    Action\Context $context
      
) {
 
    $this->_fileUploaderFactory = $fileUploaderFactory;
    parent::__construct($context);
}
 
public function execute(){
 
    $uploader = $this->_fileUploaderFactory->create(['fileId' => 'image']);
      
    $uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
      
    $uploader->setAllowRenameFiles(false);
      
    $uploader->setFilesDispersion(false);
 
    $path = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)
      
    ->getAbsolutePath('images/');
      
    $uploader->save($path);
 
}

That is it! If that you have any queries about the article or any questions in general, use the comment section below!

Related Topics

x
    Jacker

    With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.



    Related Post

    Website Support
    & Maintenance Services

    Make sure your store is not only in good shape but also thriving with a professional team yet at an affordable price.

    Get Started
    mageplaza services