Use LiipImagineBundle to Resize Image after Upload?

后端 未结 5 451
梦毁少年i
梦毁少年i 2020-12-02 21:08

I\'m using the LiipImagineBundle with Symfony 2.1 and would like to resize user uploaded images upon upload before saving them to the permanent filesystem location (to strip

5条回答
  •  清歌不尽
    2020-12-02 21:48

    Instead of loading the file using liip data manager, create liip binary object from the uploaded file:

    use Liip\ImagineBundle\Model\Binary;
    

    then use the following code:

                    // Generate a unique name for the file before saving it
                    $fileName = md5(uniqid()) . '.' . $uploadedFile->guessExtension();
    
                    $contents = file_get_contents($uploadedFile);
    
                    $binary = new Binary(
                        $contents,
                        $uploadedFile->getMimeType(),
                        $uploadedFile->guessExtension()
                    );
    
                    $container = $this->container;
                    $filterManager = $container->get('liip_imagine.filter.manager');    // the filter manager service
                    $response = $filterManager->applyFilter($binary, 'my_thumb');
    
                    $thumb = $response->getContent();                               // get the image from the response
    
                    $f = fopen($webRootDir .'/images_dir/' . $fileName, 'w');                                        // create thumbnail file
                    fwrite($f, $thumb);                                             // write the thumbnail
                    fclose($f);                                                     // close the file
    

提交回复
热议问题