8

I'm trying to display thumbnail image in my custom admin module edit form like this enter image description here Right now I'm using this addfield.

 $fieldset->addField('main_image', 'image', array(
        'label'     => Mage::helper('ram')->__('Image'),
        'required'  => false,
        'name'      => 'main_image',
    ));

I looked at lib/Varien/Data/Form/Element/Image.php and found this code which uses images url there.

public function getElementHtml()
    {
        $html = '';

        if ((string)$this->getValue()) {
            $url = $this->_getUrl();

            if( !preg_match("/^http\:\/\/|https\:\/\//", $url) ) {
                $url = Mage::getBaseUrl('media') . $url;
            }

But I'm not storing images in media folder for this module. I'm uploading images from the controller in ram_images folder.

 $path = Mage::getBaseDir('media') . DS .'ram_images' ;
   $uploader->save($path, $_FILES['main_image']['name'] );

Also, i could not change URL from Image.php file as it will affect other system modules. is there any workaround here. Please suggest a solution.

And I also want to delete image using checkbox provided below upload button.

Thanks.

3 Answers 3

10

Add the code in your controller file

public function saveAction() 
{
    ...

    if (!empty( $_FILES['main_image']['name'] )) 
    {
        $data['main_image'] =  $_FILES['main_image']['name'] ;
    } 
    else 
    {
        if (isset($data['main_image']['delete']) && $data['main_image']['delete'] == 1) 
        {
            if ($data['main_image']['value'] != '')
                $this->removeFile($data['main_image']['value']);
            $data['main_image'] = '';
        }
        else 
        {
            unset($data['main_image']);
        }
    }

    ...
}
public function removeFile($file) 
{
        $_helper = Mage::helper('ram');
        $file = $_helper->updateDirSepereator($file);
        $directory = Mage::getBaseDir('media') . DS .'ram_images' ;
        $io = new Varien_Io_File();
        $result = $io->rmdir($directory, true);
}

Create below function in Helper Class

 public function updateDirSepereator($path) 
    {
        return str_replace('\\', DS, $path);
    }
1
  • i will try this, i hope it works
    – HungryDB
    Commented Dec 6, 2014 at 10:15
7

Add 'enctype'’ ⇒ 'multipart/form-data' That should help to get something in $_FILES

in /app/code/local/Chilly/ModName/Block/Adminhtml/ModName/Edit/Form.php

You should have something looking like this:

$form = new Varien_Data_Form(array(

         'id' => 'edit_form',

        'action' => $this->getUrl('*/*/save', array('id' =>    $this->getRequest()->getParam('id'))),

        'method' => 'post',

        'enctype' => 'multipart/form-data'

    )

);

Then in /app/code/local/Chilly/ModName/Block/Adminhtml/ModName/Edit/Tab/Form.php

$fieldset->addField('main_image', 'image', array(
    'label'     => Mage::helper('ram')->__('Image'),
    'required'  => false,
    'name'      => 'main_image',
));

Then in /app/code/local/Chilly/ModName/controllers/Adminhtml/ModuleNameController.php in saveAction()

        if(isset($_FILES['main_images']['name'])) {
          try {
            $uploader = new Varien_File_Uploader('main_images');
            $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png')); // or pdf or anything
            $uploader->setAllowRenameFiles(false);
            $uploader->setFilesDispersion(false);
            $path = Mage::getBaseDir('media') . DS. 'ram_images'.DS ;
            $destFile = $path.$_FILES['main-images']['name'];
            $filename = $uploader->getNewFileName($destFile);
            $uploader->save($path, $filename);
            $data[main_images] = 'ram_images'. $filename;
          }catch(Exception $e) { }
       }

Your's edit form will be display image like as..

enter image description here

3
  • I have already uploaded image. i just want it to display as a thumbnail on edit form.
    – HungryDB
    Commented Dec 6, 2014 at 9:02
  • Above code will be display image in your edit form. Commented Dec 6, 2014 at 9:46
  • Yeah, probably. But i'm using $path = Mage::getBaseDir('media') . DS .'ram_images'; this path to upload images.
    – HungryDB
    Commented Dec 6, 2014 at 9:55
4

I think i got a simple solution here to display thumbnail image.

if(isset($_FILES['main_images']['name'])) {
          try {
            $uploader = new Varien_File_Uploader('main_images');
            $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
            $uploader->setAllowRenameFiles(false);
            $uploader->setFilesDispersion(false);
            $path = Mage::getBaseDir('media') . / ;
            $uploader->save($path, $_FILES['main_images']['name']);
            $data[main_images] ='ram_images'. $_FILES['main_images']['name'];
          }catch(Exception $e) { }
       }

just saved folder name too in the table and it worked.

To delete image just added following code to module controllers saveAction() function.

if (!empty( $_FILES['main_image']['name'] )) 
 {
    $data['main_image'] =  $_FILES['main_image']['name'] ;
 } 
 else 
 {
     if (isset($data['main_image']['delete']) && $data['main_image']['delete'] == 1) 
     {
         $data['main_image']='';
     }
     else 
     { 
         unset($data['main_iamge']);
      }
 }

It worked.

3
  • Hi @HungryDB, this worked for me as well, but partially. The images are being uploaded but the thumbnail doesn't show up on the form, only the browse button. Do I have to add a function to helper as well to make it work? Commented May 26, 2017 at 23:02
  • use field type image
    – HungryDB
    Commented May 29, 2017 at 6:34
  • Hi @HungryDB, I did, but it still only shows the button. Commented May 30, 2017 at 18:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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