-
Create folders under magento root
<magento
root>/code/<vendor>/<module>/
\Magento\Framework\Component\ComponentRegistrar::register(\Magento\Framework\Component\ComponentRegistrar::MODULE,'<vendor>_<module>',__DIR__);
-
Create folder ‘etc’ and create file ‘module.xml’ in it,, and put the below content in it.
<?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="<vendor>_<module>" setup_version="1.0.0">
</module>
</config>
-
Create a route for the controllers which are needed to be made. Create e folder ‘frontend’ in etc folder and create file ‘routes.xml’ in it, and enter the following details in it.
<?xml
version="1.0" ?>
<config
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router
id="standard">
<route
frontName="<route_path>" id="<route_path>">
<module
name="<vendor>_<module>"/>
</route>
</router>
</config>
-
Create folder ‘Controller’ and create two folder in it.
‘Delete’
and ‘Index’. Create Index.php in both of it.
-
In Delete -> Index.php , paste the following code
namespace
<vendor>\<module>\Controller\Delete;
use
Magento\Framework\App\Filesystem\DirectoryList;
class
Index extends \Magento\Framework\App\Action\Action
{
protected
$_filesystem;
protected
$image;
protected
$_file;
public
function __construct(
\Magento\Framework\App\Action\Context
$context,
\<vendor>\<module>\Block\Imageprofile
$image,
\Magento\Framework\Filesystem\Driver\File
$file,
\Magento\Framework\Filesystem
$fileSystem
)
{
parent::__construct($context);
$this->image
= $image;
$this->_file
= $file;
$this->_filesystem
= $fileSystem;
}
public
function execute(){
$fileName
= 'IMG_'.$this->image->currentUser()->getId();
$mediaRootDir
=
$this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath('images/'.$this->image->currentUser()->getId().'/');
if
($this->_file->isExists($mediaRootDir.$fileName)) {
$this->_file->deleteFile($mediaRootDir.$fileName);
}
$this->_redirect('customer/account/');
}
}
-
In Index -> Index.php , paste the following code
namespace
<vendor>\<module>\Controller\Index;
use
Magento\Framework\App\Filesystem\DirectoryList;
class
Index extends \Magento\Framework\App\Action\Action
{
protected
$_filesystem;
protected
$image;
public
function __construct(
\Magento\Framework\App\Action\Context
$context,
\Magento\Framework\Filesystem
$fileSystem,
\<vendor>\<module>\Block\Imageprofile
$image
)
{
parent::__construct($context);
$this->_filesystem
= $fileSystem;
$this->image
= $image;
}
public
function execute()
{
$result
= array();
if
($_FILES['profile_image']['name']) {
try
{
//
init uploader model.
$uploader
= $this->_objectManager->create(
'Magento\MediaStorage\Model\File\Uploader',
['fileId'
=> 'profile_image']
);
$uploader->setAllowedExtensions(['jpg',
'jpeg', 'png']);
$uploader->setAllowRenameFiles(true);
$uploader->setFilesDispersion(false);
//
get media directory
$mediaDirectory
= $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA);
//
save the image to media directory
$result
=
$uploader->save($mediaDirectory->getAbsolutePath('images/'.$this->image->currentUser()->getId().'/'),'IMG_'.$this->image->currentUser()->getId());
}
catch (Exception $e) {
\Zend_Debug::dump($e->getMessage());
}
}
$this->_redirect('customer/account/');
//echo
"Hello";
}
}
-
Finally create a folder named ‘Block’ and create a file ‘Imageprofile.php’ in it. Paste the following code in it.
namespace
<vendor>\<module>\Block;
use
Magento\Framework\App\Filesystem\DirectoryList;
class
Imageprofile extends \Magento\Framework\View\Element\Template
{
protected
$customerSession;
protected
$_file;
public
function __construct(
\Magento\Backend\Block\Template\Context
$context,
\Magento\Customer\Model\Session
$customerSession,
\Magento\Framework\Filesystem\Driver\File
$file,
array
$data = []
)
{
parent::__construct($context,$data);
$this->customerSession
= $customerSession;
$this->_file
= $file;
}
public
function currentUser()
{
return
$this->customerSession->getCustomer();//->getId();
}
public
function getMediaUrl()
{
$mediaUrl
= $this->_storeManager
->getStore()
->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
return
$mediaUrl;
}
public
function checkExist()
{
$filename
= 'IMG_'.$this->currentUser()->getId();
$filepath
=
$this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath('images/'.$this->currentUser()->getId().'/');
if
($this->_file->isExists($filepath.$filename)) {
return
1;
}else{
return
0;
}
//return
$filepath.$filename;
}
}
No comments:
Post a Comment