0

Hello I'm learning Zend Framework and I want to make simple gallery. I have a form and upload photos to db but when I want to display I got strenght text instead of images. How can retrive this images?. In db I have photo row with BLOB type. Here is my code: Controller

public function addAction()
{
    $form = new Application_Form_Gallery();
    $this->view->form = $form;
    if($this->getRequest()->isPost()){
        $formData = $this->getRequest()->getPost();
        if($form->isValid($formData)){
            $image = $form->getValue('file');
            $gallery = new Application_Model_Gallery();
            $gallery->add($image);
            $this->_helper->redirector('index');                
        }else{
            $form->populate($formData);
        }
    }
}

Model

public function add($photo){
    $data = array(
        'photo' => $photo,
        'created_at' => new Zend_Db_Expr('CURDATE()')
    );
    $this->insert($data);
}

View:

    <div class="img" style="width: 150px; height: 150px;">
<?php foreach($this->gallery as $photo):
    echo base64_decode($photo['photo']);
 endforeach; ?>
</div>
marcin.g
  • 365
  • 1
  • 2
  • 14
  • what do you mean by `strenght text`? – Raj May 03 '14 at 10:31
  • Instead of image I got this '×MøÛ¯yë®=÷ݸÓm÷óNvÓ¿:ï¯yòxér‰©•ë^uë"‚zg?]<Ó½ù$ñ˜v®³M£¦' In layout I have $this->headMeta()->appendHttpEquiv('Content-Type', 'image/jpg,image/png;'); – marcin.g May 03 '14 at 10:32
  • Why are you storing images to database? Why not upload them to a directory and store their names into database? – Jay Bhatt May 03 '14 at 11:17
  • I don't know Im learning maybe thats why :) Can you tell me how Can I do it? – marcin.g May 03 '14 at 11:18

1 Answers1

0

In your view you just "dump" the string base64-decoded string into a div, this won't work.

What you might want to do is have an action in your controller which renders each photo as a binary response. You then access this action in your view:

<img src="<?php $this->url(
    'action_that_renders_the_photo', array('id' => $gallery['photo_id']
  ); ?>">

I prefer this over base64-data in the browser. If you want to keep it as close to your example as possible, you might want to create an <img> and specify its source as base64-data:

<img src="data:image/png;base64,<?php echo $gallery['photo'] ?>">

But keep in mind, that not all browsers allow embedding base64-data.

dbrumann
  • 16,803
  • 2
  • 42
  • 58
  • It still doesnt work. I want to get image from database in this way I got it probably from directory. – marcin.g May 03 '14 at 11:13
  • "It doesn't work" is not a helpful problem description. Can you be more specific? Where exactly is your problem, what's the output and how does it differ from the expected output? Have a look at this question, maybe that helps: http://stackoverflow.com/questions/1207190/embedding-base64-images – dbrumann May 04 '14 at 19:17