/   /   /  Magento 2 Backend : simple technique for upload image coming from edit form

Note:

For more extensions and themes visit our store

Magento 2 Backend : simple technique for upload image coming from edit form


Magento 2 allow you to add custom field form as type image , for example when you click in edit link inisde grid you have edit page you can add from fiedl :

  1.  
  2. $fieldset->addField(
  3. 'image',
  4. 'image',
  5. [
  6. 'title' => __('Image'),
  7. 'label' => __('Image'),
  8. 'name' => 'image',
  9. 'note' => 'Allow image type: jpg, jpeg, gif, png',
  10. ]
  11. );
  12.  


So now you have your field , you can use your controller to save the image inside media , now inject (\Magento\Framework\Image\AdapterFactory $adapterFactory,\Magento\MediaStorage\Model\File\UploaderFactory $uploader):

  1.  
  2. /**
  3. * @var \Magento\Framework\Image\AdapterFactory
  4. */
  5. protected $adapterFactory;
  6. /**
  7. * @var \Magento\MediaStorage\Model\File\UploaderFactory
  8. */
  9. protected $uploader;
  10. /**
  11. * @var \Magento\Framework\Filesystem
  12. */
  13. protected $filesystem;
  14. /**
  15. * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
  16. */
  17. protected $timezoneInterface;
  18. public function __construct(Action\Context $context,\Magento\Framework\Image\AdapterFactory $adapterFactory,\Magento\MediaStorage\Model\File\UploaderFactory $uploader,\Magento\Framework\Filesystem $filesystem)
  19. {
  20. $this->adapterFactory = $adapterFactory;
  21. $this->uploader = $uploader;
  22. $this->filesystem = $filesystem;
  23. parent::__construct($context);
  24. }
  25.  

inisde your execute() method :
  1.  
  2. /**
  3. * Save action
  4. *
  5. * @return \Magento\Framework\Controller\ResultInterface
  6. */
  7. public function execute()
  8. {
  9. ............
  10. //start block upload image
  11. if (isset($_FILES['image']) && isset($_FILES['image']['name']) && strlen($_FILES['image']['name'])) {
  12. /*
  13. * Save image upload
  14. */
  15. try {
  16. $base_media_path = 'ibnab/owlsliders/images';
  17. $uploader = $this->uploader->create(
  18. ['fileId' => 'image']
  19. );
  20. $uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
  21. $imageAdapter = $this->adapterFactory->create();
  22. $uploader->addValidateCallback('image', $imageAdapter, 'validateUploadFile');
  23. $uploader->setAllowRenameFiles(true);
  24. $uploader->setFilesDispersion(true);
  25. $mediaDirectory = $this->filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
  26. $result = $uploader->save(
  27. $mediaDirectory->getAbsolutePath(base_media_path)
  28. );
  29. $data['image'] = base_media_path.$result['file'];
  30. } catch (\Exception $e) {
  31. if ($e->getCode() == 0) {
  32. $this->messageManager->addError($e->getMessage());
  33. }
  34. }
  35. } else {
  36. if (isset($data['image']) && isset($data['image']['value'])) {
  37. if (isset($data['image']['delete'])) {
  38. $data['image'] = null;
  39. $data['delete_image'] = true;
  40. } elseif (isset($data['image']['value'])) {
  41. $data['image'] = $data['image']['value'];
  42. } else {
  43. $data['image'] = null;
  44. }
  45. }
  46. }
  47. //end block upload image
  48. ................
  49. }
  50.  

with simple code you upload the image and you try if delete is checked (if true it will be deleted)

Comments

Related Posts

make your store more efficient

Solving problems. With open source technology. Professional results. That’s what makes Ibnab your best choice

IBNAB is a company made of a group of professionals whose work is providing secure open source solutions. Our company strives for reaching magnificent results with each experience and provides professional open source solutions that cover every part of the business process.