symfonyvichuploaderbundleliipimaginebundle

VichUploader and LiipImagine : resize and save the uploaded image


i'm working on a symfony project and i'm using vichuploader and liipImagine, so i want to resize an uploaded image and save it (the resized image) in the upload directory, in order to allow users to upload image up to 10Mb and then resize it to maintain a size of 100 KB.

I have found so far to use imagine_filter to resize the uploaded image but in this case, it keeps the original image and the resized image is created in cache.

So i was trying to do something like this: In my Controller

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()
        ]);
    }

My PostType

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,
        ]);
    }
}

Thanks for helping !


Solution

  • As I couldn't find any suggestion, i decided to use InterventionImage. I Know it's not the best practice, but when you dont get what you need, you just satisfy yourself with what you have. And as i said above, the goal is to keep on server the smallest image and allow users to upload large images. For those who may be interested, i added this

    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);
        }
    

    If there is another idea to improve this, i will be attentive, Thanks :)