You are here

class OptiPng in Image Optimize Binaries 8

Uses the OptiPng binary to optimize images.

Plugin annotation


@ImageAPIOptimizeProcessor(
  id = "optipng",
  label = @Translation("OptiPng"),
  description = @Translation("Uses the OptiPng binary to optimize images.")
)

Hierarchy

  • class \Drupal\imageapi_optimize_binaries\ImageAPIOptimizeProcessorBinaryBase extends \Drupal\imageapi_optimize\ConfigurableImageAPIOptimizeProcessorBase
    • class \Drupal\imageapi_optimize_binaries\Plugin\ImageAPIOptimizeProcessor\OptiPng

Expanded class hierarchy of OptiPng

File

src/Plugin/ImageAPIOptimizeProcessor/OptiPng.php, line 17

Namespace

Drupal\imageapi_optimize_binaries\Plugin\ImageAPIOptimizeProcessor
View source
class OptiPng extends ImageAPIOptimizeProcessorBinaryBase {

  /**
   * {@inheritdoc}
   */
  protected function executableName() {
    return 'optipng';
  }
  public function applyToImage($image_uri) {
    if ($cmd = $this
      ->getFullPathToBinary()) {
      $dst = $this
        ->sanitizeFilename($image_uri);
      if ($this
        ->getMimeType($image_uri) == 'image/png') {
        $options = array(
          '--quiet',
        );
        $arguments = array(
          $dst,
        );
        if (is_numeric($this->configuration['interlace'])) {
          $options[] = '-i ' . $this->configuration['interlace'];
        }
        $options[] = '-o' . $this->configuration['level'];
        return $this
          ->execShellCommand($cmd, $options, $arguments);
      }
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return parent::defaultConfiguration() + [
      'level' => 5,
      'interlace' => '',
    ];
  }
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildConfigurationForm($form, $form_state);
    $form['level'] = array(
      '#title' => t('Optimization level'),
      '#type' => 'select',
      '#options' => range(0, 7),
      '#default_value' => $this->configuration['level'],
    );
    $form['interlace'] = array(
      '#title' => $this
        ->t('Interlace'),
      '#type' => 'select',
      '#options' => array(
        '' => $this
          ->t('No change'),
        0 => $this
          ->t('Non-interlaced'),
        1 => $this
          ->t('Interlaced'),
      ),
      '#default_value' => $this->configuration['interlace'],
      '#description' => t('If "No change" is select, the output will have the same interlace type as the input.'),
    );
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    parent::submitConfigurationForm($form, $form_state);
    $this->configuration['level'] = $form_state
      ->getValue('level');
    $this->configuration['interlace'] = $form_state
      ->getValue('interlace');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ImageAPIOptimizeProcessorBinaryBase::$fileSystem protected property The file system service.
ImageAPIOptimizeProcessorBinaryBase::$shellOperations protected property The imageapi shell operation service.
ImageAPIOptimizeProcessorBinaryBase::create public static function
ImageAPIOptimizeProcessorBinaryBase::execShellCommand protected function Execute a shell command on the local system.
ImageAPIOptimizeProcessorBinaryBase::findExecutablePath protected function Search the local system for the given executable binary.
ImageAPIOptimizeProcessorBinaryBase::getFullPathToBinary public function
ImageAPIOptimizeProcessorBinaryBase::getSummary public function
ImageAPIOptimizeProcessorBinaryBase::sanitizeFilename protected function Sanitize the given path for binary processors.
ImageAPIOptimizeProcessorBinaryBase::saveCommandStdoutToFile protected function
ImageAPIOptimizeProcessorBinaryBase::__construct public function
OptiPng::applyToImage public function Overrides ImageAPIOptimizeProcessorBinaryBase::applyToImage
OptiPng::buildConfigurationForm public function Overrides ImageAPIOptimizeProcessorBinaryBase::buildConfigurationForm
OptiPng::defaultConfiguration public function Overrides ImageAPIOptimizeProcessorBinaryBase::defaultConfiguration
OptiPng::executableName protected function Overrides ImageAPIOptimizeProcessorBinaryBase::executableName
OptiPng::submitConfigurationForm public function Overrides ImageAPIOptimizeProcessorBinaryBase::submitConfigurationForm