G
G
GS32019-05-30 14:41:49
Laravel
GS3, 2019-05-30 14:41:49

laravel image optimization (Image-Optimizer)?

I want to tie to the site image compression when loading.
I read and decided to use Image-Optimizer .
Installed. In controller:

use Spatie\LaravelImageOptimizer\Facades\ImageOptimizer;

public function store(Request $request)
    {
        $settings = $this->settings;
        $this->reIndex();

        if ($request->hasfile('filename')) {
            $file = $request->file('filename');
            $name=time().$file->getClientOriginalName();
            $path = $file->move(public_path().'/img/', $name);
            ImageOptimizer::optimize($path);
         }

        $img= new \App\Img;
        $img->filename=$name;
        $img->save();

        return redirect('news')->with('success', 'Новость успешно добавлена!');
    }

In this case, nothing happens, the picture is simply loaded, the original one.
Libraries, as I understand it, are connected automatically:
use Spatie\ImageOptimizer\Optimizers\Svgo;
use Spatie\ImageOptimizer\Optimizers\Optipng;
use Spatie\ImageOptimizer\Optimizers\Gifsicle;
use Spatie\ImageOptimizer\Optimizers\Pngquant;
use Spatie\ImageOptimizer\Optimizers\Jpegoptim;

Or do they still need to be delivered somewhere?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Maxim Fedorov, 2019-05-30
@Maksclub

You must have the necessary extensions for this lib to work, it itself detects (if you do not specify it) a suitable one and compresses
https://github.com/spatie/image-optimizer#optimiza...
For example, jpegoptim should be set for Jpegoptim (in C seems to be written), I also use it
And so - your pipeline is wasted

O
O. J, 2019-05-30
@OrlovEvgeny

Try https://github.com/OrlovEvgeny/TinyJPG
this is a watcher daemon that subscribes to the fs event of the image directory and if a new image appears in the directory, it compresses it. Compressing via php is a little expensive.

A
alexbeglov1989, 2019-08-01
@alexbeglov1989

I'll share my experience of compressing pictures... At first, I had to manually recompress all the pictures through Photoshop. By the way, the most free option (except for the cost of a license for Photoshop). But this process takes a lot of time if there are more than 10-20 pictures on the site. After all, each picture must be manually processed, and then uploaded to the site again. Such a tedious process... Now I use this service - https://optipic.io/ Saves a lot of time) Works by itself - automatically - only 1 time it needs to be connected to the site. Google is happy

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question