88namespace Magento \MediaGalleryRenditions \Model ;
99
1010use Magento \Framework \App \Filesystem \DirectoryList ;
11+ use Magento \Framework \Exception \LocalizedException ;
1112use Magento \Framework \Filesystem ;
13+ use Magento \Framework \Filesystem \Driver \File ;
1214use Magento \Framework \Image \AdapterFactory ;
13- use Magento \MediaGalleryApi \Api \Data \AssetInterface ;
1415use Magento \MediaGalleryRenditionsApi \Api \GenerateRenditionsInterface ;
1516use Magento \MediaGalleryRenditionsApi \Api \GetRenditionPathInterface ;
16- use Magento \MediaGallerySynchronization \Model \CreateAssetFromFile ;
17- use Magento \Framework \Filesystem \Driver \File ;
1817
1918class GenerateRenditions implements GenerateRenditionsInterface
2019{
@@ -24,79 +23,108 @@ class GenerateRenditions implements GenerateRenditionsInterface
2423 private $ imageFactory ;
2524
2625 /**
27- * @var GetRenditionPathInterface
26+ * @var Config
2827 */
29- private $ getRenditionPath ;
28+ private $ config ;
3029
3130 /**
32- * @var CreateAssetFromFile
31+ * @var GetRenditionPathInterface
3332 */
34- private $ createAssetFromFile ;
33+ private $ getRenditionPath ;
3534
3635 /**
3736 * @var Filesystem
3837 */
3938 private $ filesystem ;
4039
4140 /**
42- * @var IsRenditionImageResizeable
41+ * @var IsRenditionRequired
4342 */
44- private $ isRenditionImageResizeable ;
43+ private $ isRenditionRequired ;
4544
4645 /**
4746 * @var File
4847 */
4948 private $ driver ;
5049
5150 /**
52- * GenerateRenditions constructor.
5351 * @param AdapterFactory $imageFactory
52+ * @param Config $config
5453 * @param GetRenditionPathInterface $getRenditionPath
55- * @param CreateAssetFromFile $createAssetFromFile
5654 * @param Filesystem $filesystem
5755 * @param File $driver
58- * @param IsRenditionImageResizeable $isRenditionImageResizeable
56+ * @param IsRenditionRequired $isRenditionRequired
5957 */
6058 public function __construct (
6159 AdapterFactory $ imageFactory ,
60+ Config $ config ,
6261 GetRenditionPathInterface $ getRenditionPath ,
63- CreateAssetFromFile $ createAssetFromFile ,
6462 Filesystem $ filesystem ,
6563 File $ driver ,
66- IsRenditionImageResizeable $ isRenditionImageResizeable
64+ IsRenditionRequired $ isRenditionRequired
6765 ) {
6866 $ this ->imageFactory = $ imageFactory ;
67+ $ this ->config = $ config ;
6968 $ this ->getRenditionPath = $ getRenditionPath ;
70- $ this ->createAssetFromFile = $ createAssetFromFile ;
7169 $ this ->filesystem = $ filesystem ;
72- $ this ->isRenditionImageResizeable = $ isRenditionImageResizeable ;
70+ $ this ->isRenditionRequired = $ isRenditionRequired ;
7371 $ this ->driver = $ driver ;
7472 }
7573
7674 /**
77- * @inheritDoc
75+ * @inheritdoc
7876 */
79- public function execute (array $ assets ): void
77+ public function execute (array $ paths ): void
8078 {
81- /* @var $asset AssetInterface */
82- foreach ($ assets as $ asset ) {
79+ foreach ($ paths as $ path ) {
8380 $ mediaDirectory = $ this ->filesystem ->getDirectoryRead (DirectoryList::MEDIA );
84- $ path = $ mediaDirectory ->getAbsolutePath ($ asset -> getPath () );
85- if (!$ this ->isRenditionImageResizeable ->execute ($ asset )) {
81+ $ absolutePath = $ mediaDirectory ->getAbsolutePath ($ path );
82+ if (!$ this ->isRenditionRequired ->execute ($ absolutePath )) {
8683 continue ;
8784 }
88- $ renditionImagePath = $ this ->getRenditionPath ->execute ($ asset );
89- $ renditionDirectoryPath = $ this ->driver ->getParentDirectory ($ renditionImagePath );
85+
86+ $ renditionPath = $ this ->getRenditionPath ->execute ($ path );
87+ $ this ->createDirectory ($ renditionPath );
88+
89+ try {
90+ $ this ->createRendition ($ absolutePath , $ mediaDirectory ->getAbsolutePath ($ renditionPath ));
91+ } catch (\Exception $ exception ) {
92+ throw new LocalizedException (
93+ __ ('Cannot create rendition for media asset %path ' , ['path ' => $ path ])
94+ );
95+ }
96+ }
97+ }
98+
99+ /**
100+ * Create directory for rendition file
101+ *
102+ * @param string $path
103+ * @throws LocalizedException
104+ */
105+ private function createDirectory (string $ path ): void
106+ {
107+ try {
90108 $ this ->filesystem ->getDirectoryWrite (DirectoryList::MEDIA )
91- ->create ($ renditionDirectoryPath );
92- $ image = $ this ->imageFactory ->create ();
93- $ image ->open ($ path );
94- $ image ->keepAspectRatio (true );
95- $ image ->resize (
96- $ this ->isRenditionImageResizeable ->getResizedWidth (),
97- $ this ->isRenditionImageResizeable ->getResizedHeight ()
98- );
99- $ image ->save ($ mediaDirectory ->getAbsolutePath ($ renditionImagePath ));
109+ ->create ($ this ->driver ->getParentDirectory ($ path ));
110+ } catch (\Exception $ exception ) {
111+ throw new LocalizedException (__ ('Cannot create directory for rendition %path ' , ['path ' => $ path ]));
100112 }
101113 }
114+
115+ /**
116+ * Create rendition file
117+ *
118+ * @param string $absolutePath
119+ * @param string $absoluteRenditionPath
120+ * @throws \Exception
121+ */
122+ private function createRendition (string $ absolutePath , string $ absoluteRenditionPath ): void
123+ {
124+ $ image = $ this ->imageFactory ->create ();
125+ $ image ->open ($ absolutePath );
126+ $ image ->keepAspectRatio (true );
127+ $ image ->resize ($ this ->config ->getWidth (), $ this ->config ->getHeight ());
128+ $ image ->save ($ absoluteRenditionPath );
129+ }
102130}
0 commit comments