Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jpegoptim options not running #13

Closed
travoliti opened this issue Nov 5, 2016 · 18 comments
Closed

jpegoptim options not running #13

travoliti opened this issue Nov 5, 2016 · 18 comments

Comments

@travoliti
Copy link

travoliti commented Nov 5, 2016

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
Copy link
Owner

Hi @travoliti

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

Try to debug this package to find the excute command.

@travoliti
Copy link
Author

travoliti commented Nov 14, 2016

@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
Copy link
Author

Any ideas?

@approached
Copy link
Owner

I added a test only just for you: 71bd597

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
Copy link
Author

travoliti commented Nov 17, 2016

I'm running jpegoptim version 1.4.4.

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

@approached
Copy link
Owner

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

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

@travoliti
Copy link
Author

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
Copy link
Owner

Can you debug your debug your config file:

dd(
     config('imageoptimizer')
);

@travoliti
Copy link
Author

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
Copy link
Author

travoliti commented Dec 14, 2016

@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
Copy link
Owner

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

@Okipa
Copy link

Okipa commented Feb 12, 2017

+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
Copy link

Okipa commented Mar 9, 2017

@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
Copy link

Okipa commented Mar 9, 2017

It seems that a closed issue was already reporting this problem : #4

@Okipa
Copy link

Okipa commented Mar 11, 2017

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
Copy link
Owner

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

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

@Okipa
Copy link

Okipa commented Mar 13, 2017

You're right, much better !

@Okipa
Copy link

Okipa commented Mar 13, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants