-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1507 from konarshankar07/generate-renditions--tas…
…k-1476 #1476 :- Generate renditions for all images uploaded to magento
- Loading branch information
Showing
11 changed files
with
348 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\MediaGalleryRenditions\Model; | ||
|
||
use Magento\Framework\App\Filesystem\DirectoryList; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Framework\Filesystem; | ||
use Magento\Framework\Filesystem\Driver\File; | ||
use Magento\Framework\Image\AdapterFactory; | ||
use Magento\MediaGalleryRenditionsApi\Api\GenerateRenditionsInterface; | ||
use Magento\MediaGalleryRenditionsApi\Api\GetRenditionPathInterface; | ||
|
||
class GenerateRenditions implements GenerateRenditionsInterface | ||
{ | ||
/** | ||
* @var AdapterFactory | ||
*/ | ||
private $imageFactory; | ||
|
||
/** | ||
* @var Config | ||
*/ | ||
private $config; | ||
|
||
/** | ||
* @var GetRenditionPathInterface | ||
*/ | ||
private $getRenditionPath; | ||
|
||
/** | ||
* @var Filesystem | ||
*/ | ||
private $filesystem; | ||
|
||
/** | ||
* @var IsRenditionRequired | ||
*/ | ||
private $isRenditionRequired; | ||
|
||
/** | ||
* @var File | ||
*/ | ||
private $driver; | ||
|
||
/** | ||
* @param AdapterFactory $imageFactory | ||
* @param Config $config | ||
* @param GetRenditionPathInterface $getRenditionPath | ||
* @param Filesystem $filesystem | ||
* @param File $driver | ||
* @param IsRenditionRequired $isRenditionRequired | ||
*/ | ||
public function __construct( | ||
AdapterFactory $imageFactory, | ||
Config $config, | ||
GetRenditionPathInterface $getRenditionPath, | ||
Filesystem $filesystem, | ||
File $driver, | ||
IsRenditionRequired $isRenditionRequired | ||
) { | ||
$this->imageFactory = $imageFactory; | ||
$this->config = $config; | ||
$this->getRenditionPath = $getRenditionPath; | ||
$this->filesystem = $filesystem; | ||
$this->isRenditionRequired = $isRenditionRequired; | ||
$this->driver = $driver; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function execute(array $paths): void | ||
{ | ||
foreach ($paths as $path) { | ||
$mediaDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA); | ||
$absolutePath = $mediaDirectory->getAbsolutePath($path); | ||
if (!$this->isRenditionRequired->execute($absolutePath)) { | ||
continue; | ||
} | ||
|
||
$renditionPath = $this->getRenditionPath->execute($path); | ||
$this->createDirectory($renditionPath); | ||
|
||
try { | ||
$this->createRendition($absolutePath, $mediaDirectory->getAbsolutePath($renditionPath)); | ||
} catch (\Exception $exception) { | ||
throw new LocalizedException( | ||
__('Cannot create rendition for media asset %path', ['path' => $path]) | ||
); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Create directory for rendition file | ||
* | ||
* @param string $path | ||
* @throws LocalizedException | ||
*/ | ||
private function createDirectory(string $path): void | ||
{ | ||
try { | ||
$this->filesystem->getDirectoryWrite(DirectoryList::MEDIA) | ||
->create($this->driver->getParentDirectory($path)); | ||
} catch (\Exception $exception) { | ||
throw new LocalizedException(__('Cannot create directory for rendition %path', ['path' => $path])); | ||
} | ||
} | ||
|
||
/** | ||
* Create rendition file | ||
* | ||
* @param string $absolutePath | ||
* @param string $absoluteRenditionPath | ||
* @throws \Exception | ||
*/ | ||
private function createRendition(string $absolutePath, string $absoluteRenditionPath): void | ||
{ | ||
$image = $this->imageFactory->create(); | ||
$image->open($absolutePath); | ||
$image->keepAspectRatio(true); | ||
$image->resize($this->config->getWidth(), $this->config->getHeight()); | ||
$image->save($absoluteRenditionPath); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\MediaGalleryRenditions\Model; | ||
|
||
use Magento\Framework\App\Filesystem\DirectoryList; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Framework\Filesystem; | ||
use Magento\Framework\Filesystem\Directory\ReadInterface; | ||
use Magento\MediaGalleryRenditionsApi\Api\GetRenditionPathInterface; | ||
|
||
class GetRenditionPath implements GetRenditionPathInterface | ||
{ | ||
private const RENDITIONS_DIRECTORY_NAME = '.renditions'; | ||
|
||
/** | ||
* @var Filesystem | ||
*/ | ||
private $filesystem; | ||
|
||
/** | ||
* @var IsRenditionRequired | ||
*/ | ||
private $isRenditionRequired; | ||
|
||
/** | ||
* @param Filesystem $filesystem | ||
* @param IsRenditionRequired $isRenditionRequired | ||
*/ | ||
public function __construct( | ||
Filesystem $filesystem, | ||
IsRenditionRequired $isRenditionRequired | ||
) { | ||
$this->filesystem = $filesystem; | ||
$this->isRenditionRequired = $isRenditionRequired; | ||
} | ||
|
||
/** | ||
* Returns Rendition image path | ||
* | ||
* @param string $path | ||
* @return string | ||
*/ | ||
public function execute(string $path) :string | ||
{ | ||
$mediaDirectory = $this->getMediaDirectory(); | ||
|
||
if (!$mediaDirectory->isFile($path)) { | ||
throw new LocalizedException(__('Media asset file %path does not exist!', ['path' => $path])); | ||
} | ||
|
||
if (!$this->isRenditionRequired->execute($mediaDirectory->getAbsolutePath($path))) { | ||
return $path; | ||
} | ||
|
||
return self::RENDITIONS_DIRECTORY_NAME . '/' . ltrim($path, '/'); | ||
} | ||
|
||
/** | ||
* Retrieve media directory instance with read access | ||
* | ||
* @return ReadInterface | ||
*/ | ||
private function getMediaDirectory(): ReadInterface | ||
{ | ||
return $this->filesystem->getDirectoryRead(DirectoryList::MEDIA); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\MediaGalleryRenditions\Model; | ||
|
||
class IsRenditionRequired | ||
{ | ||
/** | ||
* @var Config | ||
*/ | ||
private $config; | ||
|
||
/** | ||
* @param Config $config | ||
*/ | ||
public function __construct( | ||
Config $config | ||
) { | ||
$this->config = $config; | ||
} | ||
|
||
/** | ||
* Check if image needs to resize or not | ||
* | ||
* @param string $absolutePath | ||
* @return bool | ||
*/ | ||
public function execute(string $absolutePath): bool | ||
{ | ||
[$width, $height] = getimagesize($absolutePath); | ||
return $width > $this->config->getWidth() || $height > $this->config->getHeight(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\MediaGalleryRenditions\Plugin; | ||
|
||
use Magento\MediaGalleryRenditionsApi\Api\GenerateRenditionsInterface; | ||
use Magento\MediaGallerySynchronizationApi\Model\ImportFilesComposite; | ||
|
||
/** | ||
* Create renditions for media assets | ||
*/ | ||
class CreateRenditions | ||
{ | ||
/** | ||
* @var GenerateRenditionsInterface | ||
*/ | ||
private $generateRenditions; | ||
|
||
/** | ||
* @param GenerateRenditionsInterface $generateRenditions | ||
*/ | ||
public function __construct(GenerateRenditionsInterface $generateRenditions) | ||
{ | ||
$this->generateRenditions = $generateRenditions; | ||
} | ||
|
||
/** | ||
* Create renditions for synced files. | ||
* | ||
* @param ImportFilesComposite $subject | ||
* @param string[] $paths | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function beforeExecute(ImportFilesComposite $subject, array $paths): array | ||
{ | ||
$this->generateRenditions->execute($paths); | ||
|
||
return [$paths]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | ||
<preference for="Magento\MediaGalleryRenditionsApi\Api\GenerateRenditionsInterface" type="Magento\MediaGalleryRenditions\Model\GenerateRenditions"/> | ||
<preference for="Magento\MediaGalleryRenditionsApi\Api\GetRenditionPathInterface" type="Magento\MediaGalleryRenditions\Model\GetRenditionPath"/> | ||
<type name="Magento\MediaGallerySynchronizationApi\Model\ImportFilesComposite"> | ||
<plugin name="generate_rendtions" type="Magento\MediaGalleryRenditions\Plugin\CreateRenditions"/> | ||
</type> | ||
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
MediaGalleryRenditionsApi/Api/GenerateRenditionsInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\MediaGalleryRenditionsApi\Api; | ||
|
||
use Magento\Framework\Exception\LocalizedException; | ||
|
||
interface GenerateRenditionsInterface | ||
{ | ||
/** | ||
* Generate image renditions | ||
* | ||
* @param string[] $paths | ||
* @throws LocalizedException | ||
*/ | ||
public function execute(array $paths): void; | ||
} |
22 changes: 22 additions & 0 deletions
22
MediaGalleryRenditionsApi/Api/GetRenditionPathInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\MediaGalleryRenditionsApi\Api; | ||
|
||
use Magento\Framework\Exception\LocalizedException; | ||
|
||
interface GetRenditionPathInterface | ||
{ | ||
/** | ||
* Get Renditions image path | ||
* | ||
* @param string $path | ||
* @return string | ||
* @throws LocalizedException | ||
*/ | ||
public function execute(string $path): string; | ||
} |
Oops, something went wrong.