0

I've image name and i would like to save it to my desired folder.

How to achieve this ? Any help would be appreciated.

Update

I am trying to implementing Image Crop function at frontend.

Like User will get option to get crop image option while they upload image.

For that I'm using a example. Check Here

Its working for Core PHP.

I am trying to implementing this code in my controller.

    $data = $this->request->getParam('group_image');

    list($type, $data) = explode(';', $data);
    list(, $data)      = explode(',', $data);


    $data = base64_decode($data);
    echo $imageName = time().'.png'; exit;

Here $imageName returns Image Name now i want to save this image under Pub/Media.

PS : PHP they have used file_put_contents('upload/'.$imageName, $data);

IF i use file_put_contents its working but due to i am doing this in Module MEQP2 standard not allow me to use this function I've to implement this in Magento way.

Question : What is Best Magento way to achieve this ?

2
  • can you explain in detail what you want Jun 16 '18 at 6:14
  • @MurtuzaZabuawala, Check updated Post.
    – anonymous
    Jun 16 '18 at 6:45
0

Please write below function into your helper class and pass full image URL and image name by which u want to saved image at folder.

public function downloadImage($url, $image_name) {
            try {
                $download_image = $this->dir->getPath('media') . '/import';
                if (!file_exists($download_image)) {
                    mkdir($download_image, 0777, true);
                }
                /*Image saved at specific folder*/   
                $img = $download_image . '/' . $image_name;
                file_put_contents($img, file_get_contents($url));
                /*return image full path after saved at folder*/    
                return $img;
            } catch (\Exception $e) {
                $this->log('Error while saving image');
                return false;
            }
        }
1
  • 1
    I aint willing to use file_put_contents.
    – anonymous
    Jun 16 '18 at 9:23
0

Try with following code in your customized file

$uploader = $this->_objectManager->create(
            \Magento\MediaStorage\Model\File\Uploader::class,
            ['fileId' => 'image']
        );

$mediaDirectory = $this->_objectManager->get(\Magento\Framework\Filesystem::class)->getDirectoryRead(DirectoryList::MEDIA);

$result = $uploader->save($mediaDirectory->getAbsolutePath());
0

Step 1: You need to create helper class file Image.php at Vender\Module\Helper\Image.php and past below code

<?php
namespace Vender\Module\Helper;

use Magento\Framework\Filesystem;
use Magento\Framework\Url;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Filesystem\DirectoryList;

class Image extends \Magento\Framework\App\Helper\AbstractHelper {
    protected $scopeConfig;
    protected $storeManager;
    protected $messageManager;
    protected $_response;
    protected $_resourceConfig;
    protected $_responseFactory;
    protected $_url;
    protected $_filesystem;
    protected $_directory;
    protected $_imageFactory;

    public function __construct(
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\Message\ManagerInterface $messageManager,
        \Magento\Framework\App\ResponseInterface $response,
        \Magento\Framework\App\Config\Storage\WriterInterface $resourceConfig,
        \Magento\Framework\App\ResponseFactory $responseFactory,
        \Magento\Framework\UrlInterface $url,
        \Magento\Framework\Filesystem $filesystem,
        \Magento\Framework\Image\AdapterFactory $imageFactory
    ) {
        $this->scopeConfig      = $scopeConfig;
        $this->_storeManager    = $storeManager;
        $this->messageManager   = $messageManager;
        $this->_response        = $response;
        $this->_resourceConfig  = $resourceConfig;
         $this->_responseFactory= $responseFactory;
        $this->_url             = $url;
        $this->_filesystem      = $filesystem;
        $this->_directory       = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
        $this->_imageFactory    = $imageFactory;
    }
    public function imageResize($src,$width=35,$height=35,$dir='yourdir/'){

        $absPath = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath().$src;

        $imageResized = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath($dir).
        $this->getNewDirectoryImage($src);
        $imageResize = $this->_imageFactory->create();

        $imageResize->open($absPath);
        $imageResize->backgroundColor([255, 255, 255]);
        $imageResize->constrainOnly(TRUE);
        $imageResize->keepTransparency(TRUE);
        $imageResize->keepFrame(true);
        $imageResize->keepAspectRatio(true);

        $imageResize->resize($width,$height);
        $dest = $imageResized ;
        $imageResize->save($dest);
        $resizedURL= $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA).$dir.$this->getNewDirectoryImage($src);
        return $resizedURL;

    }

    public function getNewDirectoryImage($src)
    {
        $segments = array_reverse(explode('/',$src));
        $first_dir = substr($segments[0],0,1);
        $second_dir = substr($segments[0],1,1);
        return 'cache/'.$first_dir.'/'.$second_dir.'/'.$segments[0];
    }
}

Step 2: Using below code you can call above imageResize() method from any class, block or templete.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$imgpath = $objectManager->create('Vender\Module\Helper\Image')->imageResize('IMAGE_PATH',50,50,'DIR_NAME/');
  • $src: Add Image Path for Ex: “Your_Uploaded_Image_Folder/a/b/abc.png”
  • $width: It should be number for Ex: “50”
  • $height: It should be number for Ex: “50”
  • $dir: Add directory name which you want to save and get resize image.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.