2014-02-14 113 views
0

我有一个类用户和文档的应用程序,每个用户有一个图片配置文件(文档)它工作正常。 但现在我有问题,例如在网页的标头,我显示图片:Symfony展示图片扩展

{% if app.user.picture %} 
<img src="{{ asset('uploads/' ~ app.user.picture.Id) }}" alt="" class="img-circle"> 
{% else %} 
<img src="{{ asset('images/default.png') }}" alt="" class="img-circle"> 
{% endif %} 

,我得到了html代码:

<img src="/Project/web/uploads/30" alt="" class="img-circle"> 

此代码的工作完美的在我的本地,但现在我上传到服务器和图片不显示。 但是,如果我在浏览器上添加扩展它显示我到底想要什么。 这里是我的文档实体:

use Doctrine\ORM\Mapping as ORM; 
use Symfony\Component\Validator\Constraints as Assert; 
use Symfony\Component\HttpFoundation\File\UploadedFile; 

/** 
* @ORM\Entity 
* @ORM\HasLifecycleCallbacks 
*/ 
class Document { 

    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    public $id; 

    /** 
    * @ORM\Column(type="string", length=255) 
    * @Assert\NotBlank 
    */ 
    public $name; 

    /** 
    * @ORM\Column(type="datetime") 
    */ 
    protected $createdAt; 

    /** 
    * @ORM\Column(type="string", length=255, nullable=true) 
    */ 
    public $path; 

    /** 
    * @Assert\File(maxSize="6000000") 
    */ 
    private $file; 
    private $temp; 

    public function __construct() { 
     $this->setCreatedAt(new \DateTime()); 
    } 

    public function getId() { 
     return $this->id; 
    } 

    public function getName() { 
     return $this->name; 
    } 

    public function setName($n) { 
     $this->name = $n; 
    } 

    public function getCreatedAt() { 
     return $this->createdAt; 
    } 

    public function setCreatedAt($d) { 
     $this->createdAt = $d; 
    } 

    public function setPath() { 
     $this->path = $this->id; 
    } 

    public function getPath() { 
     return $this->path; 
    } 

    /** 
    * Sets file. 
    * 
    * @param UploadedFile $file 
    */ 
    public function setFile(UploadedFile $file = null) { 
     $this->file = $file; 
     // check if we have an old image path 
     if (isset($this->path)) { 
      // store the old name to delete after the update 
      $this->temp = $this->path; 
      $this->path = null; 
     } else { 
      $this->path = 'initial'; 
     } 
    } 

    /** 
    * Get file. 
    * 
    * @return UploadedFile 
    */ 
    public function getFile() { 
     return $this->file; 
    } 

    public function getAbsolutePath() { 
     return null === $this->path ? null : $this->getUploadRootDir() . '/' . $this->id . '.' . $this->path; 
    } 

    public function getWebPath() { 
     return null === $this->path ? null : $this->getUploadDir() . '/' . $this->path; 
    } 

    protected function getUploadRootDir() { 
     // the absolute directory path where uploaded 
     // documents should be saved 
     return __DIR__ . '/../../../../../web/' . $this->getUploadDir(); 
    } 

    protected function getUploadDir() { 
     // get rid of the __DIR__ so it doesn't screw up 
     // when displaying uploaded doc/image in the view. 
     return 'uploads/'; 
    } 

    /** 
    * @ORM\PrePersist() 
    * @ORM\PreUpdate() 
    */ 
    public function preUpload() { 
     if (null !== $this->getFile()) { 
      $this->path = $this->getFile()->guessExtension(); 
     } 
    } 

    /** 
    * @ORM\PostPersist() 
    * @ORM\PostUpdate() 
    */ 
    public function upload() { 
     if (null === $this->getFile()) { 
      return; 
     } 
     // check if we have an old image 
     if (isset($this->temp)) { 
      // delete the old image 
      unlink($this->temp); 
      // clear the temp image path 
      $this->temp = null; 
     } 

     // you must throw an exception here if the file cannot be moved 
     // so that the entity is not persisted to the database 
     // which the UploadedFile move() method does 

     $this->getFile()->move(
       $this->getUploadRootDir(), $this->id . '.' . $this->getFile()->guessExtension() 
     ); 
     $this->setFile(null); 
    } 

    /** 
    * @ORM\PreRemove() 
    */ 
    public function storeFilenameForRemove() { 
     $this->temp = $this->getAbsolutePath(); 
    } 

    /** 
    * @ORM\PostRemove() 
    */ 
    public function removeUpload() { 
     if (isset($this->temp)) { 
      unlink($this->temp); 
     } 
    } 

} 

而且我Document.orm.yml:

...\Entity\Document: 
    type: entity 
    table: null 
    repositoryClass: ...\Entity\DocumentRepository 
    fields: 
     id: 
      type: integer 
      id: true 
      generator: 
       strategy: AUTO 
     name: 
      type: string 
      length: '255' 
     path: 
      type: string 
      length: '255' 
      nullable: true 
     file: 
      type: string 
      length: '255' 
    lifecycleCallbacks: { } 

在我的帐户控制:

public function editAction(Request $request) { 
     $em = $this->getDoctrine()->getManager(); 
     $entity = $em->getRepository('...Bundle:User')->find($this->getUser()->getId()); 

     $originalpic = $entity->getPicture(); 
     $editForm = $this->createEditForm($entity); 
     $editForm->handleRequest($request); 

     $uploadedFile = $request->files->get('form_user'); 

     if ($editForm->isValid()) { 

      if ($editForm->get('password')->getData()) { 
       $factory = $this->get('security.encoder_factory'); 
       $encoder = $factory->getEncoder($entity); 
       $entity->setPassword($encoder->encodePassword($editForm->get('password')->getData(), $entity->getSalt())); 
      } 

      if ($uploadedFile['picture']) { 
       foreach ($uploadedFile as $file) { 
        $pic = new Document(); 
        $pic->setName($entity->getUsername()); 
        $pic->setFile($file); 
        $pic->setPath(); 
        $entity->setPicture($pic); 
        $em->persist($pic); 
        $em->flush(); 
        $pic->upload(); 
       } 
      } else { 
       $entity->setPicture($originalpic); 
      } 


      $em->flush(); 

      $this->container->get("session")->getFlashBag()->add('success', 'Information updated'); 
      return $this->redirect($this->generateUrl('panel_profile')); 
     } 

     return $this->render('...Bundle:Panel\Pages\Account:edit.html.twig', array(
        'entity' => $entity, 
        'edit_form' => $editForm->createView() 
     )); 
    } 

我的问题是,怎样才能改变我代码向我展示带有扩展名的图片 ///// 该解决方案为表格和文档实体添加了另一种原料:

public function upload() { 
     if (null === $this->getFile()) { 
      return; 
     } 
     // check if we have an old image 
     if (isset($this->temp)) { 
      // delete the old image 
      unlink($this->temp); 
      // clear the temp image path 
      $this->temp = null; 
     } 

     // you must throw an exception here if the file cannot be moved 
     // so that the entity is not persisted to the database 
     // which the UploadedFile move() method does 

     $this->extension = $this->getFile()->guessExtension(); 
     $this->getFile()->move(
       $this->getUploadRootDir(), $this->id . '.' . $this->getFile()->guessExtension() 
     ); 

     $this->setFile(null); 
    } 

回答

1
/** 
* @ORM\Column(type="string", length=255, nullable=true) 
*/ 
protected $extension; 


//get image extension 
    public function getImgExtension(){ 
    return $this->getFile()->getClientOriginalExtension(); 

} 
/** 
* Set path 
* 
* @param string $extension 
* @return Document 
*/ 
public function setExtension($extension) 
{ 
    $this->extension = $extension; 

    return $this; 
} 

/** 
* Get path 
* 
* @return string 
*/ 
public function getExtension() 
{ 
    return $this->extension; 
} 

获得图像的扩展,在你的文档实体添加该属性和方法。 然后从终端运行此命令:

php app/console doctrine:schema:update --force 

并在树枝文件只是包括:

<img src="{{asset(document.WebPath)}}.{{document.extension}}" alt="" class="img-circle"> 
+0

我添加到文档实体:/ ** * @ORM \柱(类型=“字符串“,length = 255,nullable = true) */ protected $ extension; public function getExtension(){ return $ this-> getFile() - > getClientOriginalExtension(); } 但我有一个成员函数调用getClientOriginalExtension()在非对象 当我尝试做:{{app.user.picture.extension}} – bfmr89

+0

在这里放置您的控制器代码。 – hizbul25

+0

我没有为此创建控制器,是base.html.twig,我这样做:{{app.user.picture.extension}} – bfmr89