Magento 1.9 – Get Full Image Path on Frontend from Custom Field

imagemagento-1.9varien-file-uploader

I am struggling with getting the full image path form image uploaded via custom field in backend

I am able to get only the name of the image file, but not the full media path

Here is the form code

  $fieldset->addField('featured_image', 'image', array(
        'name' => 'featured_image',
        'label' => 'Featured Image'
    ));

Here is the saveAction Method

 if ($data = $this->getRequest()->getPost()) {
        $model = Mage::getModel('blog/post');
 /* Code For Featured Image */


 if(isset($_FILES['featured_image']['name']) and (file_exists($_FILES['featured_image']['tmp_name']))) {
            try {
                $uploader = new Varien_File_Uploader('featured_image');
                $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
                $uploader->setAllowRenameFiles(false);
                $uploader->setFilesDispersion(false);   
                $path = Mage::getBaseDir('media') . DS .'blog'.DS;

                $cleaned_image_name = str_replace(' ', '_', $_FILES['featured_image']['name']);
                $dest_featured_img = $path.$cleaned_image_name;
                $new_fetaured_img_name = $uploader->getNewFileName($dest_featured_img);
               // $uploader->save($path, $_FILES['featured_image']['name']);
              $uploader->save($path, $new_fetaured_img_name);

              $data['featured_image'] = $new_fetaured_img_name;
            }
            catch(Exception $e) {
                echo $e->getMessage();
                exit;
            }
        }
$model->setFeaturedImage($data['featured_image']);
            $model->save();
}

Now, The image is being uploaded fine, but as we can see only name of the image file is getting saved to db. Hence When retriving in phtml file as below, only name is printed

Retrive image from db in phtml file

   <img src="<?php echo
     $post->getFeaturedImage();?>">

Output is (Current)

'<img src="uploaded_image.jpg">`

However it should be (Expected)

'<img src="http://sitename.com/media/blog/uploaded_image.jpg">'

I seriously do not want to hardcode the media/blog/ in phtml file as answered Here , nor would like to save the complete hardcoded url in db field as later on I'd be implenting cdn for images.

Also hardcoding would then fail if i intend to save image in catalog like style

media/blog/a/1/atest_image.jpg
media/blog/a/2/atest2-image2.jpg  

Then What is the correct way to get the full url of image by image name in this case.

I have seen various answers on stackoverflow / other site tutorials including followig but they don't talk about the full image url, but only the filename saved in db field.

Magento is not saving new field in database even after flush cache storage

http://www.devproblems.com/add-an-image-field-in-your-admin-plugin-by-example-aw-blog/

ad thumbnail to aw blog posts

Best Answer

Retrive image from db in phtml file like this:

<?php $imageUrl = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).'/blog/'.$post->getFeaturedImage();?>
<img src="<?php echo $imageUrl;?>">