Skip to content

Commit

Permalink
Merge pull request #1507 from konarshankar07/generate-renditions--tas…
Browse files Browse the repository at this point in the history
…k-1476

#1476 :- Generate renditions for all images uploaded to magento
  • Loading branch information
sivaschenko authored Aug 12, 2020
2 parents 0b0b4aa + 66d3974 commit 3e16193
Show file tree
Hide file tree
Showing 11 changed files with 348 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

declare(strict_types=1);

namespace Magento\MediaGalleryRenditionsApi\Model;
namespace Magento\MediaGalleryRenditions\Model;

use Magento\Framework\App\Config\ScopeConfigInterface;

Expand All @@ -31,7 +31,6 @@ class Config
private $scopeConfig;

/**
* Config constructor.
* @param ScopeConfigInterface $scopeConfig
*/
public function __construct(
Expand All @@ -47,7 +46,7 @@ public function __construct(
*/
public function getWidth(): int
{
return $this->scopeConfig->getValue(self::XML_PATH_MEDIA_GALLERY_RENDITIONS_WIDTH_PATH);
return (int) $this->scopeConfig->getValue(self::XML_PATH_MEDIA_GALLERY_RENDITIONS_WIDTH_PATH);
}

/**
Expand All @@ -57,6 +56,6 @@ public function getWidth(): int
*/
public function getHeight(): int
{
return $this->scopeConfig->getValue(self::XML_PATH_MEDIA_GALLERY_RENDITIONS_HEIGHT_PATH);
return (int) $this->scopeConfig->getValue(self::XML_PATH_MEDIA_GALLERY_RENDITIONS_HEIGHT_PATH);
}
}
130 changes: 130 additions & 0 deletions MediaGalleryRenditions/Model/GenerateRenditions.php
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);
}
}
72 changes: 72 additions & 0 deletions MediaGalleryRenditions/Model/GetRenditionPath.php
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);
}
}
37 changes: 37 additions & 0 deletions MediaGalleryRenditions/Model/IsRenditionRequired.php
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();
}
}
44 changes: 44 additions & 0 deletions MediaGalleryRenditions/Plugin/CreateRenditions.php
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];
}
}
4 changes: 3 additions & 1 deletion MediaGalleryRenditions/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"description": "Magento module that implements height and width fields for for media gallery items.",
"require": {
"php": "~7.3.0||~7.4.0",
"magento/framework": "*"
"magento/framework": "*",
"magento/module-media-gallery-renditions-api": "*",
"magento/module-media-gallery-synchronization-api": "*"
},
"type": "magento2-module",
"license": [
Expand Down
14 changes: 14 additions & 0 deletions MediaGalleryRenditions/etc/di.xml
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>
2 changes: 1 addition & 1 deletion MediaGalleryRenditions/etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magento_MediaGalleryRenditions" />
<module name="Magento_MediaGalleryRenditions"/>
</config>
21 changes: 21 additions & 0 deletions MediaGalleryRenditionsApi/Api/GenerateRenditionsInterface.php
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 MediaGalleryRenditionsApi/Api/GetRenditionPathInterface.php
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;
}
Loading

0 comments on commit 3e16193

Please sign in to comment.