【问题标题】:Trying to find a way to resize an image on upload in symfony 5试图找到一种在 symfony 5 中调整上传图像大小的方法
【发布时间】:2020-05-21 22:04:47
【问题描述】:

我试图弄清楚如何在使用 symfony 5 上传时调整图像的大小。我正在寻找一个捆绑包,可以找到干预/图像,但它似乎适用于 laravel,但不适用于 symfony。我访问了许多 Q 和 A,但对 symfony 5 并不清楚。实际上我的文件上传工作正常,但在我必须上传它们之前不调整所有图片的大小会是一种解脱。

你有什么提示吗?

谢谢!

【问题讨论】:

  • 一如既往,从packagist开始。
  • 谢谢,但所有这些软件包都提供了一种使用 twig 自定义过滤器调整图像大小的方法,但我只想在上传之前调整我的图像大小......
  • 如果 就在上传 symfony 在这里无关紧要,它会在浏览器中发生,所以这是一个 javascript 问题。请改用npm
  • if($form->isSubmitted() && $form->isValid()){ $imageFile = $form->get('file')->getData(); if($imageFile) { #上传前不能在这里调整大小??? $imageFileName = $fileUploader->upload($imageFile); $image->setFilename($imageFileName); } #.....persist - 刷新等...
  • Yes.

标签: file-upload image-resizing symfony5


【解决方案1】:

我的问题终于有了一个很好的解决方案。

首先这是我的控制器,它有一个从表单上传图像的方法:

-------------------控制器--------------- ------------

   /**
 * @Route("/admin/image/new", name="admin_image_new")
 */
public function newImage(Request $request, EntityManagerInterface $manager, FileUploader $fileUploader, ImageResizeService $imageResize)
{

    $image = new Image();
    $form = $this->createForm(ImageType::class, $image);
    $form->handleRequest($request);
    
    if($form->isSubmitted() && $form->isValid()){

        $imageFile = $form->get('file')->getData();
       
        if($imageFile) {
            
            $imageFileName = $fileUploader->upload($imageFile);
            $image->setFilename($imageFileName);
           
        }
        
        $manager->persist($image);
        $manager->flush();
        
        $this->addFlash('success', 'Image added successfully !');

        $imageName = $image->getFilename();

        $fullSizeImgWebPath = $fileUploader->getTargetDirectory().'/'.$imageName;

       [$width,$height] = getimagesize($fullSizeImgWebPath);
        
        
        
        if($width > $height){
            $width = 1500;
            $height =  1000;
           // $imageResize->writeThumbnail($fullSizeImgWebPath, 1500, 1000);
        } else if($width == $height){
            $width = 300;
            $height =  300;
            //$imageResize->writeThumbnail($fullSizeImgWebPath, 300, 300);
        } else {  
            $width = 1500;
            $height =  2254;
            //$imageResize->writeThumbnail($fullSizeImgWebPath,1000,1600);
        }

        $imageResize->writeThumbnail($fullSizeImgWebPath, $width, $height);
        return $this->redirectToRoute('admin_image');
       
    }

    
    return $this->render('admin/image/new_image.html.twig', [
        'controller_name' => 'AdminImageController',
        'form'=> $form->createView()
    ]);
}

我创建了一个服务来调整上传图片的大小:

-------------------服务--------------- ------------

class ImageResizeService {

        /**
     * Write a thumbnail image using Imagine
     * 
     * @param string $thumbAbsPath full absolute path to attachment directory e.g. /var/www/project1/images/thumbs/
     */
    public function writeThumbnail($thumbAbsPath, $width, $height) {
        $imagine = new Imagine;
        $image   = $imagine->open($thumbAbsPath);
        $size    = new Box($width, $height);

        $image->thumbnail($size,ImageInterface::THUMBNAIL_OUTBOUND)
            ->save($thumbAbsPath);                                         
    }
}

感谢您的帮助,我希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-11
    • 1970-01-01
    • 2018-05-03
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    相关资源
    最近更新 更多