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
$imageDimensionsProvider = $this->container->get('oro_product.provider.product_images_dimensions');
For example to browse all your dimensions:
foreach ($imageDimensionsProvider->getDimensionsForProductImage($productImage) as $dimension) { $filterName = $dimension->getName(); echo $filterName; }
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:
$image = $productImage->getImage();
the $image result is instance of Oro\Bundle\AttachmentBundle\Entity\File which you can using now and apply filter on it:
$imageDimensionsProvider = $this->container->get('oro_product.provider.product_images_dimensions'); $imageResizer = $this->container->get('oro_attachment.manager.image_resize'); $mediaCacheManager = $this->container->get('oro_attachment.manager.media_cache_manager_registry'); foreach ($imageDimensionsProvider->getDimensionsForProductImage($productImage) as $dimension) { $filterName = $dimension->getName(); foreach ($product->getImages() as $productImage) { $image = $productImage->getImage(); $imagePath = $this->getFilteredImageUrl($image, $filterName); $filteredImage = $imageResizer->applyFilter($image, $filterName,true); if ($filteredImage) { $mediaCacheManager->getManagerForFile($image)->writeToStorage($filteredImage->getContent(), $imagePath); } } }
To generate image path to store image in cache add this small function:
protected function getFilteredImageUrl($file, $filterName) { $urlGenerator = $this->container->get('oro_attachment.url_generator'); if ($file != null) { return $urlGenerator->generate( 'oro_frontend_attachment_filter_image', [ 'filter' => $filterName, 'id' => $file->getId(), 'filename' => $file->getFilename(), ], UrlGeneratorInterface::ABSOLUTE_PATH ); } return null; }
Now you have applied all your filters on your products images and you have stocked all in the cache.
Comments