【问题标题】:Customer Image Profile客户形象资料
【发布时间】:2017-04-12 14:07:57
【问题描述】:

有没有简单的方法来处理用户个人资料图片?

我尝试使用VichUploaderBundle 没有成功。

【问题讨论】:

  • 显示您尝试过的(实施)以及您遇到的问题。要改进您的问题,请阅读How do I ask a good question? 并相应地更新您的问题。
  • 当然,我关注installation procedure 没有任何问题,我想使用yml mapping。这对学说:模式:更新没有影响。如果有必要,我可以附加我的配置
  • 是的,添加你的代码看看你是如何实现的

标签: symfony sylius vichuploaderbundle


【解决方案1】:

以唯一的名称将它们保存在您的文件夹中,并将名称存储在数据库中。

要上传图像,您可以在表单中使用 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 时,你以类似的方式在 twig 中显示它,首先你使用 $user->getPhoto() 读取文件名并将其发送到你的模板,当我调用 user.模板中的getPhoto(),也许就我一个人:

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

【讨论】:

    猜你喜欢
    • 2013-05-02
    • 1970-01-01
    • 2011-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-17
    • 2012-11-13
    • 2016-01-05
    相关资源
    最近更新 更多