How product images are saved in database Magento 2?

A Product is the heart of any e-Commerce system, A product contains multiple information like Title, SKU, Price, Inventory, Images and so more details.

Let’s start with the Main point, A product has multiple images, and I will just explore how each product is stored in the database. Your confusion will be clear after reading the below details of the image saved in the database.

catalog_product_entity_varchar  used for store product image value.

Look at Example, Suppose a Product has a single image with a role like base, thumbnail, and small_image, All the value of the images is stored under the catalog_product_entity_varchar table with attribute_id and value field separately.

If the product image value is abc.jpg then the value is stored as catalog_product_entity_varchar the table looks like,

base thumbnail small image magento 2

In the above image with the attribute_id column,
87 is attribute_id for a base,
88 is attribute_id for the thumbnail and
89 is for small_image attribute_id and product id is 2.

You can check each image’s Role (image, small_image, thumbnail, swatch_image, etc…) attribute id value under the eav_attribute table.

All the images value for a product are also stored under the given tables,

catalog_product_entity_media_gallery
catalog_product_entity_media_gallery_value
catalog_product_entity_media_gallery_value_to_entity

Once you add any images for a product, one entry is generated under a catalog_product_entity_media_gallery table. if you have added multiple images for a product, multiple entries are generated in a table.

catalog_product_entity_media_gallery table stored the value of the images as attribute_id with image value.  when you check the above table, you can see the attribute_id value as 88 (88 is attribute_id of small_image in my case) and the value field is /a/b/abc.jpg

catalog_product_entity_media_gallery_value table used to store values of store id, Image Label, image position, Hide from Product Page (disabled column), and link with product id value stored as row_id.

catalog_product_entity_media_gallery_value_to_entity used for mapping product entity id with catalog_product_entity_media_gallery_value table.

PHYSICAL LOCATION OF IMAGES:

All the images are stored under the pub/media/catalog/product folder in the Magento root.

If the product image name is abc.jpg then it’s stored under the above folder with an a/b/abc.jpg  path with the first folder name as the first character of the image and the second folder being the second character of the image name.

If an image has only one character like a.jpg then it’s stored under a/_/a.jpg path under pub/media/catalog/product

A final path for image name abc.jpg  is pub/media/catalog/product/a/b/abc.jpg

Get Media image collection from the Specific product by Gallery Image Collection

Fetch Image Roles types by link Get Image roles types from the product.

  • How does Magento Save/Delete Media Images after Creating/Updating Products from Admin Panel?

Magento 2 Backend Product Images Saved Logic will be available in the Magento_Catalog module with Model class Magento\Catalog\Model\Product\Gallery\CreateHandler and Magento\Catalog\Model\Product\Gallery\UpdateHandler check execute() the method that handles the image saves operation.