Search code examples
phplaravelimagelaravel-5.8intervention

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, i.e., 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.


Solution

  • 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