Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dbashyal committed Dec 2, 2019
1 parent ef340f0 commit 9b807fc
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 0 deletions.
81 changes: 81 additions & 0 deletions Technooze/Timage/Helper/Resizer.php
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;
}
}
21 changes: 21 additions & 0 deletions Technooze/Timage/Setup/InstallData.php
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
}
}
22 changes: 22 additions & 0 deletions Technooze/Timage/Setup/InstallSchema.php
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();
}
}
23 changes: 23 additions & 0 deletions Technooze/Timage/composer.json
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\\": ""
}
}
}

4 changes: 4 additions & 0 deletions Technooze/Timage/etc/module.xml
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>
6 changes: 6 additions & 0 deletions Technooze/Timage/registration.php
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__
);

0 comments on commit 9b807fc

Please sign in to comment.