Y
Y
Yuri Kulaxyz2019-09-19 22:39:01
PHP
Yuri Kulaxyz, 2019-09-19 22:39:01

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.
5d83d85db7083969940533.png
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

2 answer(s)
Y
Yuri Kulaxyz, 2019-09-20
@Kulaxyz

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

A
Anton R., 2019-09-19
@anton_reut

Have you tried reading the documentation? image.intervention.io/api/save

// save the image jpg format defined by third parameter
$img->save('public/foo', 80, 'jpg');
// 80 это качество джипега

I’m not alo in Lara at all, and I googled it in a minute.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question