【问题标题】:VichUploader and LiipImagine : resize and save the uploaded imageVichUploader 和 LiipImagine :调整大小并保存上传的图像
【发布时间】:2019-11-20 06:32:19
【问题描述】:

我正在做一个 symfony 项目,我正在使用 vichuploader 和 liipImagine,所以我想调整上传图片的大小并将其(调整后的图片)保存在上传目录中,以便用户上传图片到 10Mb,然后调整它的大小以保持 100 KB 的大小。

到目前为止,我发现使用imagine_filter 来调整上传图像的大小,但在这种情况下,它会保留原始图像,并且在缓存中创建调整大小的图像。

所以我试图做这样的事情: 在我的控制器中

public function edit(Post $post,Request $request)
    {
        $form = $this->createForm(PostType::class, $post);
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            $this->em->flush();
            // And then resize all uploaded images...
            // All i know is to get the filename of each image stored in public/media/posts.
            // In loop because we allow multiple upload 
            // foreach ($post->getPictures() as $picture) {
            //   $picture->getFilename();
            // }
            $this->addFlash('success', 'ELement successfully modified');
            return $this->redirectToRoute('admin.post.index');
        }
        return $this->render('admin/post/edit.html.twig', [
            'post' => $post,
            'form' => $form->createView()
        ]);
    }

我的帖子类型

class PostType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('title')
            ->add('category', EntityType::class, [
                'class' => Category::class,
                'required' => true,
                'choice_label' => 'title'
            ])
            ->add('pictureFiles', FileType::class, [
                'required' => false,
                'multiple' => true
            ])
            ->add('content')
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Post::class,
        ]);
    }
}

感谢您的帮助!

【问题讨论】:

    标签: symfony vichuploaderbundle liipimaginebundle


    【解决方案1】:

    由于找不到任何建议,我决定使用 InterventionImage。我知道这不是最好的做法,但是当你没有得到你需要的东西时,你只是用你所拥有的东西来满足自己。 正如我上面所说,目标是在服务器上保留最小的图像并允许用户上传大图像。 对于那些可能感兴趣的人,我添加了这个

    public function edit(Post $post,Request $request)
        {
            $form = $this->createForm(PostType::class, $post);
            $form->handleRequest($request);
            if ($form->isSubmitted() && $form->isValid()) {
                $this->em->flush();
                $pics = [];
                foreach ($post->getPictures() as $picture) {
                    // this path of the image
                    $targetPath = 'media/posts/' .  $picture->getFileName();
                    // and then resize it
                    $this->resizeImage($targetPath);
                }
                dump($pics);
                $this->addFlash('success', 'ELement successfully modified');
                return $this->redirectToRoute('admin.post.index');
            }
            return $this->render('admin/post/edit.html.twig', [
                'post' => $post,
                'form' => $form->createView()
            ]);
        }
    
        private function resizeImage($targetPath)
        {
            $manager = new ImageManager(['driver' => 'gd']);
            $manager->make($targetPath)->widen(768, function ($constraint) {
                $constraint->upsize();
            })->save($targetPath);
        }
    

    如果有其他想法可以改进这一点,我会很细心, 谢谢:)

    【讨论】:

      猜你喜欢
      • 2011-10-24
      • 1970-01-01
      • 1970-01-01
      • 2013-11-20
      • 1970-01-01
      • 1970-01-01
      • 2012-01-10
      • 1970-01-01
      • 2011-10-24
      相关资源
      最近更新 更多