Answer the question
In order to leave comments, you need to log in
How to optimize image quality in Laravel?
Hello, colleagues) Laravel site, I use Intervention to save images in Storage. The problem is the loss of quality. How can you improve the quality by sacrificing memory and image size?
Here is a screenshot of how the pictures look on the site.
And here is the code itself:
foreach ($images as $image) {
$fileName = time() . '.' . $image->getClientOriginalExtension();
$destination_path = 'public/images/posts';
$img = Image::make($image->getRealPath());
$img->stream();
Storage::disk('local')->put($destination_path . '/' . $fileName, $img, 'public');
$postImage = new \App\Image;
$postImage->path = $fileName;
$postImage->post_id = $post->id;
$postImage->post()->associate($post);
$postImage->save();
}
Answer the question
In order to leave comments, you need to log in
Briefly describe what was the problem I had. When uploading images, I did not change the original size, while displaying the image should occupy 100% of the block width, which is ~ 600px. Therefore, when loading images with an extension of 300:400, it remained that way, and then expanded to 600px, which caused the quality to suffer greatly. Solved the problem with these lines:
$img->resize(650, null, function ($constraint) {
$constraint->aspectRatio();
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question