English 中文(简体)
用书目中的博彩图像制作图像
原标题:image gallery using blob images in symfony

我的数据库表中有一张图像作为博览。

我想做的是,在Success名单上显示图像。 php,如一席。

难道有人能提供某种法典,帮助或让我开始?

关于

最佳回答

First of all if you store data in DB do not store it in the one sigle table. Here my example:

schema.yml

File:
  connection: doctrine
  actAs: { Timestampable: ~ }
  columns:
    document_id: { type: integer(5), notnull: true}
    name: string(255)
    size: integer(11)
    mime: string(255)
  relations:
    Document:
      class: Document
      local: document_id
      foreign: id
      type: one
      onDelete: CASCADE
    FileData:
      local: id
      foreign: file_id
      type: one
      foreignType: one


FileData:
  connection: doctrine
  columns:
    file_id: { type: integer(6), notnull: true }
    binary_data: { type: LONGBLOB, notnull: true }
  relations:
    File: { onDelete: CASCADE, local: file_id, foreign: id, foreignType: one }

frontend/modules/file/actions/actions.class.php

<?php

class fileActions extends sfActions
{

  public function executeDownload(sfWebRequest $request)
  {
    $response = $this->getResponse();


    /** @var $file File */
    $file = $this->getRoute()->getObject();
    $this->forward404Unless($file->getDocument()->isApprovedAndShared($this->getUser()));
    $fsize = $file->getSize();

    $response->setContentType($file->getMime());
    $response->setHttpHeader( Content-Length ,$fsize);
    $response->setHttpHeader( Content-Disposition , filename= .$file->getName());
    $response->setHttpHeader( Content-transfer-encoding , binary );

    $response->setContent($file->getFileData()->getBinaryData());

    return sfView::NONE;
  }

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

    /** @var $document Document */
    $document = $this->getRoute()->getObject();
    $this->forward404Unless( $document->getOffice()->getId() == $this->getUser()->getGuardUser()->getOfficeId(), $this->getContext()->getI18N()->__( textAccessForbidden ));

    $form = new FileForm();

    $files = $request->getFiles($form->getName());
    $file_name = $files[ file ][ name ];
    $file_already_exist = FileTable::getInstance()->getFile($document->getId(), $file_name);

    if ($file_already_exist) {
      $form = new FileForm($file_already_exist);
      $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
//      $form->getObject()->setCreatedAt(date("Y-m-d H:i:s", time()));
      $form->getObject()->setUpdatedAt(date("Y-m-d H:i:s", time()));
    }
    else {
      $form = new FileForm();
      $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    }

    if ($form->isValid())
    {
      $form->getObject()->setDocumentId($document->getId());
      $file_result = $form->save();

      if ($file_already_exist)
        $this->getUser()->setFlash( notice , $this->getContext()->getI18N()->__("textFileReplacedOk", array( %s% =>$file_result->getName())));
      else
        $this->getUser()->setFlash( notice , $this->getContext()->getI18N()->__("textFileUploadOk", array( %s% =>$file_result->getName())));

      $this->redirect($this->generateUrl("document_edit",$document));
    }

    $this->getUser()->setFlash( error , $this->getContext()->getI18N()->__("textFileUploadError"));
    $this->forward("document","edit");

  }


  public function executeDelete(sfWebRequest $request)
  {
    /** @var $file File */
    $file = $this->getRoute()->getObject();

    $this->forward404Unless($request->getParameter( document_id ) == $file->getDocumentId() , sprintf( The object doesn t belong to the document (file: %s). , $request->getParameter( id )));
    $this->forward404Unless($file = FileTable::getInstance()->find(array($file->getId() )), sprintf( Object file does not exist (%s). , $request->getParameter( id )));
    $file->delete();

    $this->getUser()->setFlash( notice , sprintf($this->getContext()->getI18N()->__("textFileDeletedOk"),$file->getName()));

    $this->redirect($this->generateUrl( "document_edit",DocumentTable::getInstance()->find(array($file->getDocumentId())) ));
  }

}

fileDataForm.class.php

class FileDataForm extends BaseFileDataForm
{
  public function configure()
  {
    unset(
      $this[ file_id ]
    );        
  }
}

档案馆

class FileForm extends BaseFileForm
{
  public function configure()
  {
    $this->disableCSRFProtection();
    $this->useFields(array());
    $this->widgetSchema[ file ] = new sfWidgetFormInputFile(array());

    $this->setValidator( file , new sfValidatorFile(array(
      max_size  => 1024*1024*sfConfig::get("app_max_file_size", 10) //MB
    )));


  }

  public function save($con = null)
  {

    /** @var $validated_file
     var_dump( $validated_file) */
    $validated_file = $this->getValue( file );

    /** @var $file File */
    $file = $this->getObject();
    $file->setMime($validated_file->getType());
    $file->setName($validated_file->getOriginalName());
    $file->setSize($validated_file->getSize());

    $fileData = new FileData();

    $fd = @fopen($validated_file->getTempName(),  rb );

    $fileData->setBinaryData(fread($fd,$validated_file->getSize()));

    fclose($fd);

    $file->setFileData($fileData);
    unset($this[ file ]); //do not save Validated file: this is a trick :)

    return parent::save($con);
  }
}
问题回答

在你名单上的行动中,收集了适当的图像物体。 在模板中,生成了<代码><img> 标签,注明了另一种行动,即把图像本身从数据库中抹去,然后在设定适当的网站标题后将其产出。

您重新储存数据库中图像的任何具体原因? 更高效地将其储存在磁盘上,有些地方是圆顶,因此你仍然可以控制出入。

<>>>>

一个基本的例子如下。 我在这里只是这样说,可能会有错误。

//actions.class.php
public function executeIndex(sfWebRequest $request) {
  //get the images belonging to the current gallery
  $this->images = Doctrine_Core::getTable("Image")->retrieveByGalleryId($request->getParameter("id"));
}

//indexSuccess.php
<?php foreach ($images as $image): ?>
  <img src="<?php echo url_for("image_show", $image) ?>" alt="<?php echo $image->title ?>" />
<?php endforeach>

//actions.class.php again
public function executeShow(sfWebRequest $request) {
  $image = $this->getRoute()->getObject();
  $this->getResponse()->clearHttpHeaders();
  $this->getResponse()->setContentType($image->mime_type);
  $this->getResponse()->setHttpHeader( Content-Disposition ,  attachment; filename=  . $image->file_name);
  $this->getResponse()->setHttpHeader( Content-length , $image->file_size);
  readfile($image->file_name);
  return sfView::NONE;
}
$profile_picture = base64_encode(stream_get_contents($image->getContent()));
echo  <img src="data:image/jpeg;base64, .$profile_picture. " /> ;




相关问题
Resources for Image Recognition

I am looking for a recommendation for an introduction to image processing algorithms (face and shape recognition, etc.) and wondered if anyone had an good recommendations, either for books, ...

Good reference book for digital image processing? [closed]

I am learning digital image processing on my own and would like recomendations on good reference books. If you know of books to definately stay away from that would be useful as well. Thanks

Python Tesseract can t recognize this font

I have this image: I want to read it to a string using python, which I didn t think would be that hard. I came upon tesseract, and then a wrapper for python scripts using tesseract. So I started ...

What s the quickest way to parallelize code?

I have an image processing routine that I believe could be made very parallel very quickly. Each pixel needs to have roughly 2k operations done on it in a way that doesn t depend on the operations ...

Computing object statistics from the second central moments

I m currently working on writing a version of the MATLAB RegionProps function for GNU Octave. I have most of it implemented, but I m still struggling with the implementation of a few parts. I had ...

Viola-Jones face detection claims 180k features

I ve been implementing an adaptation of Viola-Jones face detection algorithm. The technique relies upon placing a subframe of 24x24 pixels within an image, and subsequently placing rectangular ...

热门标签