-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
157 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace Technooze\Timage\Helper; | ||
|
||
use Magento\Framework\Filesystem; | ||
use Magento\Framework\Image\AdapterFactory; | ||
use Magento\Framework\View\Element\Template; | ||
use Magento\Framework\App\Filesystem\DirectoryList; | ||
use Magento\Framework\App\Helper\Context; | ||
use Magento\Store\Model\StoreManagerInterface; | ||
|
||
class Resizer extends \Magento\Framework\App\Helper\AbstractHelper | ||
{ | ||
/* @var Filesystem */ | ||
private $_filesystem; | ||
|
||
/* @var StoreManagerInterface */ | ||
private $storeManager; | ||
|
||
/* @var AdapterFactory */ | ||
private $imageFactory; | ||
|
||
/* @var \Magento\Framework\Filesystem\Directory\WriteInterface */ | ||
private $_directory; | ||
|
||
public function __construct( | ||
Context $context, | ||
StoreManagerInterface $storeManager, | ||
AdapterFactory $imageFactory, | ||
Filesystem $filesystem | ||
) | ||
{ | ||
$this->storeManager = $storeManager; | ||
$this->imageFactory = $imageFactory; | ||
$this->_filesystem = $filesystem; | ||
$this->_directory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA); | ||
parent::__construct($context); | ||
} | ||
|
||
/** | ||
* @param $img string (relative to media folder /catalog/product/g/c/giftcard.jpg) | ||
* @param $width int | ||
* @param $height int | ||
* @return string | ||
*/ | ||
public function resizeImage($img, $width = 780, $height = 505) | ||
{ | ||
$mediaDir = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA); | ||
$img = $mediaDir->getAbsolutePath(null) . $img; | ||
|
||
try { | ||
if (!$this->_directory->isFile($img) || !$this->_directory->isExist($img)) { | ||
return false; | ||
} | ||
/* Target directory path where our resized image will be save */ | ||
$targetDir = $mediaDir->getAbsolutePath('technooze/cache/' . $width . 'x' . $height); | ||
$pathTargetDir = $this->_directory->getRelativePath($targetDir); | ||
/* If Directory not available, create it */ | ||
if (!$this->_directory->isExist($pathTargetDir)) { | ||
$this->_directory->create($pathTargetDir); | ||
} | ||
if (!$this->_directory->isExist($pathTargetDir)) { | ||
return false; | ||
} | ||
|
||
$image = $this->imageFactory->create(); | ||
$image->open($img); | ||
$image->keepAspectRatio(true); | ||
$image->resize($width, $height); | ||
$imageName = pathinfo($img, PATHINFO_BASENAME); | ||
$dest = $targetDir . '/' . $imageName; | ||
$image->save($dest); | ||
if ($this->_directory->isFile($this->_directory->getRelativePath($dest))) { | ||
return $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . 'technooze/cache/' . $width . 'x' . $height . '/' . $imageName; | ||
} | ||
} catch (\Exception $e) { | ||
$this->_logger->critical($e); | ||
} | ||
return false; | ||
} | ||
} |
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 | ||
|
||
namespace Technooze\Timage\Setup; | ||
|
||
use Magento\Framework\Setup\InstallDataInterface; | ||
use Magento\Framework\Setup\ModuleContextInterface; | ||
use Magento\Framework\Setup\ModuleDataSetupInterface; | ||
|
||
class InstallData implements InstallDataInterface | ||
{ | ||
/** | ||
* Function install | ||
* @param ModuleDataSetupInterface $setup | ||
* @param ModuleContextInterface $context | ||
* @return void | ||
*/ | ||
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) | ||
{ | ||
//install data here | ||
} | ||
} |
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 | ||
|
||
namespace Technooze\Timage\Setup; | ||
|
||
use Magento\Framework\Setup\InstallSchemaInterface; | ||
use Magento\Framework\Setup\ModuleContextInterface; | ||
use Magento\Framework\Setup\SchemaSetupInterface; | ||
|
||
class InstallSchema implements InstallSchemaInterface | ||
{ | ||
/** | ||
* Function install | ||
* @param SchemaSetupInterface $setup | ||
* @param ModuleContextInterface $context | ||
* @return void | ||
*/ | ||
public function install(SchemaSetupInterface $setup, ModuleContextInterface $context) | ||
{ | ||
$installer = $setup; | ||
$installer->startSetup(); | ||
} | ||
} |
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,23 @@ | ||
{ | ||
"name": "technooze/module-timage", | ||
"description": "N/A", | ||
"require": { | ||
"php": "~5.5.0|~5.6.0|~7.0.0", | ||
"magento/framework": "100.0.*" | ||
}, | ||
"type": "magento2-module", | ||
"version": "1.0.0", | ||
"license": [ | ||
"OSL-3.0", | ||
"AFL-3.0" | ||
], | ||
"autoload": { | ||
"files": [ | ||
"registration.php" | ||
], | ||
"psr-4": { | ||
"Technooze\\Timage\\": "" | ||
} | ||
} | ||
} | ||
|
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,4 @@ | ||
<?xml version="1.0"?> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> | ||
<module name="Technooze_Timage" setup_version="1.0.0"/> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php | ||
\Magento\Framework\Component\ComponentRegistrar::register( | ||
\Magento\Framework\Component\ComponentRegistrar::MODULE, | ||
'Technooze_Timage', | ||
__DIR__ | ||
); |