Laravel 5.1干预图像压缩

时间:2018-12-11 18:25:22

标签: laravel laravel-5.1

我使用Laravel 5.1和干预软件包: http://image.intervention.io/

我尝试压缩我的图像。我想上传一个2.5Mb的图像,并通过保持分辨率和图像质量将图像大小减小到大约700kb来对其进行压缩。只需将大图像文件缩小为小文件即可。 上传的图片是.jpg文件

我使用以下代码:

2018-12-11 13:30:01.538664: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
Traceback (most recent call last):
  File "Test_Sparse.py", line 15, in <module>
    x: (indices, values, shape)}))
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 929, in run
    run_metadata_ptr)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1128, in _run
    str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (2, 2) for Tensor u'Placeholder_1:0', which has shape '(?, 3)'

但是我的图像大小仍然约为2.07Mb

问题是我必须使用哪种方法才能获得理想的结果?

1 个答案:

答案 0 :(得分:0)

您已压缩或缩小图像大小,这将对您更好,对我也将在压缩图像后提供最佳分辨率。

$path = public_path().'/public_thumb_photo/';

        File::makeDirectory($path, $mode = 0777, true, true);

        $extension=$request->file('thumb')->getClientOriginalExtension();

        $filename=$request->file('thumb')->getClientOriginalName();

        $imgname ='public_thumb_photo_'.mt_rand(000000,999999) . '.'.$extension;

        $request->file('thumb')->move($path,$imgname);

        $data = getimagesize($path.$imgname);
                    $widths = $data[0];
                    $heights = $data[1];

                     if($widths > 1000 && $heights > 1000)
                    {
                        //dd("demo1");
                        if($widths > 3000 && $heights > 3000)
                        {
                            //dd("demo123");
                            $width = $widths - 1000;    
                            $height = $heights - 1000;
                        }
                        else
                        {
                            //dd("demo 2");
                            $width = $widths - 500;
                            $height = $heights - 500;
                        }

                    }
                    else
                    {
                        //dd("demo 3");
                        $width = $widths - 250;
                        $height = $heights - 250;
                    }


                    Image::make($path.$imgname)->resize($width,$height,function ($constraint) {
                        $constraint->aspectRatio();
                    })->save($path.$imgname,20);