I'm trying to reduce the size of the images uploaded by users on a project I'm working on. The main idea is to resize the image down by 10%, and checking if its size is under a set requirement.
Here is my code: ($optimisedImage is the original image uploaded by the user and recieved from the request)
$optimisedImage->save("user_uploads/" . $scrollFeedName);
$scrollFeedImg = Image::make("user_uploads/" . $scrollFeedName);
while(filesize("user_uploads/" . $scrollFeedName) > 1000000) {
$scrollFeedImg->resize(floor($scrollFeedImg->width() * 0.9), floor($scrollFeedImg->height() * 0.9));
$scrollFeedImg->save();
}
Currently there seems to be an issue with the loop, getting stuck. I also tried the Intervention Image filesize(), but was getting an error stating that the width or heigth need to be higher than 0.
Is there any way to achieve my goal, or are there better solutions to that problem?
Stack:
- PHP: 8.1.10
- Laravel: 9.19
Step 1: Install intervention for resize image
Step 2: Add provider path and alias path in
config/app.phpStep 3: Resize Image code for controller