Customizing the path product images are uploaded to (and thus their URL)

Hi everyone,

I’ve been scouring the web and the Magento core for a few days now, looking for a way to customize the pattern used to generate product image save paths in Magento 2, simply because the default paths (with the `getDispersionPath` subdirectories) are pretty bad.

It _seems_ like the logic behind this is not easily overridable or extensible, as it is dispersed across many different low-level core modules. The fact that I’m running Magento 2 headlessly and am using S3 + Cloudfront (through the `AwsS3` driver) to serve my images _might_ complicate matters further.

I’ve tried a few things so far to no avail:
– Override `MagentoCatalogModelProductGalleryProcessor`’s `addImage` method (defined in core at `vendor/magento/module-catalog/Model/Product/Gallery/Processor.php`)
– Override `MagentoCatalogModelResourceModelProductAttributeBackendImage`s `afterSave` method (defined in core at `vendor/magento/module-catalog/Model/ResourceModel/Product/Attribute/Backend/Image.php`)

I’ve also searched for all places where this `getDispersionPath` method is defined or called, but these are so deep down that it’s difficult to trace them back up to any high-level callers. Unfortunately, the `catalog/product/` string that’s hardcoded in the image paths is used in a multitude of places across the Magento core too, so no clear lead there.

Has anyone tried doing this before? Would you deem it worthwhile for me to continue looking for a durable solution, or should I best give up and keep the paths as is?

Thanks a lot for your time and wisdom.

submitted by /u/_dzn
[link] [comments]