【问题标题】:How to set image size and dimension in Intervention Image package using Laravel如何使用 Laravel 在 Intervention Image 包中设置图像大小和尺寸
【发布时间】:2020-11-18 11:26:47
【问题描述】:

我正在使用 Intervention Image 包来处理上传的图像,然后再将其保存到服务器。我已经成功设置了图片的尺寸,并且工作正常,但是我想修复图片的大小,即生成的图片应该小于等于30KB并且应该只有 300 dpi 的分辨率。

这里是代码,我用来调整上传图片的大小:

$file = $post->file('profile_image');

$filename = Carbon::now()->timestamp.'_'.$file->getClientOriginalName();

\Image::make($file->getRealPath())->resize(160, 160)->save('uploads/profile/'.$filename);

如果我对干预图像包的担忧是可行的,请给我一个解决方案。

提前致谢。

【问题讨论】:

    标签: php laravel image laravel-5.8 intervention


    【解决方案1】:

    你可以试试这样的

    $size = 160;
    $img = \Image::make($file->getRealPath())->resize($size, $size)->save('uploads/profile/'.$filename);
    while($img->filesize() < "someAmount"){
        $size = (int)$size - 20; // adjust based on your need
        $img = \Image::make($file->getRealPath())->resize($size, $size)->save('uploads/profile/'.$filename);
    }
    

    注意这不是正确的答案,但它是解决这个问题的想法

    【讨论】:

    • 您好,Kamlesh,感谢您的回复,但如何将图像分辨率设置为 300 dip
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-26
    • 2011-10-14
    • 2017-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多