Add Image in a Custom database and render it

namespace(Imran)/yourmodule(news)/Block/Adminhtml/News/Edit/Tab/Form.php
in adding field

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

in controller add those line before save

  if(isset($_FILES['newsimage']['name']) && $_FILES['newsimage']['name'] != '') {
                    echo"I am here";
				try {
					$uploader = new Varien_File_Uploader('newsimage');

	           		$uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
					$uploader->setAllowRenameFiles(false);

					$uploader->setFilesDispersion(false);

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

				} catch (Exception $e) {
                                    Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                                    Mage::getSingleton('adminhtml/session')->setNewsData($this->getRequest()->getPost());
                                    $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
                                    return;
                                }//'social/'.

	  			$imagehere=$postData['image'] = $_FILES['newsimage']['name'];
                             //   print_r($postData); die();

			}

			if(isset($postData['newsimage']['delete'])) $data['image'] = '';

                if($postData['created_time'] != NULL )
                {
                    $datecustom = Mage::app()->getLocale()->date($postData['created_time'], Zend_Date::DATE_SHORT);
                    
                   // print_r($datecustom);die();
                   // $model->setStartDate($date->toString('YYYY-MM-dd HH:mm:ss'));
                }

                 
                $newsModel->setId($this->getRequest()->getParam('id'))
                    //->setTitle($postData['title'])
                   // ->setContent($postData['content'])
                    ->setimage($imagehere)
                   // ->setStatus($postData['status'])
                   // ->setRemarks($postData['remarks'])
                   // ->setcreated_time($datecustom->toString('YYYY-MM-dd HH:mm:ss'))
                    ->save();

To show in admin html interface
namespace(Imran)/yourmodule(news)/Block/Adminhtml/News/Grid.php

$this->addColumn('image', array(
            'header'    => Mage::helper('news')->__('Image'),
            'align'     =>'center',
            'width'     => '50px',
            'height'    => '100px',
            'filter'    => false,
            'sortable'  => false,
            'renderer'  => 'news/adminhtml_widget_grid_renderer',
	));

namespace(Imran)/yourmodule(news)/Block/Adminhtml/Widget/Grid/Renderer.php

<?php
/** http://www.magentix.fr **/

class Imran_News_Block_Adminhtml_Widget_Grid_Renderer extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract {
	
	public function render(Varien_Object $row) {
		return ($this->_getImage($row));
	}
	
	protected function _getImage(Varien_Object $row) {
		$img = $row->image != '' ? '<img width="50" height="50" src="'.Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).$row->image.'" alt="" />' : '';
		return $img;
	}
	
}
Advertisements

Tagged: , ,

One thought on “Add Image in a Custom database and render it

  1. Zeonwax December 13, 2011 at 11:16 pm

    Hey, This is just what I am looking for. I have tried to implement this but I keep getting:
    Invalid Block Type… Can you help?

Comments are closed.

%d bloggers like this: