How to Show Images on Frontend for Custom Module in Magento 1.8

frontendmagento-1.8module

I have a form in my custom module with all the fields saved in the database table,
now I have a created an image upload form in the next section of the tab which saves the data in the different table from the above mention table.

I have successfully uploaded the image from the backend, and now I want to show those images in frontend.

I have searched the internet for quite a long time but I didn't get how can I show those images.

I showed all the content of the first table on frontend but I have no idea how to do that for the images. Please someone help me with this.

EDIT

Here is my controller save function

public function saveAction()
   {
     if ($this->getRequest()->getPost())
     {
       try {
             $postData = $this->getRequest()->getPost(); 
     $articleModel = Mage::getModel('blog/article');
     $imgFilename = NULL;

    if($_FILES['image']['name'] != '') 
      {
                try { 
                     $uploader = new Varien_File_Uploader('image');
                     $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
                     $uploader->setAllowRenameFiles(false);
                     $uploader->setFilesDispersion(false);
         $uploader->setAllowCreateFolders(true);
                     // Set media as the upload dir
                     //$media_path  = Mage::getBaseDir('media') . DS;
         $media_path = Mage::getBaseDir('media') . DS . 'blog' . DS;
                     $imgFilename = $media_path . $postData['image'];
                     // Upload the image

                     $uploader->save($media_path, $_FILES['image']['name']);
                    }
                 catch (Exception $e) 
        { 
                     Mage::log($e);
                     $this->_redirectError(502);
                    }
        $data['image'] = $_FILES['image']['name'];
              } 
     else 
      {        
        if(isset($data['image']['delete']) && $data['image']['delete'] == 1)
         $data['image'] = '';
        else 
        unset($data['image']);
      }
        $imageModel=Mage::getModel('blog/image');

                if( $this->getRequest()->getParam('id') <= 0 )
                  $articleModel->setCreatedTime(
                  Mage::getSingleton('core/date')
                        ->gmtDate());
                  $articleModel
                  ->addData($postData)
                  ->setUpdatedTime(
                         Mage::getSingleton('core/date')
                         ->gmtDate())
                  ->setId($this->getRequest()->getParam('id'))
                  ->save();
          $lastid = $articleModel->getId();
        $imageModel->setArticleId($lastid)->setImage($data['image'])->save();

                Mage::getSingleton('adminhtml/session')
                           ->addSuccess('successfully saved');
                Mage::getSingleton('adminhtml/session')
                            ->setarticleData(false);
                $this->_redirect('*/*/');
            if ($this->getRequest()->getParam('back')) {
    $this->_redirect('*/*/edit',array('id' => $articleModel->getId()));
        return;
    }
          } 
    catch (Exception $e)
    {
             Mage::getSingleton('adminhtml/session')
                              ->addError($e->getMessage());
             Mage::getSingleton('adminhtml/session')
                ->setarticleData($this->getRequest()
                                ->getPost());
             $this->_redirect('*/*/edit',
                        array('id' => $this->getRequest()
                                            ->getParam('id')));
             return;
            }
          }
          $this->_redirect('*/*/');
        }

Best Answer

does the ./media/blog/ exist and is it writable? And what is $postData['image'] value. Is it filled in the form with the intended name for the image? I don't think it's used anywhere.

Also, since you're saving the image to a different database table than the blog you will need to load a second model on the frontend. And since it seems you can have multiple images it should be a collection you're loading

$article = Mage::getModel('blog/article')->load([the required id]);
$image_collection = Mage::getModel('blog/image')->getCollection()
  ->addFilterToFilter('article_id', $article->getId());

foreach ($image_collection as $image):
   echo '<img src="' . Mage::getBaseUrl('media').'/blog/'.$image->getData('image') . '"/>';
endforeach;

if the images are not displayed debug the following way

  1. Put a try{}catch{} around the save methods in the controller to see if an error is given while saving
  2. Make sure there is actually data saved in the database and the data is correct through PHPMyAdmin or MySQL CLI
  3. check the size of the collection if there are actually items begin loaded
  4. print the $image->getData() to see what data it contains
Related Topic