简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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