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

Custom SVGO optimizer not running as expexted with symfony/process 3.4.4 #45

Closed
fiskhandlarn opened this issue Feb 21, 2018 · 4 comments

Comments

@fiskhandlarn
Copy link

fiskhandlarn commented Feb 21, 2018

I've created a custom SVGO optimizer:

<?php

namespace SotPlate\Optimizers;

use Spatie\ImageOptimizer\Image;
use Spatie\ImageOptimizer\Optimizers\Svgo;

class CustomSvgo extends Svgo
{
    public $binaryName = 'npm run svgo --';

    public $options = [
        '--disable=cleanupIDs',
        '--enable=removeTitle',
        '--disable=removeStyleElement', // some colored logos rely on style-tag
        '--enable=removeScriptElement',
        '--enable=removeEmptyContainers',
    ];

    public function __construct($options = [])
    {
        $this->setOptions($this->options);
    }

    public function canHandle(Image $image): bool
    {
        var_dump($image->extension());
        var_dump($image->mime());
        if ($image->extension() !== 'svg' && $image->extension() !== 'tmp') {
            return false;
        }

        return $image->mime() === 'text/html' || $image->mime() === 'image/svg+xml';
    }
}

... which I'm triggering in Wordpress like this:

use Spatie\ImageOptimizer\OptimizerChain;
use SotPlate\Optimizers\CustomSvgo;
add_filter('wp_handle_upload_prefilter', function ( $file ) {
    if ($file['type'] !== 'image/svg+xml' ) {
        return $file;
    }

    // run svgo on uploaded file
    $svgo = new CustomSvgo();
    $optimizerChain =
                    (new OptimizerChain)
                    ->addOptimizer($svgo)
                    ->optimize($file["tmp_name"]);

    return $file;
});

With this added to my package.json:

{
    "scripts": {
        "svgo": "cross-env NODE_ENV=production node_modules/svgo/bin/svgo"
    }
}

I'm using this in my boilerplate for most of my new projects. Recently this stopped working (i.e. the svgo command is never run, or at least the optimized results are not saved back to the uploaded file).
spatie/image-optimizer version is the same, but the symfony/process versions aren't. The above works with symfony/process version 3.4.0 but not with 3.4.4.

The only difference in output when dumping $process last in OptimizerChain::applyOptimizer() seems to be

object(Symfony\Component\Process\Process)#7166 (29) {
  ["processPipes":"Symfony\Component\Process\Process":private]=>
  object(Symfony\Component\Process\Pipes\WindowsPipes)#7168 (8) {
    ["files":"Symfony\Component\Process\Pipes\WindowsPipes":private]=>
    array(2) {
      [1]=>
      string(30) "C:\WINDOWS\TEMP\sf_proc_00.out"
      [2]=>
      string(30) "C:\WINDOWS\TEMP\sf_proc_00.err"
    }
}

when working, and

object(Symfony\Component\Process\Process)#7166 (29) {
  ["processPipes":"Symfony\Component\Process\Process":private]=>
  object(Symfony\Component\Process\Pipes\WindowsPipes)#7168 (8) {
    ["readBytes":"Symfony\Component\Process\Pipes\WindowsPipes":private]=>
    array(2) {
      [1]=>
      int(0)
      [2]=>
      int(1424)
    }
}

when not working. (I'm omitting identical output for the two cases of $process above.)

I'm guessing of course that is an issue with symfony/process and not this package, but my knowledge of these two packages are limited, so I'm asking here first hoping someone at least can point me in the right direction. :)

I'm running Windows 10 and Apache for the above. I haven't tested this with the regular Spatie\ImageOptimizer\Optimizers\Svgo, but I'm guessing the outcome will be the same.

@fiskhandlarn
Copy link
Author

I temporarily fixed this by forcing symfony/process to 3.4.0 in my main composer.json by adding this:

{
    "require": {
        "symfony/process": "3.4.0"
    }
}

@freekmurze
Copy link
Member

Closing this as this is probably an issue on process.

@fiskhandlarn
Copy link
Author

@freekmurze It probably is, but will they understand if I post this as an issue in their project? If not, how do you think I should proceed to remedy this issue?

@fiskhandlarn
Copy link
Author

@freekmurze Please advise regarding the above :)

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

2 participants