简体   繁体   English

如何使用 Laravel 在干预图像包中设置图像大小和尺寸

[英]How to set image size and dimension in Intervention Image package using Laravel

I'm using the Intervention Image package for manipulating the uploaded image before saving it to the server.我正在使用干预图像包来操作上传的图像,然后再将其保存到服务器。 I have successfully set the dimension of the image, and it's working fine, but I want to fix the image size, ie, the image generated should be less than equals to 30KB and should have a resolution of 300 dpi only.我已经成功设置了图像的尺寸,它工作正常,但我想修复图像大小,即生成的图像应小于等于30KB,并且分辨率仅为300 dpi。

Here is the code, I'm using to resize the uploaded image:这是代码,我用来调整上传图像的大小:

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

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

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

If my concern is feasible with the Intervention Image package, please give me a solution.如果我的担忧对干预图像包可行,请给我一个解决方案。

Thanks in advance.提前致谢。

you can try somthing like this你可以尝试这样的事情

$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);
}

NOTE this is not correct answer but it is idea to solve this problem注意这不是正确的答案,但解决这个问题是个好主意

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM