Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #14 from seltix5/master
Browse files Browse the repository at this point in the history
Option to halt processing of failed optimisation, or continue processing command chain.
  • Loading branch information
bensquire authored Dec 2, 2018
2 parents 6118f9b + 225438f commit fed7351
Show file tree
Hide file tree
Showing 12 changed files with 14 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/PHPImageOptim/PHPImageOptim.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ public function chainCommand($object): PHPImageOptim
/**
* @return bool
*/
public function optimise(): bool
public function optimise($stopIfFail = true): bool
{
$this->stopIfFail = $stopIfFail;

foreach ($this->chainedCommands as $chainedCommand) {
$chainedCommand->determinePreOptimisedFileSize();
$chainedCommand->setImagePath($this->imagePath);
Expand Down
1 change: 1 addition & 0 deletions src/PHPImageOptim/Tools/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Common
protected $originalFileSize = '';
protected $finalFileSize = '';
protected $optimisationLevel = 1;
protected $stopIfFail = true;

/**
* @param string $binaryPath
Expand Down
2 changes: 1 addition & 1 deletion src/PHPImageOptim/Tools/Gif/Gifsicle.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Gifsicle extends Common implements ToolsInterface
public function optimise(): ToolsInterface
{
exec($this->binaryPath . ' -b -O2 ' . escapeshellarg($this->imagePath), $aOutput, $iResult);
if ($iResult !== 0) {
if ($this->stopIfFail && $iResult !== 0) {
throw new Exception('GIFSICLE was unable to optimise image, result:' . $iResult . ' File: ' . $this->imagePath);
}

Expand Down
2 changes: 1 addition & 1 deletion src/PHPImageOptim/Tools/Jpeg/Guetzli.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function optimise(): ToolsInterface
$iResult
);

if ($iResult !== 0) {
if ($this->stopIfFail && $iResult !== 0) {
throw new Exception('GUETZLI was unable to optimise image, result:' . $iResult . ' File: ' . $this->imagePath);
}
return $this;
Expand Down
2 changes: 1 addition & 1 deletion src/PHPImageOptim/Tools/Jpeg/JpegOptim.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class JpegOptim extends Common implements ToolsInterface
public function optimise(): ToolsInterface
{
exec($this->binaryPath . ' --strip-all --all-progressive ' . escapeshellarg($this->imagePath), $aOutput, $iResult);
if ($iResult !== 0) {
if ($this->stopIfFail && $iResult !== 0) {
throw new Exception('JPEGOPTIM was unable to optimise image, result:' . $iResult . ' File: ' . $this->imagePath);
}

Expand Down
2 changes: 1 addition & 1 deletion src/PHPImageOptim/Tools/Jpeg/JpegTran.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class JpegTran extends Common implements ToolsInterface
public function optimise(): ToolsInterface
{
exec($this->binaryPath . ' -optimize ' . escapeshellarg($this->imagePath), $aOutput, $iResult);
if ($iResult !== 0) {
if ($this->stopIfFail && $iResult !== 0) {
throw new Exception('JPEGTRAN was unable to optimise image, result:' . $iResult . ' File: ' . $this->imagePath);
}

Expand Down
2 changes: 1 addition & 1 deletion src/PHPImageOptim/Tools/Jpeg/MozJpeg.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function optimise(): ToolsInterface

rename($tempFile, $this->imagePath);

if ($iResult !== 0) {
if ($this->stopIfFail && $iResult !== 0) {
throw new Exception('MOZJPEG was unable to optimise image, result:' . $iResult . ' File: ' . $this->imagePath);
}
return $this;
Expand Down
2 changes: 1 addition & 1 deletion src/PHPImageOptim/Tools/Png/AdvPng.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class AdvPng extends Common implements ToolsInterface
public function optimise(): ToolsInterface
{
exec($this->binaryPath . ' -z -4 -i20 -- ' . escapeshellarg($this->imagePath), $aOutput, $iResult);
if ($iResult !== 0) {
if ($this->stopIfFail && $iResult !== 0) {
throw new Exception('ADVPNG was unable to optimise image, result:' . $iResult . ' File: ' . $this->imagePath);
}

Expand Down
2 changes: 1 addition & 1 deletion src/PHPImageOptim/Tools/Png/OptiPng.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class OptiPng extends Common implements ToolsInterface
public function optimise(): ToolsInterface
{
exec($this->binaryPath . ' -i0 -o7 -zm1-9 ' . escapeshellarg($this->imagePath), $aOutput, $iResult);
if ($iResult !== 0) {
if ($this->stopIfFail && $iResult !== 0) {
throw new Exception('OPTIPNG was unable to optimise image, result:' . $iResult . ' File: ' . $this->imagePath);
}

Expand Down
2 changes: 1 addition & 1 deletion src/PHPImageOptim/Tools/Png/PngCrush.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function optimise(): ToolsInterface
// Switch back to previous directory
chdir($prevDir);

if ($iResult != 0) {
if ($this->stopIfFail && $iResult != 0) {
throw new Exception('PNGCRUSH was unable to optimise image, result:' . $iResult . ' File: ' . $this->imagePath);
}

Expand Down
2 changes: 1 addition & 1 deletion src/PHPImageOptim/Tools/Png/PngOut.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function optimise(): ToolsInterface
return $this;
}

if ($iResult !== 0) {
if ($this->stopIfFail && $iResult !== 0) {
throw new Exception('PNGOUT was unable to optimise image, result:' . $iResult . ' File: ' . $this->imagePath);
}

Expand Down
2 changes: 1 addition & 1 deletion src/PHPImageOptim/Tools/Png/PngQuant.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class PngQuant extends Common implements ToolsInterface
public function optimise(): ToolsInterface
{
exec($this->binaryPath . ' --speed 1 --ext=.png --force ' . escapeshellarg($this->imagePath), $aOutput, $iResult);
if ($iResult !== 0) {
if ($this->stopIfFail && $iResult !== 0) {
throw new Exception('PNGQUANT was unable to optimise image, result:' . $iResult . ' File: ' . $this->imagePath);
}

Expand Down

0 comments on commit fed7351

Please sign in to comment.