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

[Post-Processors] Replaced ProcessBuilder with Process and add additional tests #1025

Merged
merged 1 commit into from Feb 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 8 additions & 8 deletions Imagine/Filter/PostProcessor/JpegOptimPostProcessor.php
Expand Up @@ -15,7 +15,7 @@
use Liip\ImagineBundle\Binary\FileBinaryInterface;
use Liip\ImagineBundle\Model\Binary;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\ProcessBuilder;
use Symfony\Component\Process\Process;

class JpegOptimPostProcessor implements PostProcessorInterface, ConfigurablePostProcessorInterface
{
Expand Down Expand Up @@ -141,33 +141,33 @@ public function processWithConfiguration(BinaryInterface $binary, array $options
throw new \RuntimeException(sprintf('Temp file can not be created in "%s".', $tempDir));
}

$pb = new ProcessBuilder([$this->jpegoptimBin]);
$processArguments = [$this->jpegoptimBin];

$stripAll = array_key_exists('strip_all', $options) ? $options['strip_all'] : $this->stripAll;
if ($stripAll) {
$pb->add('--strip-all');
$processArguments[] = '--strip-all';
}

$max = array_key_exists('max', $options) ? $options['max'] : $this->max;
if ($max) {
$pb->add('--max='.$max);
$processArguments[] = '--max='.$max;
}

$progressive = array_key_exists('progressive', $options) ? $options['progressive'] : $this->progressive;
if ($progressive) {
$pb->add('--all-progressive');
$processArguments[] = '--all-progressive';
} else {
$pb->add('--all-normal');
$processArguments[] = '--all-normal';
}

$pb->add($input);
$processArguments[] = $input;
if ($binary instanceof FileBinaryInterface) {
copy($binary->getPath(), $input);
} else {
file_put_contents($input, $binary->getContent());
}

$proc = $pb->getProcess();
$proc = new Process($processArguments);
$proc->run();

if (false !== strpos($proc->getOutput(), 'ERROR') || 0 !== $proc->getExitCode()) {
Expand Down
19 changes: 9 additions & 10 deletions Imagine/Filter/PostProcessor/MozJpegPostProcessor.php
Expand Up @@ -14,7 +14,7 @@
use Liip\ImagineBundle\Binary\BinaryInterface;
use Liip\ImagineBundle\Model\Binary;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\ProcessBuilder;
use Symfony\Component\Process\Process;

/**
* mozjpeg post-processor, for noticably better jpeg compression.
Expand Down Expand Up @@ -85,24 +85,23 @@ public function processWithConfiguration(BinaryInterface $binary, array $options
return $binary;
}

$pb = new ProcessBuilder([$this->mozjpegBin]);
$processArguments = [$this->mozjpegBin];

// Places emphasis on DC
$pb->add('-quant-table');
$pb->add(2);
$processArguments[] = '-quant-table';
$processArguments[] = 2;

$transformQuality = array_key_exists('quality', $options) ? $options['quality'] : $this->quality;
if ($transformQuality !== null) {
$pb->add('-quality');
$pb->add($transformQuality);
$processArguments[] = '-quality';
$processArguments[] = $transformQuality;
}

$pb->add('-optimise');
$processArguments[] = '-optimise';

// Favor stdin/stdout so we don't waste time creating a new file.
$pb->setInput($binary->getContent());

$proc = $pb->getProcess();
$proc = new Process($processArguments);
$proc->setInput($binary->getContent());
$proc->run();

if (false !== strpos($proc->getOutput(), 'ERROR') || 0 !== $proc->getExitCode()) {
Expand Down
12 changes: 6 additions & 6 deletions Imagine/Filter/PostProcessor/OptiPngPostProcessor.php
Expand Up @@ -15,7 +15,7 @@
use Liip\ImagineBundle\Binary\FileBinaryInterface;
use Liip\ImagineBundle\Model\Binary;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\ProcessBuilder;
use Symfony\Component\Process\Process;

class OptiPngPostProcessor implements PostProcessorInterface, ConfigurablePostProcessorInterface
{
Expand Down Expand Up @@ -93,27 +93,27 @@ public function processWithConfiguration(BinaryInterface $binary, array $options
throw new \RuntimeException(sprintf('Temp file can not be created in "%s".', $tempDir));
}

$pb = new ProcessBuilder([$this->optipngBin]);
$processArguments = [$this->optipngBin];

$level = array_key_exists('level', $options) ? $options['level'] : $this->level;
if ($level !== null) {
$pb->add(sprintf('--o%d', $level));
$processArguments[] = sprintf('--o%d', $level);
}

$stripAll = array_key_exists('strip_all', $options) ? $options['strip_all'] : $this->stripAll;
if ($stripAll) {
$pb->add('--strip=all');
$processArguments[] = '--strip=all';
}

$pb->add($input);
$processArguments[] = $input;

if ($binary instanceof FileBinaryInterface) {
copy($binary->getPath(), $input);
} else {
file_put_contents($input, $binary->getContent());
}

$proc = $pb->getProcess();
$proc = new Process($processArguments);
$proc->run();

if (false !== strpos($proc->getOutput(), 'ERROR') || 0 !== $proc->getExitCode()) {
Expand Down
15 changes: 7 additions & 8 deletions Imagine/Filter/PostProcessor/PngquantPostProcessor.php
Expand Up @@ -14,7 +14,7 @@
use Liip\ImagineBundle\Binary\BinaryInterface;
use Liip\ImagineBundle\Model\Binary;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\ProcessBuilder;
use Symfony\Component\Process\Process;

/**
* pngquant post-processor, for optimal, web-safe, lossy png compression
Expand Down Expand Up @@ -83,18 +83,17 @@ public function processWithConfiguration(BinaryInterface $binary, array $options
return $binary;
}

$pb = new ProcessBuilder([$this->pngquantBin]);
$processArguments = [$this->pngquantBin];

// Specify quality.
$tranformQuality = array_key_exists('quality', $options) ? $options['quality'] : $this->quality;
$pb->add('--quality');
$pb->add($tranformQuality);
$processArguments[] = '--quality';
$processArguments[] = $tranformQuality;

// Read to/from stdout to save resources.
$pb->add('-');
$pb->setInput($binary->getContent());

$proc = $pb->getProcess();
$processArguments[] = '-';
$proc = new Process($processArguments);
$proc->setInput($binary->getContent());
$proc->run();

// 98 and 99 are "quality too low" to compress current current image which, while isn't ideal, is not a failure
Expand Down
1 change: 1 addition & 0 deletions Tests/Fixtures/bash/empty-command.sh
@@ -0,0 +1 @@
#!/bin/sh
23 changes: 23 additions & 0 deletions Tests/Imagine/Filter/PostProcessor/JpegOptimPostProcessorTest.php
@@ -0,0 +1,23 @@
<?php

namespace Liip\ImagineBundle\Tests\Imagine\Filter\PostProcessor;

use Liip\ImagineBundle\Binary\BinaryInterface;
use Liip\ImagineBundle\Imagine\Filter\PostProcessor\JpegOptimPostProcessor;
use Liip\ImagineBundle\Model\Binary;
use Liip\ImagineBundle\Tests\AbstractTest;

class JpegOptimPostProcessorTest extends AbstractTest
{
public function testJpegOptimPostProcessor()
{
$jpegOptimPostProcessor = new JpegOptimPostProcessor(
__DIR__.'/../../../Fixtures/bash/empty-command.sh'
);

$binary = new Binary('content', 'image/jpg', 'jpg');
$result = $jpegOptimPostProcessor->process($binary);

$this->assertInstanceOf(BinaryInterface::class, $result);
}
}
23 changes: 23 additions & 0 deletions Tests/Imagine/Filter/PostProcessor/MozJpegPostProcessorTest.php
@@ -0,0 +1,23 @@
<?php

namespace Liip\ImagineBundle\Tests\Imagine\Filter\PostProcessor;

use Liip\ImagineBundle\Binary\BinaryInterface;
use Liip\ImagineBundle\Imagine\Filter\PostProcessor\MozJpegPostProcessor;
use Liip\ImagineBundle\Model\Binary;
use Liip\ImagineBundle\Tests\AbstractTest;

class MozJpegPostProcessorTest extends AbstractTest
{
public function testMozJpegPostProcessor()
{
$mozJpegPostProcessor = new MozJpegPostProcessor(
__DIR__.'/../../../Fixtures/bash/empty-command.sh'
);

$binary = new Binary('content', 'image/jpg', 'jpg');
$result = $mozJpegPostProcessor->process($binary);

$this->assertInstanceOf(BinaryInterface::class, $result);
}
}
23 changes: 23 additions & 0 deletions Tests/Imagine/Filter/PostProcessor/OptiPngPostProcessorTest.php
@@ -0,0 +1,23 @@
<?php

namespace Liip\ImagineBundle\Tests\Imagine\Filter\PostProcessor;

use Liip\ImagineBundle\Binary\BinaryInterface;
use Liip\ImagineBundle\Imagine\Filter\PostProcessor\OptiPngPostProcessor;
use Liip\ImagineBundle\Model\Binary;
use Liip\ImagineBundle\Tests\AbstractTest;

class OptiPngPostProcessorTest extends AbstractTest
{
public function testJpegOptimPostProcessor()
{
$optiPngPostProcessor = new OptiPngPostProcessor(
__DIR__.'/../../../Fixtures/bash/empty-command.sh'
);

$binary = new Binary('content', 'image/png', 'png');
$result = $optiPngPostProcessor->process($binary);

$this->assertInstanceOf(BinaryInterface::class, $result);
}
}
23 changes: 23 additions & 0 deletions Tests/Imagine/Filter/PostProcessor/PngquantPostProcessorTest.php
@@ -0,0 +1,23 @@
<?php

namespace Liip\ImagineBundle\Tests\Imagine\Filter\PostProcessor;

use Liip\ImagineBundle\Binary\BinaryInterface;
use Liip\ImagineBundle\Imagine\Filter\PostProcessor\PngquantPostProcessor;
use Liip\ImagineBundle\Model\Binary;
use Liip\ImagineBundle\Tests\AbstractTest;

class PngquantPostProcessorTest extends AbstractTest
{
public function testPngquantPostProcessor()
{
$pngquantPostProcessor = new PngquantPostProcessor(
__DIR__.'/../../../Fixtures/bash/empty-command.sh'
);

$binary = new Binary('content', 'image/png', 'png');
$result = $pngquantPostProcessor->process($binary);

$this->assertInstanceOf(BinaryInterface::class, $result);
}
}