You are here

public function PngCrush::applyToImage in Image Optimize Binaries 8

Overrides ImageAPIOptimizeProcessorBinaryBase::applyToImage

File

src/Plugin/ImageAPIOptimizeProcessor/PngCrush.php, line 26

Class

PngCrush
Uses the PngCrush binary to optimize images.

Namespace

Drupal\imageapi_optimize_binaries\Plugin\ImageAPIOptimizeProcessor

Code

public function applyToImage($image_uri) {
  if ($cmd = $this
    ->getFullPathToBinary()) {
    $dst = $this
      ->sanitizeFilename($image_uri);
    if ($this
      ->getMimeType($image_uri) == 'image/png') {
      $temp_file = $this->fileSystem
        ->tempnam('temporary://', 'file');
      $options = array(
        '-rem alla',
        '-reduce',
        '-brute',
        '-q',
      );
      $arguments = array(
        $dst,
        $this->fileSystem
          ->realpath($temp_file),
      );
      if ($this
        ->execShellCommand($cmd, $options, $arguments)) {
        return (bool) \Drupal::service('file_system')
          ->move($temp_file, $image_uri, FileSystemInterface::EXISTS_REPLACE);
      }
    }
  }
  return FALSE;
}