0

I am learning Magento and trying to make a module to manage Chefs in a Restaurant. I created an upload button to upload image and it works (images save in directory, file name saved in database).

But I don't know how to have a small preview of the image next to the button when the image has been uploaded (like when uploading Catalog picture). At the moment, when I click "edit" to edit a Chef Info, the Image field look the same if I uploaded image already or not.

Here is my field:

 $fieldset->addField('image_url', 'image', array(
        'label'     => Mage::helper('tressympa_cheflist')->__('Profile Picture'),
        'required'  => false,
        'name'      => 'image_url',
    ));

Any suggestion? Thank you very much!

1

save image name with your complete path.

media/{yourfolder}/imagename.jpg

save in database with yourfolder/imagename.jpg

it will show automatically in edit form.

OR

you can rendered the field and show the image

$fieldset->addField('image_url', 'image', array(
        'label'     => Mage::helper('tressympa_cheflist')->__('Profile Picture'),
        'required'  => false,
        'renderer' => 'modulename/adminhtml_modulename_renderer_image',
        'name'      => 'image_url',
    ));

add your file Block/Adminhtml/ModuleName/Renderer/Image.php

 <?php
    class NameSpace_ModuleName_Block_Adminhtml_ModuleName_Renderer_Image extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract{
       public function render(Varien_Object $row)   {
            $html = '<img id="' . $this->getColumn()->getId() . '" src="'.Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).$row->getData($this->getColumn()->getIndex()) . '"';
            $html .= '/>';
            return $html;
        }
    }
    ?>
  • Thank you for your help @Qaisar Satti, I thought the first solution must work but it didn't. I created a folder named 'chef_image' inside Media. And I saved in database and I opened database and it shows: chef_image/hemp-lion-quiltsandart-300x200.jpg. However, still no preview image :( – Tai Christian Oct 13 '15 at 2:26
  • What ouput link you get for image – Qaisar Satti Oct 13 '15 at 3:39
  • Sorry I am new to Magento.. But what do you mean my "output link"? How does it look like? – Tai Christian Oct 13 '15 at 11:18
  • it mean what is your value in edit form for image? – Qaisar Satti Oct 13 '15 at 11:28
  • can you add your edit script here too and your database value for image. – Qaisar Satti Oct 13 '15 at 11:42
0

You can achieve this if you save your image in default media folder instead of media/{yourfolder}/imagename.jpg like change image path

media/{yourfolder}/imagename.jpg => media/imagename.jpg

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.