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 exploring 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 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_varchartable with attribute_id and value field separately.

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

base thumbnail small image magento 2

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

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

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

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 attribute_id value as 88 (88 is attribute_id of small_image in my case) and 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 an above folder with an a/b/abc.jpg  path with the first folder name is the first character of image and the second folder is 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 Magento Save/Delete Media Images after Create/Update Product 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() method that handles the image saves operation.