approached / laravel-image-optimizer

[deprecated] Image optimizer for laravel
MIT License
152 stars 33 forks source link

jpegoptim options not running #13

Closed travoliti closed 7 years ago

travoliti commented 8 years ago

I am using jpegoptim. Here is my config:

    'options' => [
        'ignore_errors' => false,

        'optipng_bin' => '/usr/bin/optipng',
        'optipng_options' => ['-force'],

        'gifsicle_bin'     => '/usr/bin/gifsicle',
        'gifsicle_options' => ['-b', '-O5'],

        'jpegoptim_bin'     => '/usr/bin/jpegoptim',
        'jpegoptim_options' => ['-m80'],
    ],

    'transform_handler' => [
        'png'  => 'optipng',
        'jpg'  => 'jpegoptim',
        'jpeg' => 'jpegoptim',
        'gif'  => 'gifsicle',
    ],

But when I run the code, the package compresses it using lossless compression (instead of lossy compression (-m80) like I set in the config:

$pic = public_path('landscape.jpg');
$imageOptimizer = new ImageOptimizer();
$imageOptimizer->optimizeImage($pic);

But when I run the command in the command line, it works fine:

jpegoptim -m80 landscape.jpg

I only have jpegoptim and not jpegtran.

What am I doing wrong?

approached commented 8 years ago

Hi @travoliti

What is the error? Is that just the parameter -m80?

Try to debug this package to find the excute command.

travoliti commented 8 years ago

@approached

Okay, I think I know what's going on, but I don't know why it's happening.

This is the original file size of the image: 2,214 KB When I manually use jpegoptim -m80 landscape.jpg in command line, the file size is: 1,030 KB When I run it through your package, the file size is: 2,085 KB

I found out that the default options for jpegoptim for the psliwa/image-optimizer package are:

array('--strip-all', '--all-progressive')

So I tried running these options manually in the command line (jpegoptim --strip-all --all-progressive landscape.jpg) to see if I got 2,085 KB (like your package did), and I got exactly 2,085 KB.

So the question is, why is it ignoring my jpegoptim options set in the config?

travoliti commented 8 years ago

Any ideas?

approached commented 8 years ago

I added a test only just for you: https://github.com/approached/laravel-image-optimizer/commit/71bd597a0e048c4c87fc69b3d4314e5fa58af3c9

Result with --strip-all:

File: /home/kloos/workspace/laravel-image-optimizer/tests/files/testimage.jpg
Size: 197116
File: /tmp/php_image_optimizer.jpg
Size: 171662
Saved: 25454
Saved in percent: 13%

Result with -m80:

File: /home/kloos/workspace/laravel-image-optimizer/tests/files/testimage.jpg
Size: 197116
File: /tmp/m80.jpg
Size: 22768
Saved: 174348
Saved in percent: 88%

So you can sse, it works well on my machine. Which version are you use for jpegoptim?

travoliti commented 8 years ago

I'm running jpegoptim version 1.4.4.

http://i.imgur.com/7EzsOdN.png

approached commented 8 years ago

No idea. But you can try to run the tests.

Reference: https://travis-ci.org/approached/laravel-image-optimizer/jobs/176700955

travoliti commented 8 years ago

I tried it again under a different Laravel project, and I still can't get it to work. It seems to just be ignoring my config and using the config options used in the psliwa/image-optimizer package.

approached commented 8 years ago

Can you debug your debug your config file:

dd(
     config('imageoptimizer')
);
travoliti commented 7 years ago

Output looks fine to me:

array:3 [▼
  "options" => array:7 [▼
    "ignore_errors" => false
    "optipng_bin" => "/usr/local/bin/optipng"
    "optipng_options" => array:3 [▼
      0 => "-o2"
    ]
    "gifsicle_bin" => "/usr/bin/gifsicle"
    "gifsicle_options" => array:2 [▼
      0 => "-b"
      1 => "-O5"
    ]
    "jpegoptim_bin" => "/usr/local/bin/jpegoptim"
    "jpegoptim_options" => array:2 [▼
      0 => "-m80"
      1 => "--all-progressive"
    ]
  ]
  "transform_handler" => array:4 [▼
    "png" => "optipng"
    "jpg" => "jpegoptim"
    "jpeg" => "jpegoptim"
    "gif" => "gifsicle"
  ]
  "log_file" => "/var/www/project/storage/logs/image_optimize.log"
]
travoliti commented 7 years ago

@approached

Could it have anything to do with one of these issues?

https://github.com/psliwa/image-optimizer/issues?utf8=%E2%9C%93&q=is%3Aissue%20jpegoptim

approached commented 7 years ago

I have no idea, why only you has this problem. I cannot reproduce it.

Okipa commented 7 years ago

+1 here ! I try to override the default params by adding a --max=60 option in the config, as @travoliti tried to do, but my images are compressed with the --strip-all and --all-progressive options for jpegoptim. This is strange, it seems that the config values are not used ...

Okipa commented 7 years ago

@approached, I just did a test. in my config file, I have set the following options :

/*
     |--------------------------------------------------------------------------
     | Options for image transforming
     |--------------------------------------------------------------------------
     |
     | Bin path you can check easy with follow command in a shell:
     | which optipng
     |
     */
    'options'           => [
        'ignore_errors' => false,

        'optipng_bin'     => env('OPTIPNG', '/usr/bin/optipng'),
        'optipng_options' => ['-i0', '-o7', '-quiet', '-strip all', '-preserve'],

        'gifsicle_bin'     => env('GIFSICLE', '/usr/bin/gifsicle'),
        'gifsicle_options' => ['-b', '-O5'],

        'jpegoptim_bin'     => env('JPEGOPTIM', '/usr/bin/jpegoptim'),
        'jpegoptim_options' => ['--strip-all', '--all-progressive', '--quiet'],
    ],

    /*
     |--------------------------------------------------------------------------
     | Transformer for image
     |--------------------------------------------------------------------------
     |
     | You can choice which tranformer you will use
     |
     */
    'transform_handler' => [
        'png'  => 'optipng',
        'jpg'  => 'jpegoptim',
        'jpeg' => 'jpegoptim',
        'gif'  => 'gifsicle',
    ],

I made a dump of the executed commande in the execute() function from the vendor/ps/image-optimizer/src/ImageOptimizer/Command.php that is used when we launch the image optimization. For the png optimization, I got the following command :

/usr/bin/optipng '-i0' '-o2' '-quiet' '/home/vagrant/workspace/project/storage/app/settings/image.png' 1> /dev/null 2> /dev/null

For the jpg optimization, I got the following commande :

/usr/bin/jpegoptim '--strip-all' '--all-progressive' '/home/vagrant/workspace/project/storage/app/users/image.jpg' 1> /dev/null 2> /dev/null

As you can see, the personal config file is currently ignored : my custom options are not taken. Would you have an idea about what might cause this problem and how to fix this ?

EDIT : in fact, it seems that your own default config is not even used. I noticed that you set the --strip-all option only for jpegoptim and in the executed script, the --strip-alland the --all-progressive ones are added. The custom config seems not being transmitted at all to the ps/image-optimizer package you use.

Okipa commented 7 years ago

It seems that a closed issue was already reporting this problem : https://github.com/approached/laravel-image-optimizer/issues/4

Okipa commented 7 years ago

Ok, I think I figured what went wrong. I am using your package in a helper and not directly in a controller. Moreover in an old fashion way, because I still call your package this way, as it was advised at the time :

$opt = new ImageOptimizer();
$opt->optimizeImage($image_path, $extension);

I realized that if the config was not passed to the ps/image-optimizer package you use, that was because you now call your package using the Laravel IOC pattern and not this way anymore. I assume that you load the custom config file at this moment. In order to load the config using the old fashion way, I just had to add the config in the object instantiation.

$opt = new ImageOptimizer(config('imageoptimizer.options'));
$opt->optimizeImage($image_path, $extension);

Hopping it will help others.

approached commented 7 years ago

@Okipa or use the old-new way like this:

$opt = app('Approached\LaravelImageOptimizer\ImageOptimizer'); $opt->optimizeImage($image_path, $extension);

Okipa commented 7 years ago

You're right, much better !

Okipa commented 7 years ago

I think you can close the issue by the way ;)