0

Is there a general class that deals with image processing (mainly resizing) that just receives a path to an image, some parameters and processes and saves the image somewhere and returns the new path?
I need this to make it work for images related to my custom entities Something like the image helper for products Magento\Catalog\Helper\Image but this looks very complicated to replicate as it's tightly coupled to the product entity.

If such a class exists in the core, I would appreciate and example on how to use.

1

You can try this factory class

\Magento\Framework\Image\AdapterFactory

For example if you initialize this class in your constructor you can use the below functionality.

public function __construct(                 
    \Magento\Framework\Image\AdapterFactory $imageFactory         
) {                      
    $this->imageFactory = $imageFactory;         
}

public function functionName($absolutePath)
{
   $imageProcessor = $this->imageFactory->create();         
   $imageProcessor->open($absolutePath);
   $imageProcessor->constrainOnly(TRUE);         
   $imageProcessor->keepTransparency(TRUE);         
   $imageProcessor->keepFrame(FALSE);         
   $imageProcessor->keepAspectRatio(TRUE);         
   $imageProcessor->resize(100, 100);
   $imageProcessor->save($this->destination);

   // And maybe here return the $this->destination property?
   return $this->destination;
}

For more functionality you can take a look in this class

Magento\Framework\Image\Adapter\AbstractAdapter

I would personally wrap this whole logic in a try catch block.

1
  • Thanks. I'll give it a try – Marius Apr 7 '20 at 14:22
1

I assume you are looking for \Magento\Framework\Image

1
  • I might. Do you also have an example on how to use? – Marius Apr 7 '20 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.