/   /   /  OroCommerce for developers: some lines about product image dimensions

Note:

For more extensions and themes visit our store

OroCommerce for developers: some lines about product image dimensions


In the modern E-commerce system like OroCommerce dimensions of product image is changing related to current view for example in shopping list page the thumbnail has used otherwise in product view medium size has used and for zoom the system using the original one.

To explore the full .yml responsible to manage dimension of product images is:
/src/Oro/Bundle/ProductBundle/Resources/views/layouts/default/config/images.yml

Note: you can override images.yml in your theme 

To use your configuration of images.yml inside your php you can get the service
oro_product.provider.product_images_dimensions 

  1.  
  2. $imageDimensionsProvider = $this->container->get('oro_product.provider.product_images_dimensions');
  3.  

For example to browse all your dimensions:

  1.  
  2.             foreach ($imageDimensionsProvider->getDimensionsForProductImage($productImage) as $dimension) {
  3.                 $filterName = $dimension->getName();
  4.                 echo $filterName;
  5.             }
  6.  

the result is product_original , product_gallery_popup , product_gallery_main ….

You can get product image and apply any filter of those to your image:

  1.  
  2.   $image = $productImage->getImage();
  3.  

the $image result is instance of Oro\Bundle\AttachmentBundle\Entity\File which you can using now and apply filter on it:
  1.  
  2.            $imageDimensionsProvider = $this->container->get('oro_product.provider.product_images_dimensions');
  3.             $imageResizer = $this->container->get('oro_attachment.manager.image_resize');
  4.             $mediaCacheManager = $this->container->get('oro_attachment.manager.media_cache_manager_registry');
  5.             foreach ($imageDimensionsProvider->getDimensionsForProductImage($productImage) as $dimension) {
  6.                 $filterName = $dimension->getName();
  7.         foreach ($product->getImages() as $productImage) {
  8.            $image = $productImage->getImage();
  9.             $imagePath = $this->getFilteredImageUrl($image, $filterName);
  10.             $filteredImage = $imageResizer->applyFilter($image, $filterName,true);
  11.             if ($filteredImage) {
  12.                 $mediaCacheManager->getManagerForFile($image)->writeToStorage($filteredImage->getContent(), $imagePath);
  13.             }
  14.         }
  15.        }
  16.  

To generate image path to store image in cache add this small function:

  1.  
  2.     protected function getFilteredImageUrl($file, $filterName) {
  3.   $urlGenerator = $this->container->get('oro_attachment.url_generator');
  4.         if ($file != null) {
  5.             return $urlGenerator->generate(
  6.                             'oro_frontend_attachment_filter_image', [
  7.                         'filter' => $filterName,
  8.                         'id' => $file->getId(),
  9.                         'filename' => $file->getFilename(),
  10.                             ], UrlGeneratorInterface::ABSOLUTE_PATH
  11.             );
  12.         }
  13.         return null;
  14. }
  15.  

Now you have applied all your filters on your products images and you have stocked all in the cache. 

Comments

IBNAB is a company made of a group of professionals whose work is providing secure open source solutions. Our company strives for reaching magnificent results with each experience and provides professional open source solutions that cover every part of the business process.