Skip to content

Commit 3e16193

Browse files
authored
Merge pull request #1507 from konarshankar07/generate-renditions--task-1476
#1476 :- Generate renditions for all images uploaded to magento
2 parents 0b0b4aa + 66d3974 commit 3e16193

File tree

11 files changed

+348
-7
lines changed

11 files changed

+348
-7
lines changed

MediaGalleryRenditionsApi/Model/Config.php renamed to MediaGalleryRenditions/Model/Config.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
declare(strict_types=1);
88

9-
namespace Magento\MediaGalleryRenditionsApi\Model;
9+
namespace Magento\MediaGalleryRenditions\Model;
1010

1111
use Magento\Framework\App\Config\ScopeConfigInterface;
1212

@@ -31,7 +31,6 @@ class Config
3131
private $scopeConfig;
3232

3333
/**
34-
* Config constructor.
3534
* @param ScopeConfigInterface $scopeConfig
3635
*/
3736
public function __construct(
@@ -47,7 +46,7 @@ public function __construct(
4746
*/
4847
public function getWidth(): int
4948
{
50-
return $this->scopeConfig->getValue(self::XML_PATH_MEDIA_GALLERY_RENDITIONS_WIDTH_PATH);
49+
return (int) $this->scopeConfig->getValue(self::XML_PATH_MEDIA_GALLERY_RENDITIONS_WIDTH_PATH);
5150
}
5251

5352
/**
@@ -57,6 +56,6 @@ public function getWidth(): int
5756
*/
5857
public function getHeight(): int
5958
{
60-
return $this->scopeConfig->getValue(self::XML_PATH_MEDIA_GALLERY_RENDITIONS_HEIGHT_PATH);
59+
return (int) $this->scopeConfig->getValue(self::XML_PATH_MEDIA_GALLERY_RENDITIONS_HEIGHT_PATH);
6160
}
6261
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MediaGalleryRenditions\Model;
9+
10+
use Magento\Framework\App\Filesystem\DirectoryList;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\Filesystem;
13+
use Magento\Framework\Filesystem\Driver\File;
14+
use Magento\Framework\Image\AdapterFactory;
15+
use Magento\MediaGalleryRenditionsApi\Api\GenerateRenditionsInterface;
16+
use Magento\MediaGalleryRenditionsApi\Api\GetRenditionPathInterface;
17+
18+
class GenerateRenditions implements GenerateRenditionsInterface
19+
{
20+
/**
21+
* @var AdapterFactory
22+
*/
23+
private $imageFactory;
24+
25+
/**
26+
* @var Config
27+
*/
28+
private $config;
29+
30+
/**
31+
* @var GetRenditionPathInterface
32+
*/
33+
private $getRenditionPath;
34+
35+
/**
36+
* @var Filesystem
37+
*/
38+
private $filesystem;
39+
40+
/**
41+
* @var IsRenditionRequired
42+
*/
43+
private $isRenditionRequired;
44+
45+
/**
46+
* @var File
47+
*/
48+
private $driver;
49+
50+
/**
51+
* @param AdapterFactory $imageFactory
52+
* @param Config $config
53+
* @param GetRenditionPathInterface $getRenditionPath
54+
* @param Filesystem $filesystem
55+
* @param File $driver
56+
* @param IsRenditionRequired $isRenditionRequired
57+
*/
58+
public function __construct(
59+
AdapterFactory $imageFactory,
60+
Config $config,
61+
GetRenditionPathInterface $getRenditionPath,
62+
Filesystem $filesystem,
63+
File $driver,
64+
IsRenditionRequired $isRenditionRequired
65+
) {
66+
$this->imageFactory = $imageFactory;
67+
$this->config = $config;
68+
$this->getRenditionPath = $getRenditionPath;
69+
$this->filesystem = $filesystem;
70+
$this->isRenditionRequired = $isRenditionRequired;
71+
$this->driver = $driver;
72+
}
73+
74+
/**
75+
* @inheritdoc
76+
*/
77+
public function execute(array $paths): void
78+
{
79+
foreach ($paths as $path) {
80+
$mediaDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
81+
$absolutePath = $mediaDirectory->getAbsolutePath($path);
82+
if (!$this->isRenditionRequired->execute($absolutePath)) {
83+
continue;
84+
}
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 {
108+
$this->filesystem->getDirectoryWrite(DirectoryList::MEDIA)
109+
->create($this->driver->getParentDirectory($path));
110+
} catch (\Exception $exception) {
111+
throw new LocalizedException(__('Cannot create directory for rendition %path', ['path' => $path]));
112+
}
113+
}
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+
}
130+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MediaGalleryRenditions\Model;
9+
10+
use Magento\Framework\App\Filesystem\DirectoryList;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\Filesystem;
13+
use Magento\Framework\Filesystem\Directory\ReadInterface;
14+
use Magento\MediaGalleryRenditionsApi\Api\GetRenditionPathInterface;
15+
16+
class GetRenditionPath implements GetRenditionPathInterface
17+
{
18+
private const RENDITIONS_DIRECTORY_NAME = '.renditions';
19+
20+
/**
21+
* @var Filesystem
22+
*/
23+
private $filesystem;
24+
25+
/**
26+
* @var IsRenditionRequired
27+
*/
28+
private $isRenditionRequired;
29+
30+
/**
31+
* @param Filesystem $filesystem
32+
* @param IsRenditionRequired $isRenditionRequired
33+
*/
34+
public function __construct(
35+
Filesystem $filesystem,
36+
IsRenditionRequired $isRenditionRequired
37+
) {
38+
$this->filesystem = $filesystem;
39+
$this->isRenditionRequired = $isRenditionRequired;
40+
}
41+
42+
/**
43+
* Returns Rendition image path
44+
*
45+
* @param string $path
46+
* @return string
47+
*/
48+
public function execute(string $path) :string
49+
{
50+
$mediaDirectory = $this->getMediaDirectory();
51+
52+
if (!$mediaDirectory->isFile($path)) {
53+
throw new LocalizedException(__('Media asset file %path does not exist!', ['path' => $path]));
54+
}
55+
56+
if (!$this->isRenditionRequired->execute($mediaDirectory->getAbsolutePath($path))) {
57+
return $path;
58+
}
59+
60+
return self::RENDITIONS_DIRECTORY_NAME . '/' . ltrim($path, '/');
61+
}
62+
63+
/**
64+
* Retrieve media directory instance with read access
65+
*
66+
* @return ReadInterface
67+
*/
68+
private function getMediaDirectory(): ReadInterface
69+
{
70+
return $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
71+
}
72+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MediaGalleryRenditions\Model;
9+
10+
class IsRenditionRequired
11+
{
12+
/**
13+
* @var Config
14+
*/
15+
private $config;
16+
17+
/**
18+
* @param Config $config
19+
*/
20+
public function __construct(
21+
Config $config
22+
) {
23+
$this->config = $config;
24+
}
25+
26+
/**
27+
* Check if image needs to resize or not
28+
*
29+
* @param string $absolutePath
30+
* @return bool
31+
*/
32+
public function execute(string $absolutePath): bool
33+
{
34+
[$width, $height] = getimagesize($absolutePath);
35+
return $width > $this->config->getWidth() || $height > $this->config->getHeight();
36+
}
37+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MediaGalleryRenditions\Plugin;
9+
10+
use Magento\MediaGalleryRenditionsApi\Api\GenerateRenditionsInterface;
11+
use Magento\MediaGallerySynchronizationApi\Model\ImportFilesComposite;
12+
13+
/**
14+
* Create renditions for media assets
15+
*/
16+
class CreateRenditions
17+
{
18+
/**
19+
* @var GenerateRenditionsInterface
20+
*/
21+
private $generateRenditions;
22+
23+
/**
24+
* @param GenerateRenditionsInterface $generateRenditions
25+
*/
26+
public function __construct(GenerateRenditionsInterface $generateRenditions)
27+
{
28+
$this->generateRenditions = $generateRenditions;
29+
}
30+
31+
/**
32+
* Create renditions for synced files.
33+
*
34+
* @param ImportFilesComposite $subject
35+
* @param string[] $paths
36+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
37+
*/
38+
public function beforeExecute(ImportFilesComposite $subject, array $paths): array
39+
{
40+
$this->generateRenditions->execute($paths);
41+
42+
return [$paths];
43+
}
44+
}

MediaGalleryRenditions/composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
"description": "Magento module that implements height and width fields for for media gallery items.",
44
"require": {
55
"php": "~7.3.0||~7.4.0",
6-
"magento/framework": "*"
6+
"magento/framework": "*",
7+
"magento/module-media-gallery-renditions-api": "*",
8+
"magento/module-media-gallery-synchronization-api": "*"
79
},
810
"type": "magento2-module",
911
"license": [

MediaGalleryRenditions/etc/di.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
9+
<preference for="Magento\MediaGalleryRenditionsApi\Api\GenerateRenditionsInterface" type="Magento\MediaGalleryRenditions\Model\GenerateRenditions"/>
10+
<preference for="Magento\MediaGalleryRenditionsApi\Api\GetRenditionPathInterface" type="Magento\MediaGalleryRenditions\Model\GetRenditionPath"/>
11+
<type name="Magento\MediaGallerySynchronizationApi\Model\ImportFilesComposite">
12+
<plugin name="generate_rendtions" type="Magento\MediaGalleryRenditions\Plugin\CreateRenditions"/>
13+
</type>
14+
</config>

MediaGalleryRenditions/etc/module.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
9-
<module name="Magento_MediaGalleryRenditions" />
9+
<module name="Magento_MediaGalleryRenditions"/>
1010
</config>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MediaGalleryRenditionsApi\Api;
9+
10+
use Magento\Framework\Exception\LocalizedException;
11+
12+
interface GenerateRenditionsInterface
13+
{
14+
/**
15+
* Generate image renditions
16+
*
17+
* @param string[] $paths
18+
* @throws LocalizedException
19+
*/
20+
public function execute(array $paths): void;
21+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MediaGalleryRenditionsApi\Api;
9+
10+
use Magento\Framework\Exception\LocalizedException;
11+
12+
interface GetRenditionPathInterface
13+
{
14+
/**
15+
* Get Renditions image path
16+
*
17+
* @param string $path
18+
* @return string
19+
* @throws LocalizedException
20+
*/
21+
public function execute(string $path): string;
22+
}

0 commit comments

Comments
 (0)