使用Intervention包来压缩图像。我的图像没有被压缩,仍然是相同的大小

时间:2018-05-20 06:41:11

标签: php laravel laravel-5 intervention

我正在使用此程序包http://image.intervention.io/getting_started/installation来压缩上传到我的服务器的图像。但是图像没有被压缩。

  1. 首先我安装了干预套餐,把它放在我的 终端:

      

    作曲家需要干预/图像

  2. 然后我在控制器顶部添加了这个:

      

    使用Intervention \ Image \ ImageManagerStatic作为图像;

  3. 然后我添加了编码以缩小图像

      

    Image :: make(request() - > file('img')) - > encode('jpg',1);

  4. 这不会缩小图像。它仍然是相同的大小。

    <?php
    
    namespace App\Http\Controllers;
    
    use Intervention\Image\ImageManagerStatic as Image;
    use Illuminate\Support\Facades\Storage;
    use Illuminate\Http\Request;
    
    class UploadsController extends Controller
    {
    
        public function store()
        {
    
            // Get image
            $img = request()->file('img');
    
            // Minify image
            Image::make($img)->encode('jpg', 1);
    
            // Store image in uploads folder
            Storage::disk('public')->put('uploads', $img);
    
        }
    
    }
    

2 个答案:

答案 0 :(得分:1)

很简单:

  

质量仅在您编码JPG格式时应用,因为PNG压缩是无损的并且不会影响图像质量。默认值:90。

如果您希望此库压缩图片,请使用jpg

答案 1 :(得分:0)

好像您只是保存原始图像,而不是调整大小的版本。您可以尝试这样的事情:

public function store()
{

    // Get image
    $img = request()->file('img');

    // Minify image
    $resizedImage = Image::make($img)->encode('jpg', 1); // put this in a variable

   // use a unique filename
   $filename = $img->hashName()

    // Store image in uploads folder
    Storage::disk('public')->put('uploads/'.$filename, $resizedImage->__toString());

}
相关问题