Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Jul 4, 2017
1 parent e8ac030 commit 16a37e4
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 18 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"require": {
"php": "^7.0",
"league/glide": "^1.2",
"ps/image-optimizer": "^1.1",
"spatie/image-optimizer": "^0.0.4",
"spatie/temporary-directory": "^1.0.0",
"symfony/process": "^3.0"
},
Expand Down
7 changes: 0 additions & 7 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,4 @@
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<logging>
<log type="tap" target="build/report.tap"/>
<log type="junit" target="build/report.junit.xml"/>
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
</phpunit>
27 changes: 17 additions & 10 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use BadMethodCallException;
use ImageOptimizer\OptimizerFactory;
use Spatie\Image\Exceptions\InvalidImageDriver;
use Spatie\ImageOptimizer\OptimizerChainFactory;

/** @mixin \Spatie\Image\Manipulations */
class Image
Expand Down Expand Up @@ -43,7 +44,7 @@ public function __construct(string $pathToImage)
*/
public function useImageDriver(string $imageDriver)
{
if (! in_array($imageDriver, ['gd', 'imagick'])) {
if (!in_array($imageDriver, ['gd', 'imagick'])) {
throw InvalidImageDriver::driver($imageDriver);
}

Expand Down Expand Up @@ -72,7 +73,7 @@ public function manipulate($manipulations)

public function __call($name, $arguments)
{
if (! method_exists($this->manipulations, $name)) {
if (!method_exists($this->manipulations, $name)) {
throw new BadMethodCallException("Manipulation `{$name}` does not exist");
}

Expand Down Expand Up @@ -100,26 +101,32 @@ public function save($outputPath = '')
->save($outputPath);

if ($this->shouldOptimize()) {
$opmitizationOptions = $this->manipulations->getFirstManipulationArgument('optimize');
$optimizerChainConfiguration = $this->manipulations->getFirstManipulationArgument('optimize');

$opmitizationOptions = json_decode('{}', true);
$optimizerChainConfiguration = json_decode($optimizerChainConfiguration, true);

$this->performOptimization($outputPath, $opmitizationOptions);
$this->performOptimization($outputPath, $optimizerChainConfiguration);
}
}

protected function shouldOptimize(): bool
{
return ! is_null($this->manipulations->getFirstManipulationArgument('optimize'));
return !is_null($this->manipulations->getFirstManipulationArgument('optimize'));
}

protected function performOptimization($path, array $optimizationOptions)
protected function performOptimization($path, array $optimizerChainConfiguration)
{
$factory = new OptimizerFactory($optimizationOptions);
$optimizerChain = OptimizerChainFactory::create();

$optimizer = $factory->get();
if (count($optimizerChainConfiguration)) {
$optimizers = array_map(function (array $optimizerOptions, string $optimizerClassName) {
return (new $optimizerClassName)->setOptions($optimizerOptions);
}, $optimizerChainConfiguration, array_keys($optimizerChainConfiguration));

$optimizer->optimize($path);
$optimizerChain->setOptimizers($optimizers);
}

$optimizerChain->optimize($path);
}

protected function addFormatManipulation($outputPath)
Expand Down
15 changes: 15 additions & 0 deletions tests/Manipulations/OptimizeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Spatie\Image\Image;
use Spatie\Image\Test\TestCase;
use Spatie\ImageOptimizer\Optimizers\Jpegoptim;

class OptimizeTest extends TestCase
{
Expand All @@ -18,4 +19,18 @@ public function it_can_optimize_an_image()

$this->assertFileExists($targetFile);
}

/** @test */
public function it_can_optimize_an_image_with_the_given_optimization_options()
{
$targetFile = $this->tempDir->path('optimized.jpg');

Image::load($this->getTestFile('test.jpg'))
->optimize([Jpegoptim::class => [
'--all-progressive',
]])
->save($targetFile);

$this->assertFileExists($targetFile);
}
}

0 comments on commit 16a37e4

Please sign in to comment.