You are here

public function JpegOptim::applyToImage in Image Optimize Binaries 8

Overrides ImageAPIOptimizeProcessorBinaryBase::applyToImage

File

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

Class

JpegOptim
Uses the JpegOptim 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/jpeg') {
      $options = array(
        '--quiet',
        '--strip-all',
      );
      $arguments = array(
        $dst,
      );
      if (is_numeric($this->configuration['progressive'])) {
        switch ($this->configuration['progressive']) {
          case 0:
            $options[] = '--all-normal';
            break;
          case 1:
            $options[] = '--all-progressive';
            break;
        }
      }
      if (is_numeric($this->configuration['quality'])) {
        $options[] = '--max=' . escapeshellarg($this->configuration['quality']);
      }
      if (is_numeric($this->configuration['size'])) {
        $options[] = '--size=' . escapeshellarg($this->configuration['size'] . '%');
      }
      return $this
        ->execShellCommand($cmd, $options, $arguments);
    }
  }
  return FALSE;
}