2017-04-12 59 views
0

有没有简单的方法来处理用户配置文件图像?Customer Image Profile

我试过VichUploaderBundle没有成功。

+1

显示您尝试过的操作(实施)以及您卡住的位置。为了改善您的问题,请阅读[我如何问一个好问题?](http://stackoverflow.com/help/how-to-ask)并相应地更新您的问题。 –

+0

当然,我遵循[安装程序](https://github.com/dustin10/VichUploaderBundle/blob/master/Resources/doc/installation.md)没有任何问题,我想使用[yml映射](https: //github.com/dustin10/VichUploaderBundle/blob/master/Resources/doc/mapping/yaml.md)。这对教义没有影响:schema:update。 如果有必要我可以附加我的配置 –

+1

是的,添加你的代码,看看你是如何实现它 –

回答

0

将它们保存在数据库中具有唯一名称和存储名称的文件夹中。

要上传图片,您可以在窗体中使用FileType字段,其文档为here。您可以使用将ImageType的表单验证我记得,这里是一些代码,将文件保存在文件夹中,并保存的文件名在数据库:

$user = new User(); 
    $form = $this->createForm(UserForm::class, $user); 

    $form->handleRequest($request); 

    if ($form->isSubmitted() && $form->isValid()) { 
     $img = $user->getPhoto(); 

     $fileName = md5(uniqid()).'.'.$img->guessExtension(); 
     $user->setPhoto($fileName); 
     // Move the file to the directory where brochures are stored 
     $img->move(
      $this->getParameter('user_image_directory'), 
      $fileName 
     ); 
     $em = $this->getDoctrine()->getManager(); 
     $em->persist($user); 
     $em->flush(); 
    } 

而当你想要显示的IMG你显示它首先你用$ user-> getPhoto()读取文件名并将它发送到你的模板,当我从模板中调用user.getPhoto()时有一些错误,也许它只是我:

img src="{{ asset('media/users/') }}{{ filename }}"></div>