Skip to content

Commit

Permalink
Use process instead of processbuilder for postprocess filters and add…
Browse files Browse the repository at this point in the history
… additional tests
  • Loading branch information
fabianbloching authored and robfrawley committed Feb 6, 2018
1 parent 63e3fad commit 66821a5
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 32 deletions.
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);
}
}

0 comments on commit 66821a5

Please sign in to comment.