Ask your Symfony questions! Pay money and get answers fast! (more info)

How to insert image file into database through YAML and call it ? Symfony

  • SOLVED

How to inset a image in database using YAML and call that image on webpage. DO i have to make change in action.class.php or layout.php.

my action.class.php is :


public function executeShow(sfWebRequest $request)
{
$this->members = Doctrine::getTable('members')->find(array($request->getParameter('id')));
$this->forward404Unless($this->members);
}

public function executeNew(sfWebRequest $request)
{
$this->form = new membersForm();
}

public function executeCreate(sfWebRequest $request)
{
$this->forward404Unless($request->isMethod(sfRequest::POST));

$this->form = new membersForm();

$this->processForm($request, $this->form);

$this->setTemplate('new');
}

public function executeEdit(sfWebRequest $request)
{
$this->forward404Unless($members = Doctrine::getTable('members')->find(array($request->getParameter('id'))), sprintf('Object members does not exist (%s)$
$this->form = new membersForm($members);
}

public function executeUpdate(sfWebRequest $request)
{
$this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
$this->forward404Unless($members = Doctrine::getTable('members')->find(array($request->getParameter('id'))), sprintf('Object members does not exist (%s)$
$this->form = new membersForm($members);

$this->processForm($request, $this->form);
$this->setTemplate('new');
}

public function executeEdit(sfWebRequest $request)
{
$this->forward404Unless($members = Doctrine::getTable('members')->find(array($request->getParameter('id'))), sprintf('Object members does not exist (%s)$
$this->form = new membersForm($members);
}

public function executeUpdate(sfWebRequest $request)
{
$this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
$this->forward404Unless($members = Doctrine::getTable('members')->find(array($request->getParameter('id'))), sprintf('Object members does not exist (%s)$
$this->form = new membersForm($members);

$this->processForm($request, $this->form);

$this->setTemplate('edit');
}

public function executeDelete(sfWebRequest $request)
{
$request->checkCSRFProtection();

$this->forward404Unless($members = Doctrine::getTable('members')->find(array($request->getParameter('id'))), sprintf('Object members does not exist (%s)$
$members->delete();

$this->redirect('bcl/index');
}

protected function processForm(sfWebRequest $request, sfForm $form)
{
$form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
if ($form->isValid())
$this->setTemplate('edit');
}

public function executeDelete(sfWebRequest $request)
{
$request->checkCSRFProtection();

$this->forward404Unless($members = Doctrine::getTable('members')->find(array($request->getParameter('id'))), sprintf('Object members does not exist (%s)$
$members->delete();

$this->redirect('bcl/index');
}

protected function processForm(sfWebRequest $request, sfForm $form)
{
$form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
if ($form->isValid())
{
$members = $form->save();

$this->redirect('bcl/edit?id='.$members->getId());
}
}
}

Answers (2)

2010-06-16

Yura Rodchyn answers:

If you mean loading images from fixtures, then convert the image using base64 and paste into fixture file.

For example:

File: DataFixturesHelper.php
<?php

class DataFixtureHelper
{
public static function image_for_yml($file)
{
$string = file_get_contents($file);
$string = <strong>base64_encode</strong>($string);
$string = str_replace("\n", "\n ", $string);
return $string . "\n";
}
}

?>


File: data.yml
<?php require_once(dirname(__FILE__).'/../../lib/helper/DataFixtureHelper.class.php'); ?>
Photos:
id: 1
image_name: default_avatar
image_mime_type: image/jpeg
image_data: |
<?php echo DataFixtureHelper::image_for_yml('default_avatar.jpg'); ?>



In your action echo image as I'm answer previous
<?php

class myActions extends sfActions

{

public function renderRawImage($data, $mimeType = 'image/jpeg')
{
$this->getResponse()->setContentType($mimeType);
$this->getResponse()->setContent($data);
return sfView::NONE;
}
}

class articlesActions extends myActions
{
public function executeShowImage(sfWebRequest $request)
{
$foto = Doctrine::getTable('fotos')->findOneBy('id',$request->getParameter('id'));
if(count($foto) > 0){
$this->renderRawImage(<strong>base64_decode</strong>($foto->getInhalt()));
return sfView::NONE;
}
}
}

?>


Or any other way you like.


Anurag Verma comments:

hi,

I am a newbie in symfony....

so, you saying i should make a new file DataFixtureHelper.php and then data.yml and then make add these codes to my action.class.php.

2010-06-16

José Antonio answers:

Hello Anurag Verma:

You can used a BLOB type in your database for store the images filetypes. For example, you can defined a image's table with one BLOB field, the others fields you can store the image's name and extension.

#YAML schema.yml
doctrine:
...
image_table:
id:
name: { type: VARCHAR, size: '200', required: true }
image: { type: BLOB, required: true }
extention: { type: VARCHAR, size: '4', required: true }
...

In your action, you can retrieve the images and store (temporality or not) in a image file. Then you can show it in the template. An example:

#In the action...
public function executeGetImage(sfWebRequest $request){
$image= Doctrine::getTable('image_table')->findOneBy('id',$request->getParameter('id'));
if(count($image)>0){
$this->image = sfConfig::get('sf_upload_dir').DIRECTORY_SEPARATOR.$image->getStudId().'.'.$image->getExtention();
$stream = fopen($this->image,'wb');
fwrite($stream, $image->getInhalt());
fclose($stream);
}
}

In the getImageSuccess.php you can put this code...

<img src="<?php echo $image;?>" alt="image" />

To save an image you can setter the BLOB field in Doctrine and pased the result of the function <strong>stream_get_contents($handler)</strong> where $handler will be the fopen with the upload image;


Anurag Verma comments:

hi Jose,

I tried your script but its is still not working. I made two tables one "Group" contain id,name, position and other table "image_table" contain id, image, extension.
Then gave a foreign relationship between the id.

But it is still not showing any image.


José Antonio comments:

I'll be glad if you can post your code here to see where can be the problem...


Anurag Verma comments:


# config/doctrine/schema.yml
team:
actAs: { Timestampable: ~ }
columns:
id: { type: integer, notnull: true, primary: true, autoincrement: true }
name: { type: string(255), notnull: true }
email: { type: string(255), notnull: true }

image_table:
columns:
id: { type:integer, notnull: true, primary: true, autoincrement: true }
image: { type: blob }
extension: { type: string(255), size: '4' }
relations:
team: { onDelete: CASCADE, local:id, foreign: id }

and the action class is

<?php

/**
* new actions.
*
* @package bclweb
* @subpackage new
* @author Your name here
* @version SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
*/
class bclActions extends sfActions
{
public function executeIndex(sfWebRequest $request)
{
$this->groups = Doctrine::getTable('group')
->createQuery('a')
->execute();
}

public function executeShow(sfWebRequest $request)
{
$this->group = Doctrine::getTable('group')->find(array($request->getParameter('id')));
$this->forward404Unless($this->group);
}

public function executeNew(sfWebRequest $request)
{
$this->form = new membersForm();
}

public function executeCreate(sfWebRequest $request)
{
$this->forward404Unless($request->isMethod(sfRequest::POST));

$this->form = new membersForm();

$this->processForm($request, $this->form);

$this->setTemplate('new');
}

public function executeEdit(sfWebRequest $request)
{
$this->forward404Unless($group = Doctrine::getTable('group')->find(array($request->getParameter('id'))), sprintf('Object group does not exist (%s).', $request->getParameter('id')));
$this->form = new membersForm($group);
}

public function executeUpdate(sfWebRequest $request)
{
$this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
$this->forward404Unless($group = Doctrine::getTable('group')->find(array($request->getParameter('id'))), sprintf('Object group does not exist (%s).', $request->getParameter('id')));
$this->form = new membersForm($group);

$this->processForm($request, $this->form);

$this->setTemplate('edit');
}

public function executeDelete(sfWebRequest $request)
{
$request->checkCSRFProtection();

$this->forward404Unless($group = Doctrine::getTable('group')->find(array($request->getParameter('id'))), sprintf('Object group does not exist (%s).', $request->getParameter('id')));
$group->delete();

$this->redirect('new/index');
}

protected function processForm(sfWebRequest $request, sfForm $form)
{
$form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
if ($form->isValid())
{
$group = $form->save();

$this->redirect('new/edit?id='.$group->getId());
}
}
}


José Antonio comments:

Well before all, you should know a couple of things about the images....
You can store you web applications images in the web server, in a repository or in a database. If you wanna do the last one then you can used a BLOB fieldtype to store the image's content. For this reason you should read the image uploaded to the server for the user, open it using <strong>$handler = fopen($url)</strong> and the read it's content; for that I recommend <strong> $stream = stream_get_content($handler)</strong>. The $stream variable you can pass to the setter method in the "ImageTable" object.

$handler = fopen($url);
$stream = stream_get_content($handler)
$image = new ImageTable();
$image->setImage($stream);
...
$iamge->save();

Once you do that, then can you retrieve the image with the Doctrine objects collector. When you have the object then you should save the content of the blob field into a file, and with its you can show you in the template.

$image= Doctrine::getTable('image_table')->findOneBy('id',$request->getParameter('id'));
if(count($image)>0){
$this->image = sfConfig::get('sf_upload_dir').DIRECTORY_SEPARATOR.$image->getStudId().'.'.$image->getExtention();
$stream = fopen($this->image,'wb');
fwrite($stream, $image->getImage());
fclose($stream);
}

In the template you can access to the var $image and this should have the file URL.

<img src="<?php echo $image;?>" />

I test it in Propel, in Doctrine should work fine.