You are here

class AdvPng in Image Optimize Binaries 8

Uses the AdvPng binary to optimize images.

Plugin annotation


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

Hierarchy

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

Expanded class hierarchy of AdvPng

1 file declares its use of AdvPng
AdvPngTest.php in tests/src/Unit/AdvPngTest.php

File

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

Namespace

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

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

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return parent::defaultConfiguration() + [
      'recompress' => TRUE,
      'mode' => 3,
    ];
  }
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildConfigurationForm($form, $form_state);
    $form['recompress'] = array(
      '#title' => $this
        ->t('Recompress'),
      '#type' => 'checkbox',
      '#default_value' => $this->configuration['recompress'],
    );
    $form['mode'] = array(
      '#title' => $this
        ->t('Compression mode'),
      '#type' => 'select',
      '#options' => array(
        0 => $this
          ->t('Disabled'),
        1 => $this
          ->t('Fast'),
        2 => $this
          ->t('Normal'),
        3 => $this
          ->t('Extra'),
        4 => $this
          ->t('Insane'),
      ),
      '#default_value' => $this->configuration['mode'],
    );
    return $form;
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
AdvPng::applyToImage public function Overrides ImageAPIOptimizeProcessorBinaryBase::applyToImage
AdvPng::buildConfigurationForm public function Overrides ImageAPIOptimizeProcessorBinaryBase::buildConfigurationForm
AdvPng::defaultConfiguration public function Overrides ImageAPIOptimizeProcessorBinaryBase::defaultConfiguration
AdvPng::executableName protected function Overrides ImageAPIOptimizeProcessorBinaryBase::executableName
AdvPng::submitConfigurationForm public function Overrides ImageAPIOptimizeProcessorBinaryBase::submitConfigurationForm
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